Legacy GM Ds_grids

L

lord_berto

Guest
Hello friends :) back again to ask :

How could I re-organize a DS_GRID so that basically all the values BUT those with 0 are Shifted.
Basically imagine an inventory. If I drop something in slot 3..
[Lotion] [Tissue] [NOTHING] [Pineapple]
How can the [Pineapple] shift over to the spot that equals "0" (nothing)?

Any help is much appreciated :)
 

TheouAegis

Member
Whenever you make any changes to the inventory, loop through the grid (start with 1, not 0) and check if the slot before it (that's why you need to start with 1) is empty. If it is, copy the data to the previous cell and clear the current cell. This will have a cascading effect, so if you wanted, you could even have this occur over multiple steps and the player would see his inventory shifting into place.
 
N

nonodev

Guest
I hope this work.
Code:
//Where SLOT_ID: Is the 'y' value of the grid that stores the item id information.
//That work just if your inventory used 'x' for the slots and 'y' for the information of that slot.
var _grid = your_grid;
var _gridWidth = ds_grid_width(_grid);
var _gridHeight = ds_grid_height(_grid);
var _exists = ds_grid_value_exists(_grid,0,SLOT_ID,_gridWidth-1,SLOT_ID,0);
if (_exists) {
for (var i=0;i<_gridWidth ;i++) {
var value = _grid[# i,SLOT_ID];
if (_value==0) {
ds_grid_copy(_grid,_grid,i+1,0,_gridWidth-1,_gridHeight-1,i,0);
ds_grid_set_region(_grid,_gridWidth-1,0,_gridWidth-1,_gridHeight-1,0);
}
}
}
}
 
Top