GMS 2 Grid Loop Help..

R

Reaxgrim

Guest
Trying to get this code to loop through my inventory grid and check if the grid slot is "noone" and if it is "noone" add in an item and then stop.


but it seems to add the item to every other grid slot. ie: 1,0 - 2,0 - 3,0 etc
instead of just adding it to the first "noone" it comes across and then stops.

Below is the code

Code:
/// @description Player Inventory Add Script


for (var xp = 0; xp < ds_grid_width(argument0); xp++)
{
  
    for (var yp = 0; yp < ds_grid_height(argument0); yp++ )
    {

        if (argument0[# xp,yp] != noone){
          
            show_debug_message(string(xp) + string(yp) + string(" NOT EMPTY"));
        
            
        } else {
        
            show_debug_message(string(xp) + string(yp) + string(" EMPTY"));
            ds_grid_add(argument0,xp,yp,argument1);
            show_debug_message("Added Item");;
            
            break;
        }
 
    }
}
and debug code-
-4 is NOONE (ie: empty)
-1 is where its added the item.

Code:
00-1
01-4
02-4
03-4
10-1
11-4
12-4
13-4
20-1
21-4
22-4
23-4
30-1
31-4
32-4
33-4

but it should only add the code to 00 then 01 then 02 or whatever ones it comes to thats -4 (empty)

any help would be great :)
cheers
Reax.
 
D

dj_midknight

Guest
Seems like when you break out of the inner loop the outer loop continues and keeps the flow going.

Might want to try adding a boolean value as part of your condition.

Code:
var found = false;

for (var xp = 0; xp < ds_grid_width(argument0) && !found; xp++)
{
 
    for (var yp = 0; yp < ds_grid_height(argument0)  && !found; yp++ )
    {

        if (argument0[# xp,yp] != noone){
         
            show_debug_message(string(xp) + string(yp) + string(" NOT EMPTY"));
       
           
        } else {
       
            show_debug_message(string(xp) + string(yp) + string(" EMPTY"));
            ds_grid_add(argument0,xp,yp,argument1);
            show_debug_message("Added Item");;
            
            found = true;

            break;
        }

    }
}
 
R

Reaxgrim

Guest
thanks for that. sorted now :)

its always something simple heh
 
Top