Legacy GM [SOLVED] ds_grid Position Detection Help

S

Storm1208

Guest
Hey! This is my first post on the forum so I'll try to be as clear as possible!

I'm creating a digital version of the board game "battleship" for a coding class at my high school; however, I've run into some issues trying to get my grid to detect when an object is within a specific square. I tried to create a ds_grid array system for a 10x10 grid and initialize all of its values to 0 as seen below:
Code:
var i = 9;
var j = 9;
grid = ds_grid_create(i,j);

repeat(81)
    {
    grid[i, j] = 0;
    j--;
    if(j == 0 && i != 0)
    {
    j = 9;
    i--;
    }
}
I then created the 10x10 grid visually in a "draw" event (each square is 64x64 pixels). Now that I have both a visual representation of the grid and an array, I can't figure out how to program my code to detect when an instance placed within a specific square of the visual grid will change the correlating ds_grid value to "1". I tried looking at other posts but couldn't find anything. I appreciate any help anyone can give me!
 
T

TimothyAllen

Guest
Not to nitpick, but that repeat loop is ugly (for loop is more appropriate here). Also I think that ds_grid cells will default to a value of 0, but if you want to be sure, use ds_grid_clear(id, value).
Code:
ds_grid_clear(grid, 0);
As for converting room position to grid position, you would do something like this:
Code:
var cell_width = 32;
var cell_height = 32;
var grid_x = x div cell_width;
var grid_y = y div cell_height;
If your visual representation is offset from (0, 0) then you will need to also consider that offset. (same for if your grid cells are spaced apart)
 
S

Storm1208

Guest
Not to nitpick, but that repeat loop is ugly (for loop is more appropriate here). Also I think that ds_grid cells will default to a value of 0, but if you want to be sure, use ds_grid_clear(id, value).
Code:
ds_grid_clear(grid, 0);
As for converting room position to grid position, you would do something like this:
Code:
var cell_width = 32;
var cell_height = 32;
var grid_x = x div cell_width;
var grid_y = y div cell_height;
If your visual representation is offset from (0, 0) then you will need to also consider that offset. (same for if your grid cells are spaced apart)
Woah, thanks! Sorry about the sloppy repeat loop. I've only been coding in GameMaker for about a month and I'm still getting the hang of things.

I'll likely remove the loop and implement the ds_grid_clear code snippet into the program instead.

Also, thanks for that grid position tip! I'm not by my laptop right now but I'll try to implement it as soon as I get the chance and see if it works. Quick question though, would the cell width and cell height variables be 64 instead of 32 since my grid has squares that are 64x64?
 
T

TimothyAllen

Guest
The cell_wdith and cell_height is totally dependent on your visual representation of the grid.
 
S

Storm1208

Guest
The cell_wdith and cell_height is totally dependent on your visual representation of the grid.
So I ended up implementing your grid detection method and it worked perfectly! The positions within the array now correlate directly with their counterparts in the visually created grid and I couldn't be happier!

I unfortunately ran into another issue where I was unable to use the "draw_text" function to print the specific value of a square in my array in order to check it (I had used the code "draw_text(32 , 60, string(ds_grid_get(grid,3,1));" and was receiving a weird error message that prevented the program from functioning properly):

ds_grid_get argument 1 incorrect type (array) expecting a Number (YYGI32)

I checked multiple forums and it seems other people had the same issue (and couldn't solve it :(). Regardless, I'm grateful for your help! I'm going to go ahead and add [SOLVED] to the title of the thread since you helped me fix my initial issue!
 

Simon Gust

Member
I am kinda confused about if you're using ds_grids OR arrays OR both, because the error message tells you that you try to access your ds_grid like an array. Those 2 are of course not the same thing.

I noticed your use of brackets [] to access the array / grid.
There is a slight difference.
When using ds_grids you write
Code:
grid[# i, j]; // this is the shorcut version
// or
ds_grid_get(grid, i, j);

When using arrays you use
Code:
array[i, j];
// or in special cases
array[@ i, j]
 
S

Storm1208

Guest
I am kinda confused about if you're using ds_grids OR arrays OR both, because the error message tells you that you try to access your ds_grid like an array. Those 2 are of course not the same thing.

I noticed your use of brackets [] to access the array / grid.
There is a slight difference.
When using ds_grids you write
Code:
grid[# i, j]; // this is the shorcut version
// or
ds_grid_get(grid, i, j);

When using arrays you use
Code:
array[i, j];
// or in special cases
array[@ i, j]
So, uh, funny story. I checked the step event of my grid object and I accidentally referred to my ds_list using a standard "grid[ x , y ]" call instead of "ds_grid_set()" (just like you said). I just changed it and it runs perfectly, lol. Thanks for the input!
 
Top