• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Updating ds_grid with object [SOLVED]

J

Jacknoshima

Guest
Hi,

So I'm trying to set up a system where an option in the inventory screen updates the object stored in the inventory ds_grid, and it's acting really weird on me.

So, if I run this code:

/// @arg object
var object = argument0;
ds_grid_set(global.ds_inventory, 0, 0, object);​

My bow in the ds_grid will update just fine (assuming it's in position 0). That it might not be in position 0 means I have to cycle through the grid to find it, and then update it where it is, so I tried:

/// @arg object
var object = argument0;
var grid_index = -1;

for (var j = 0; j < ds_grid_height(global.ds_inventory); j++)
{
if ds_grid_get(global.ds_inventory, 0, j) == o_bow_standard
{
grid_index = j;​
}​
}

show_debug_message(string(grid_index));

ds_grid_set(global.ds_inventory, 0, grid_index, object);​

And this just doesn't work.

I added debugging messages and despite the fact that I KNOW for a fact that my bow is in the inventory, the grid_index doesn't get assigned anything except -1.

However, when I add in the loop but change the ds_grid_set back to being 0 instead of grid index, the debug message shows the correct grid_index and the bow updates.

This is the code:

/// @arg object
var object = argument0;
var grid_index = -1;
for (var j = 0; j < ds_grid_height(global.ds_inventory) + 1; j++)
{
if ds_grid_get(global.ds_inventory, 0, j) == o_bow_standard
{
grid_index = j;​
}​
}

show_debug_message(string(grid_index));

ds_grid_set(global.ds_inventory, 0, 0, object);​

The above works and the debug message outputs 0, which is the y value of the bow in the ds_grid.

So why does the grid_index not get assigned when I use the grid_index to update the value in the grid after the grid_index should have been assigned but it does get assigned when I use a static number to update the grid?
 
C

CedSharp

Guest
Welcome to the Game Maker forums!
In the future, please use [ code ].......[ /code ] ( without the spaces ) for any code, as it will make it easier for us to read.
Code:
Here is an example.
Now, if your inventory is empty, or does not contain the o_bow_standard object, grid_index will not get assigned, and you will end up calling this
Code:
ds_grid_set(global.ds_inventory, 0, -1, object);
which should produce an error.

Try running this code and see what the debug shows:
Code:
/// @arg object
var object = argument0;
var grid_index = -1;
var log = "global.ds_inventory, column 0 =";
var cell = -1;
for(var j = 0; j < ds_grid_height(global.ds_inventory) + 1; j++ )
{
    cell = ds_grid_get(global.ds_inventory, 0, j);
    if( cell == o_bow_standard ) {
        grid_index = j;
    }
    log += " [ "+string(j)+" = "+string(cell)+" ]";
}

show_debug_message(log);
show_debug_message(string(grid_index));
See if the bow actually is in the inventory when the script is called :)

EDIT: You can (and probably should) use the debugger instead, this is lots of work for something that the debugger already does, stupid me. Do as @Paskaler said ;)
 
J

Jacknoshima

Guest
Hey,

Sorry, I wasn't sure what the code tags were, but will remember them in future, thanks!

I tried putting in the break statement but it's still having the same issue.

I know the bow is in the inventory because on the same screen I create sprites from everything that's in my inventory, if it weren't there then it wouldn't show up. And it registers the bow's location in my inventory so long as I don't try to use the grid_index variable to update the bow. Grid index returns the location if I do:

Code:
/// @arg object
var object = argument0;
var grid_index = -1;
for (var j = 0; j < ds_grid_height(global.ds_inventory) + 1; j++)
{
 
 if ds_grid_get(global.ds_inventory, 0, j) == o_bow_standard
 {
  grid_index = j;
 
  break;
 }
}
show_debug_message(string(grid_index));
ds_grid_set(global.ds_inventory, 0, 0, object);
the
Code:
show_debug_message(string(grid_index));
line outputs 0

if I change
Code:
ds_grid_set(global.ds_inventory, 0, 0, object);
to
Code:
ds_grid_set(global.ds_inventory, grid_index, 0, object);
the same debug message outputs -1

I added breakpoints so I could see what they all show in the debugger and everything is the same as what I've said above.
 
J

Jacknoshima

Guest
That debugging script has helped me see the issue a little better

So I added:

Code:
show_debug_message("Standard Bow ID:" + string(o_bow_standard));
to what you posted, to make:

Code:
/// @arg object
var object = argument0;
var grid_index = -1;
var log = "global.ds_inventory, column 0 =";
var cell = -1;
show_debug_message("Standard Bow ID:" + string(o_bow_standard));
for (var j = 0; j < ds_grid_height(global.ds_inventory) + 1; j++)
{
 
 cell = ds_grid_get(global.ds_inventory, 0, j);
 if cell == o_bow_standard
 {
  grid_index = j;
 }
 log += " [ "+string(j)+" = "+string(cell)+" ]";
}
show_debug_message(log);
show_debug_message(string(grid_index));

ds_grid_set(global.ds_inventory, 0, grid_index, object);
which outputted:
Code:
Standard Bow ID:24
global.ds_inventory, column 0 = [ 0 = 100057 ] [ 1 = 100064 ] [ 2 = undefined ]
and if I switch it to:
Code:
/// @arg object
var object = argument0;
var grid_index = -1;
var log = "global.ds_inventory, column 0 =";
var cell = -1;
show_debug_message("Standard Bow ID:" + string(o_bow_standard));
for (var j = 0; j < ds_grid_height(global.ds_inventory) + 1; j++)
{
 
 cell = ds_grid_get(global.ds_inventory, 0, j);
 if cell == o_bow_standard
 {
  grid_index = j;
 }
 log += " [ "+string(j)+" = "+string(cell)+" ]";
}
show_debug_message(log);
show_debug_message(string(grid_index));

ds_grid_set(global.ds_inventory, 0, 0, object);
it outputs:
Code:
[/COLOR][/FONT][/LEFT]
Standard Bow ID:24
global.ds_inventory, column 0 = [ 0 = 24 ] [ 1 = 100064 ] [ 2 = undefined ]

so it's like it's altering the ID of the instance...
 
C

CedSharp

Guest
Note that in GameMaker, there are 2 'type' of IDs:

Resources IDs, for example 'o_bow_standard' which is 24,
and Instance IDs, which usually starts at 10000.

When you place many of one object in a room, each of them are called an instance.
Each instance has a different ID.

That ID is NOT THE SAME as the object's id.
To get the Object's ID from an instance, you use the object_index variable:

Change the log variable in the script to this:
Code:
if(!is_undefined(cell) && instance_exists(cell)) log += " [ "+string(j)+" = "+string(cell.object_index)+" ]";
else log += " [ "+string(j) + " = empty ]";
This way, it should show the object ID instead ( for example, 24 ).
Hope this solves your issue!
 
J

Jacknoshima

Guest
Oh! That did it!
I changed it to
Code:
 if cell.object_index == o_bow_standard.object_index
and it works perfectly now

Thank you so much! I'll make sure I remember about the difference between ID's, I had no idea about that
 
C

CedSharp

Guest
Oh! That did it!
I changed it to
Code:
 if cell.object_index == o_bow_standard.object_index
and it works perfectly now

Thank you so much! I'll make sure I remember about the difference between ID's, I had no idea about that
obj_bow_standard is already an object index, you can ommit it:
Code:
if cell.object_index == obj_bow_standard
 
J

Jacknoshima

Guest
I was being overly cautious haha
I took it off and I'll bare that in mind, thank you
 
Top