GMS 2 ds_grid not showing values set in create event

D

Daniel Cáceres

Guest
I've filled a ds_grid in the create event but when I iterate over it in the draw event (same occurs in the step event) it shows the correct value only for the top left cell. All other cells show the value stored in the top down cell.
I've stored an array in each cell of the grid, acccording to yoyo doc it should a number or a string, but I saw a tutorial about tile collisions that used an array, and it is working fine in the create event. Any difference between myGrid [# _gx, _gy] = _spr_coords; and ds_grid_set(myGrid, _gx, _gy, _spr_coords);

code in create event:

enum E_COORDS {X=0, Y=1};

var _gw, _gh, _rw, _rh, _x, _y, _spr_coords, _spr_coords_from_grid;
_gw = 3; // grid width
_gh = 5; // grid height
_rw = room_width;
_rh = room_height;
_x = _rw div _gw; // x coord
_y = _rh div _gh; // y coord
_spr_coords = [0, 0]; // sprite coords
_spr_coords_from_grid = [0, 0]; // sprite coords from grid (previously set)

myGrid = ds_grid_create(_gw, _gh);

for (var _gx = 0; _gx < _gw; _gx++) {
for (var _gy = 0; _gy < _gh; _gy++) {
// store coords in grid
_spr_coords[E_COORDS.X] = irandom_range(_x * _gx + (1/3 * _x), _x * _gx + (2/3 * _x));
_spr_coords[E_COORDS.Y] = irandom_range(_y * _gy + (1/3 * _y), _y * _gy + (2/3 * _y));
//myGrid [# _gx, _gy] = _spr_coords;
ds_grid_set(myGrid, _gx, _gy, _spr_coords); // any difference with previous commented line ?
// show coords in grid
_spr_coords_from_grid = myGrid [# _gx, _gy];
show_debug_message("coords in create: gx="+string(_gx)+" gy="+string(_gy)+
" coordX="+string(_spr_coords_from_grid[E_COORDS.X])+
" coordY="+string(_spr_coords_from_grid[E_COORDS.Y]));
}
}

code in draw event:

var _spr_coords = [0, 0]; // sprite coords
var _gw=ds_grid_width(myGrid);
var _gh=ds_grid_height(myGrid);
for (var _gx = 0; _gx < _gw; _gx++) {
for (var _gy = 0; _gy < _gh; _gy++) {
_spr_coords = myGrid [# _gx, _gy];
show_debug_message("ccords in draw x="+string(_gx)+" y="+string(_gy)+
" coordX="+string(_spr_coords[E_COORDS.X])+
" coordY="+string(_spr_coords[E_COORDS.Y]));
draw_sprite(spr_mySprite, 0, _spr_coords[E_COORDS.X], _spr_coords[E_COORDS.Y]);
}
}
output:
coords in create: gx=0 gy=0 coordX=44 coordY=50
coords in create: gx=0 gy=1 coordX=60 coordY=163
coords in create: gx=0 gy=2 coordX=52 coordY=263
coords in create: gx=0 gy=3 coordX=51 coordY=364
coords in create: gx=0 gy=4 coordX=52 coordY=458
coords in create: gx=1 gy=0 coordX=140 coordY=65
coords in create: gx=1 gy=1 coordX=147 coordY=140
coords in create: gx=1 gy=2 coordX=156 coordY=235
coords in create: gx=1 gy=3 coordX=140 coordY=361
coords in create: gx=1 gy=4 coordX=138 coordY=450
coords in create: gx=2 gy=0 coordX=249 coordY=64
coords in create: gx=2 gy=1 coordX=265 coordY=141
coords in create: gx=2 gy=2 coordX=264 coordY=258
coords in create: gx=2 gy=3 coordX=254 coordY=354
coords in create: gx=2 gy=4 coordX=260 coordY=458

ccords in draw x=0 y=0 coordX=44 coordY=50
ccords in draw x=0 y=1 coordX=260 coordY=458
ccords in draw x=0 y=2 coordX=260 coordY=458
ccords in draw x=0 y=3 coordX=260 coordY=458
ccords in draw x=0 y=4 coordX=260 coordY=458
ccords in draw x=1 y=0 coordX=260 coordY=458
ccords in draw x=1 y=1 coordX=260 coordY=458
ccords in draw x=1 y=2 coordX=260 coordY=458
ccords in draw x=1 y=3 coordX=260 coordY=458
ccords in draw x=1 y=4 coordX=260 coordY=458
ccords in draw x=2 y=0 coordX=260 coordY=458
ccords in draw x=2 y=1 coordX=260 coordY=458
ccords in draw x=2 y=2 coordX=260 coordY=458
ccords in draw x=2 y=3 coordX=260 coordY=458
ccords in draw x=2 y=4 coordX=260 coordY=458
 

Simon Gust

Member
Your whole ds grid is filled with the exact same array over and over.
You need an individual array for every entry of the grid.
You can do this by putting the
Code:
var _spr_coords = [0, 0];
inside the for loops on the line right before you set it to irandom_range(_x, etc...).
 
D

Daniel Cáceres

Guest
Solved!. Thank you very much!.
Two more questions:
  • Is there any difference between myGrid [# _gx, _gy] = _spr_coords; and ds_grid_set(myGrid, _gx, _gy, _spr_coords); ?
  • How can I include "Code sections" in muy posts like you did. I mean that boxes in the posts to separate text from code
 
Top