Legacy GM ds_grid_add not recognizing 0 as a number

P

Prometheus1998_

Guest
I'm trying to code a 3-dimensional grid structure for Game Maker Studio version 1.4.1804, and I've hit a bit of a snag. Every time I test the code to add a value to the data structure, it gives me this:

############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object obj_test:

ds_grid_add argument 1 incorrect type (undefined) expecting a Number (YYGI32)
at P�K� (line 14) - ds_grid_add(grid,real(tx),real(ty),val);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_ds_3d_add (line 14)
called from - gml_Object_obj_test_CreateEvent_1 (line 5) - ds_3d_add(test,1,1,1,2);
called from - gml_Object_obj_camera_Key_SPACE_1 (line 2) - instance_create(self.x,self.y,obj_test);

Here's the script that encounters the error:
Code:
///ds_3d_add(id,val,x,y,z)
//Initialize the values
var ds_3d = argument0;
var val = argument1;
var tx = real(argument2);
var ty = real(argument3);
var tz = real(argument4);

//Check data type
ds_3d_axisError(argument2,argument3,argument4);

//Add value to 3d grid
var grid = ds_map_find_value(ds_3d,real(tz));
ds_grid_add(grid,real(tx),real(ty),val);
Here's the "ds_3d_axisError" script as well, which exists solely to check if the coordinate inputs aren't numbers:
Code:
//Check if all inputs are type real; if not, crash the game.
if (is_real(argument0) && is_real(argument1) && is_real(argument2)){
    show_message("All good on input types; proceeding...");
    return false;
}
else{
    show_message("Only use numbers for grid coordinates.");
    game_end();
}
Every time I run it, the "ds_3d_axisError" script tells me the inputs are fine... and then the error message in the spoiler tags pops up. Does anyone have any idea what's going on here?
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Are you sure "grid" actually exists? When trying to read a value from a map, and the key doesn't exist, it returns undefined.
 
P

Prometheus1998_

Guest
Are you sure "grid" actually exists? When trying to read a value from a map, and the key doesn't exist, it returns undefined.
Here's the script that's supposed to initialize the map and its component grids:
Code:
///ds_3d_create(x,y,z)
//Initialize the values
ds_3d = ds_map_create();
var tz;

//Check if all coordinates are numbers; if not, crash the game.
ds_3d_axisError(argument0,argument1,argument2);

//Create the 3d grid
for(tz = 0;tz < argument2;tz += 1){
    stz = ds_grid_create(argument0,argument1);
    ds_map_add(ds_3d,tz,stz);
}

//Verify the structure created properly
if ds_exists(ds_3d,ds_type_map)=true{
    show_message("ds_map exists.");
    for (tz = 0; tz < argument2; tz += 1){
        var str = ds_map_find_value(ds_3d,tz);
        if ds_exists(str,ds_type_grid)=true{
            show_message("ds_grid at z-value "+string(tz)+" exists.");
        }
        else{
            show_message("ds_grid at z-value "+string(tz)+" does not exist.");
        }
    }
}
else{
    show_message("ds_map does not exist.");
}


//Return 3d grid's ID
return ds_3d;
On running it, the checks show all data structures that should be created exist.

EDIT: Changing the variable holding the grid's ID from a temporary to a normal variable didn't solve the problem. I didn't think it would, but that's one more thing checked at least.

Anyone have any ideas at all?
 
Last edited by a moderator:

TheSnidr

Heavy metal viking dentist
GMC Elder
In ds_3d_add, do this check:
if is_undefined(grid) show_message("Grid does not exist");
 
P

Prometheus1998_

Guest
In ds_3d_add, do this check:
if is_undefined(grid) show_message("Grid does not exist");
That indeed shows that the value is undefined. How do I go about fixing the problem?
 

FrostyCat

Redemption Seeker
First I would use the debugger to inspect the data structure for improper initializations. Remember to right-click on the data structure values to select the correct type. If undefined values or a series of identical IDs appear at lower nesting levels, that is a sure sign something is wrong.

Another possible source of your problem is floating-point error or overflows. Maps only use strings as keys, and if you pass in a numeric value as a key, it gets converted to a string first. You initialized only the ones mapped to integers, and only within a given range. If you get something under the decimal point due to floating-point or go outside the given range, the undefined value is to be expected.

Personally, I do not see a premium in using a data structure for your situation over a nested array, in runtime or devtime. It's a dense setup instead of a sparse setup, and the numbering is a strict zero-index, so the map is basically pointless. A 2D array nested in a 1D array would have done the exact same job, but with better type safety and support for automatic garbage collection. Using data structures while only employing the functionality that arrays can also offer makes little sense to me, but it seems rather fashionable in GM user circles online.
 
P

Prometheus1998_

Guest
First I would use the debugger to inspect the data structure for improper initializations. Remember to right-click on the data structure values to select the correct type. If undefined values or a series of identical IDs appear at lower nesting levels, that is a sure sign something is wrong.

Another possible source of your problem is floating-point error or overflows. Maps only use strings as keys, and if you pass in a numeric value as a key, it gets converted to a string first. You initialized only the ones mapped to integers, and only within a given range. If you get something under the decimal point due to floating-point or go outside the given range, the undefined value is to be expected.

Personally, I do not see a premium in using a data structure for your situation over a nested array, in runtime or devtime. It's a dense setup instead of a sparse setup, and the numbering is a strict zero-index, so the map is basically pointless. A 2D array nested in a 1D array would have done the exact same job, but with better type safety and support for automatic garbage collection. Using data structures while only employing the functionality that arrays can also offer makes little sense to me, but it seems rather fashionable in GM user circles online.
Arrays seem to be far more cooperative than ds_maps or ds_grids have been. I don't know why I hadn't tried going that route before. Thank you for the recommendation.
 
Top