• 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 [SOLVED] Finding a specific item in a ds_grid and subtracting it.

PlayerOne

Member
Right now I'm creating an upgrade system akin to the Fallout 4 / New Vegas workbench system. Everything is set up but I need to find the first item id of an item that I'm specifically looking for. In this case the item id is a sprite that is used to identify items in my players inventory.

Now I have found a function that identifies if an item exists in the grid.

Code:
ds_grid_value_exists(global.player_inv, 0, 0, ds_grid_width(global.player_inv), ds_grid_height(global.player_inv), item);
The problem is that I need the x and y position in that grid for the very first item that I'm looking for. Then I can subtract it when an upgrade meets the requirements for my players equipment.

There is a function that seems to do what I'm looking for:

Code:
//word for word directly from the doc's
if ds_grid_value_disk_exists(grid, 5, 5, 5, val)
   {
   xpos = ds_grid_value_disk_x(grid, 5, 5, 5, val);
   ypos = ds_grid_value_disk_y(grid, 5, 5, 5, val);
   }
I tried using it (modified to fit my game of course) but I'm not sure if I'm using it correctly.
 
M

MirthCastle

Guest
I don't have access to the help file right now. But I don't think you need to use the disc version of that function...

I think the basic one for value exists, and DS grid get value X or Y.

Another way to would be to use a nested for-loop. You would just scan the grid, and when it finds the item you're looking for... You use the 2 for-loop variables like I and J, to do what you need with it (that's all those functions are doing anyway).

I'm on my phone right now but I can write up an example when I get home if you dont know how.
 

PlayerOne

Member
I appreciate that. Although I was trying to access data within a ds_grid and not on the disc. After a few hours of tinkering this is what I needed:

Code:
if ds_grid_value_exists(global.player_inv, 0, 0, ds_grid_width(global.player_inv), ds_grid_height(global.player_inv), item); // <<< Does the item exist?
{
xpos = ds_grid_value_disk_x(grid, 5, 5, 5, val); // <<< Write [X,0]
ypos = ds_grid_value_disk_y(grid, 5, 5, 5, val); // <<<  write [0,Y]
}
The answer was staring at me in the face I didn't notice the answer.

My goal is to check if there is anything in [0,X], [1,X], etc. It was tricky especially taking into account other factors.
 
M

MirthCastle

Guest
I was referring to:
  1. ds_grid_value_x
  2. ds_grid_value_y
you don't need the disk_x and disk_y - though it uses less parameters and seems easier then ds_grid_value_x.

the for loop would\could look like this in a script:
itemtofind = argument[0];
var griditem
for (var i = 0; i < ds_grid_width(global.player_inv), i++ ) {
for (var j = 0; j < ds_grid_height(global.player_inv), j++ ) {
griditem = global.player_inv[# i , j]

if ( griditem = itemtofind ) {
//do stuff with item at that spot​
break;
}​
}​
}

in the above - the i and j are the same as xpos and ypos.

I believe this is far more efficient because you aren't doing three separate full grid lookups for the one item like you are in the grid functions.
 
Top