if(mouse_check_button(mb_left) && position_meeting(mouse_x,mouse_y,_Object))
{
gridname[# 1,1] += 10;
}
list_name[| 0] = 1; // Set position 0 in a list to 1, using the | accessor (shift+backslash)
map_name[? "my_key"] = 0; // Set the "my_key" map entry to 0 with ? accessor
my_array[@ 1] = 10; /* This directly sets position 1 in my_array to 10, you don't necessarily need this unless you have done something like provide an array as an argument in a script and then want to change the original array without having to return it from the script. */
I knew about the accessors, but didn't have much luck with them in the past.It's a way, that's for sure. Personally, I'd do this:
# is what's called an accessor. It allows you to directly access the grid value at the position you provide immediately after (1,1). You can both read and write using it. There's a multitude of other accessors, here's a little rundown:Code:if(mouse_check_button(mb_left) && position_meeting(mouse_x,mouse_y,_Object)) { gridname[# 1,1] += 10; }
Further reading: https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/accessors.htmlCode:list_name[| 0] = 1; // Set position 0 in a list to 1, using the | accessor (shift+backslash) map_name[? "my_key"] = 0; // Set the "my_key" map entry to 0 with ? accessor my_array[@ 1] = 10; /* This directly sets position 1 in my_array to 10, you don't necessarily need this unless you have done something like provide an array as an argument in a script and then want to change the original array without having to return it from the script. */
The functionality of an accessor and the function calls (ds_grid_get/set) is exactly the same. In fact, the only time I ever use the built-in functions for lists is when I'm adding an item to a list and haven't bothered to keep track of the list size. Any other time I'm going to be using accessors and if I'm using something other than a list, I'm pretty sure I always use the accessor version. It's way quicker to type, it reads easier (the thing you are accessing is the very first thing you start reading, instead of reading through "ds_grid_set" or "ds_map_add" or whatever the function name is), it lets you do more with less (hence why I could cut your three line statement down to one line), just all around better, there is no downsides to using accessors that aren't present in the function calls themselves.
I tried the accessors again and found that you cant assign a ds_grid accessor to a instance variable.The functionality of an accessor and the function calls (ds_grid_get/set) is exactly the same. In fact, the only time I ever use the built-in functions for lists is when I'm adding an item to a list and haven't bothered to keep track of the list size. Any other time I'm going to be using accessors and if I'm using something other than a list, I'm pretty sure I always use the accessor version. It's way quicker to type, it reads easier (the thing you are accessing is the very first thing you start reading, instead of reading through "ds_grid_set" or "ds_map_add" or whatever the function name is), it lets you do more with less (hence why I could cut your three line statement down to one line), just all around better, there is no downsides to using accessors that aren't present in the function calls themselves.
my_workaround_x = 1;
my_workaround_y = 0;
my_variable = my_grid[# my_workaround_x, my_workaround_y];
Thank You, i'll give this a try.Yes, you can't assign something like my_grid[# 1,0] to a variable (i.e. my_variable = my_grid[# 1,0] only contains my_grid[# 1,0] in the frame that it is called).
A possible workaround is something like this:
Create Event:
Step Event:Code:my_workaround_x = 1; my_workaround_y = 0;
Now my_variable is always equal to my_grid[# 1,0] but if you want to check another x position in the grid, just reassign my_workaround_x to that x and my_variable will equal that, obviously the same is true for my_workaround_y.Code:my_variable = my_grid[# my_workaround_x, my_workaround_y];
I couldn't get it to work. Atleast for what I was trying to do.Yes, you can't assign something like my_grid[# 1,0] to a variable (i.e. my_variable = my_grid[# 1,0] only contains my_grid[# 1,0] in the frame that it is called).
A possible workaround is something like this:
Create Event:
Step Event:Code:my_workaround_x = 1; my_workaround_y = 0;
Now my_variable is always equal to my_grid[# 1,0] but if you want to check another x position in the grid, just reassign my_workaround_x to that x and my_variable will equal that, obviously the same is true for my_workaround_y.Code:my_variable = my_grid[# my_workaround_x, my_workaround_y];
NOTE: This is true for both the function calls and the accessors, so don't think of this as a weakness in accessors. Rather a particular way that GML works.
// Creat event
_Addto = ds_grid[# 1,1];
// step event
if(something)
{
_Addto += 10; // adding this value to the already existing value of ds_grid[# 1,1]
};
_Addto = ds_grid[# 1,1];
if(something)
{
_Addto += 10; // adding this value to the already existing value of ds_grid[# 1,1]
};
_Addto = ds_grid[# 1,1];
if(something)
{
ds_grid[# 1, 1] += 10; // adding this value to the already existing value of ds_grid[# 1,1]
}