• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

Undefined and (-1) being returned from variables

Carcophan

Member
Hello everyone.

I have a script that successfully generates a random map using ds_grid_create. Additionally, the mp_grid_draw(global.gridPathing) works as expected, showing proper pathing, etc.

I can move around, collision works well. However, when I go to inspect any of the individual tile elements with RMouse pop up messages, I am getting -1 or undefined values returned on screen.

I know I am doing something that is overly simple wrong, was hoping for an extra set of eyes. Searching for 'undefined' doesn't really yield much valid content. I keep experimenting with different concepts and ideas I see online but nothing seems to help.


var mx = tilemap_get_at_pixel(global.map_id, mouse_x, mouse_y);
var my = tilemap_get_at_pixel(global.map_id, mouse_x, mouse_y);
var IndexMouse = tilemap_get(global.map_id, mx, my); //displays as -1
//i've replaced the map_id with cell and cellFill for fun, but neither worked, just in case.

//also as a debug message, these show undefined.
var tester1 = global.cell[# mouse_x, mouse_y]; //displays as undefined
var tester2 = global.cellFill[# mouse_x, mouse_y]; //displays as undefined

global.lay_id = layer_get_id("BGTiles");
global.map_id = layer_tilemap_get_id(global.lay_id); //cell/cellFill

global.map_id = ds_grid_create(mazeWidth, mazeHeight);
ds_grid_set_region(global.map_id, 0, 0, mazeWidth, mazeHeight, 1);
These are triggered from a mouse click event within a player step object. The grids are generated in the create event in a level object.

The current code is more example than anything, used in an attempt to debug, not all the code is shown above.
Code:
    show_message(string(tester1) + " and " + string(tester2) + " and AA = " + string(outVal11) + " and BB =  " + string(outVal22)
    + "  CX: " + string(cx) + " & MX: " + string(mx) + " & Mouse Index: " + string(tileIndexOfMouse)
    + "  & CharIndex: " + string(tileIndexOfChar) + " & Step Loc:  " + string(charStepLoc))
So MX and the tileindexofMouse that is fed by mx are both returning -1's.




Thoughts?
Thanks in advance.
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
For ds_grid operations, you would need to divide your mouse coordinates by your cell coordinates.

For tilemap operations, your code doesn't seem to be making any sense - you are getting the same value into separate mx/my variables and then trying to use it as a coordinate? "mx" would already contain your desired tile data.
 

Carcophan

Member
Thanks for the feedback Yellow.

I hadn't ever had to explicitly divide the mouse_x _y by the tileWidth/roomwidth/whatever before, and was able to get X and Y tile information with similar code in a different project.

My thought process is pointing to this being caused by how the grids are used/assigned.

Code:
for ( global.i = 0; global.i < mazeWidth; global.i++){
    for ( global.j = 0; global.j < mazeHeight; global.j++){
  
    global.cellFill = ds_grid_get(global.maze, global.i, global.j);
    //show_debug_message(string(global.cellFill))  //is working - is showing 1/0s. as expected
    draw_sprite(sCells, global.cellFill, global.i*global.tileSize, global.j*global.tileSize);
    tilemap_set(global.map_id, global.cellFill, global.i, global.j);
    tilemap_set(global.grid2, global.cellFill, global.i, global.j);
    }//draws map as expected
}


//and-or
for ( global.i = 0; global.i < mazeWidth; global.i++){
    for ( global.j = 0; global.j < mazeHeight; global.j++){
   
        global.cellFill = ds_grid_get(global.maze, global.i, global.j);
        //show_debug_message(string(global.cellFill))  //is working - is showing 1/0s. 
   
        if(global.cellFill = 1) { 
            global.grid2[# global.i, global.j] = 1; 
            //tilemap_set(global.map_id, 1, global.i, global.j);
        }
   
        if(global.cellFill = 0) { 
            global.grid2[# global.i, global.j] = 0; 
            //tilemap_set(global.map_id, 0, global.i, global.j);
        }
        if(global.grid2[# global.i, global.j] = 0){

        }else{
            mp_grid_add_cell(global.gridPathing, global.i, global.j);  //works
        }
    }
}
 
Last edited:

Carcophan

Member
I've tried the following, just for my curiosity:
Code:
    var px1 = global._hp_map_id[# obj_Player01.x, obj_Player01.y]
    var px2 = global.gridHP_[# obj_Player01.x, obj_Player01.y]
    var px3 = global.cellFill[# obj_Player01.x, obj_Player01.y]
    var mx1a = tilemap_get_at_pixel(global._hp_map_id, mouse_x, mouse_y);
    var mx2a = tilemap_get_at_pixel(global._hp_map_id, mouse_x, mouse_y);
    var mx3a = tilemap_get_at_pixel(global._hp_map_id, mouse_x, mouse_y);
    var mx1b = global._hp_map_id[# mouse_x, mouse_y];
    var mx2b = global.gridHP_[# mouse_x, mouse_y];
    var mx3b = global.cellFill[# mouse_x, mouse_y];
    var mx1c = global._hp_map_id[# mouse_x div global.tileSize / 2, mouse_y div global.tileSize / 2];
    var mx2c = global.gridHP_[# mouse_x div global.tileSize / 2, mouse_y div global.tileSize / 2];
    var mx3c = global.cellFill[# mouse_x div global.tileSize / 2, mouse_y div global.tileSize / 2];

show_message(" `: "            + string(px1)
    + " `: "                + string(px2)
    + " `: "                + string(px3)
    + " `: "                + string(mx1a)
    + " `: "                + string(mx2a)
    + " `: "                + string(mx3a)
    + " `: "                + string(mx1b)
    + " `: "                + string(mx2b)
    + " `: "                + string(mx3b)
    + " `: "                + string(mx1c)
    + " `: "                + string(mx2c)
    + " `: "                + string(mx3c)
)
And am still getting my -1's and undefined, instead of X/Y values or 1 and 0's or other values.
 
Top