• 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!

GML Visual How can I draw a ds_grid

B

bebesnuggles

Guest
My brain is fried, and I cannot think of how to draw a ds_grid. Please give a brother a hand Thanks
 

obscene

Member
Use a for loop. Multiply the x,y grid position of each value by however wide/tall you want your cells to be.
 

Neptune

Member
GML:
for (var a = 0; a < ds_grid_height(grid_name); a++)
{
    for (var i = 0; i < ds_grid_width(grid_name); i++)
    {
        draw_text(16*i,16*a,string(grid_name[# i,a])); 
    }
}
GML:
ds_grid_get(grid_name, i, a)
is the same as
GML:
grid_name[# i,a]
It is just whichever notation you prefer!
 
Last edited:
B

bebesnuggles

Guest
GML:
for (var a = 0; a < ds_grid_height(grid_name); a++)
{
    for (var i = 0; i < ds_grid_width(grid_name); i++)
    {
        draw_text(16*i,16*a,string(grid_name[# i,a]));
    }
}
GML:
ds_grid_get(grid_name, i, a)
is the same as
GML:
grid_name[# i,a]
It is just whichever notation you prefer!
What do the variables i and a mean?
thank
 

Neptune

Member
They're local variables (which you can define by using the word 'var' before a declaration). A local variable is different from an 'instance' variable (the kind you would put in a Create Event), because it is only accessible in that event, and is thrown out, after all the lines of code in that event ran (every step of your game)... So it is used, and thrown out 60 times per second in most cases.

In a 'for' loop, in this case two loops that are nested, the loop always requires this format:
1. Declaration
2. Boundary Rule
3. Increment / Decrement

If the declaration variable, doesnt meet the requirement of the boundary rule, the loop breaks. If the boundary rule IS met, the increment / decrement portion is run. In our case adding 1 to 'a' and 'i'.

There is NOTHING special about the names 'i' and 'a'. You'll find in most physics / engineering they will use i , j , k to represent the x , y , z vector axis... I personally don't use i & j because they look so similar 😅
Long story short, you could name them anything:
GML:
for (var my_vertical_loop = 0; my_vertical_loop < ds_grid_height(grid_name); my_vertical_loop++)
{
    for (var my_horizontal_loop = 0; my_horizontal_loop < ds_grid_width(grid_name); my_horizontal_loop++)
    {
        draw_text(16*my_horizontal_loop,16*my_vertical_loop,string(grid_name[# my_horizontal_loop,my_vertical_loop]));
    }
}
In many many many cases in gamedev you can use local variables, make a habit of them! :)
 
Last edited:
B

bebesnuggles

Guest
They're local variables (which you can define by using the word 'var' before a declaration). A local variable is different from an 'instance' variable (the kind you would put in a Create Event), because it is only accessible in that event, and is thrown out, after all the lines of code in that event ran (every step of your game)... So it is used, and thrown out 60 times per second in most cases.

In a 'for' loop, in this case two loops that are nested, the loop always requires this format:
1. Declaration
2. Boundary Rule
3. Increment / Decrement

If the declaration variable, doesnt meet the requirement of the boundary rule, the loop breaks. If the boundary rule IS met, the increment / decrement portion is run. In our case adding 1 to 'a' and 'i'.

There is NOTHING special about the names 'i' and 'a'. You'll find in most physics / engineering they will use i , j , k to represent the x , y , z vector axis... I personally don't use i & j because they look so similar 😅
Long story short, you could name them anything:
GML:
for (var my_vertical_loop = 0; my_vertical_loop < ds_grid_height(grid_name); my_vertical_loop++)
{
    for (var my_horizontal_loop = 0; my_horizontal_loop < ds_grid_width(grid_name); my_horizontal_loop++)
    {
        draw_text(16*my_horizontal_loop,16*my_vertical_loop,string(grid_name[# my_horizontal_loop,my_vertical_loop]));
    }
}
In many many many cases in gamedev you can use local variables, make a habit of them! :)
Ok, thanks so much! this should work a lot more smoothly than what i've been doing.
 

Neptune

Member
No problem! Yeah that nested 'for' loop format is great in a lot of scenarios, if your project is at all grid or tile based... Looping across tile maps, data structures, spawning particles, checking every tile in a room etc etc 👍
 
I had a lot of trouble with DS structure when I first started using them. They are not difficult to use when you know how to access them. However, I still don't understand why they are a thing with the exception of 'how easy they are to manipulate' opposed to arrays. With arrays, you need to code extra scripts. For example, you can shuffle a ds_grid (Excellent for a card game).

I have come to accept this is the way GMS works. It can become confusing when you want to master your own universe, but it does save alot of time.
 
Top