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
yeAs in, loop through all the cells in the grid and draw each one in a relative position based on the indices?
Cool. Just remove the question mark from the end of my reply and that's your answer.
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]));
}
}
ds_grid_get(grid_name, i, a)
grid_name[# i,a]
What do the variables i and a mean?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])); } }
is the same asGML:ds_grid_get(grid_name, i, a)
It is just whichever notation you prefer!GML:grid_name[# i,a]
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]));
}
}
Ok, thanks so much! this should work a lot more smoothly than what i've been doing.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:
In many many many cases in gamedev you can use local variables, make a habit of them!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])); } }