Getting value from DS_GRID at mouse point

I

Ieyfo

Guest
I have spent a while trying to figure this out also have talked with people on the game maker discord and no solution. The issue I'm having is I can't return the x,y of the grid from the place I clicked with my mouse. I think you could do it with some complex math but I can't wrap my head around it. Also what would the best way to add to the grids x,y clicked on by the mouse?
 
D

Deanaro

Guest
Not complex at all!!!
Gridx = floor(mouse_x/grid_width);
Gridy= floor (mouse_y/grid_height);

That wasnt so hard was it :p

EDIT:
To translate those coordinates into a list position for ds_grid do this:
GridN = (room _width/grid_width) * (Gridy) + Gridx;

DONE EDITTING!!
 
E

Erik

Guest
If you have a ds_grid that has the same width as the number of tiles that your
room is wide and the same height as the number of tiles the room is tall then that
should be easy to accomplish.

So your grid would probably look something like this
(i assume that your grid/tiles are 16*16 pixels but you might use another size of your room grid)
In this example i store the ds_grid in a global called global.grid but you can use any name you want.
Code:
global.grid=(ds_grid_create((room_width div 16),(room_height div 16));
You would then GET the value of a space in the ds_grid based the the room tile that you left click by
doing something similar to this. I am storing the value returned from the ds_grid in a variable called grid_content
that you can then use for whatever you want.
Code:
if (mouse_button == mb_left) {
        grid_content=(ds_grid_get(global.grid((mouse_x div 16),(mouse_y div 16));
}
If you would like to set a space in the ds_grid with the mouse based on what tile you right click
you would use something similar to this. Lets say that you want to set the space in the ds_grid to 2.
Code:
if (mouse_button == mb_right) {
        grid_content=(ds_grid_set(global.grid((mouse_x div 16),(mouse_y div 16),2);
}
Now you have a ds_grid that is the same size as the room is in tiles (16*16). You can now left click
on tiles in the room to fetch the value from the ds_grid and right click on tiles in the room to
set the corresponding ds_grid space to 2.

I hope this helps. :)
 
Top