GML Feeding variable names into script

S

Samus

Guest
Hello, so I'm working on a cool new game with online networking. I use DS_Grids to keep track of where the players and other objects in the game are and what their status is, things like that. So when it comes to simulating collisions between the different objects, I would loop through the DS_Grid for that kind of object and for the other kind of object involved, and plug whatever those values are into
Code:
point_in_circle(yadayadayada)
or something like that to find out whether they have/will collide or not. That works great, but when there are a lot of different objects in the game and I'm checking for collisions with all of them, its going to get really cluttered. So I decided to make a script I could use over and over again for the various ds_grids involved:
Code:
///server_collision(grid1, grid2, xpos1, ypos1, xpos2, ypos2, i)
var grid1, grid2, xpos1, ypos1, xpos2, ypos2, i;
grid1 = argument[0];
grid2 = argument[1];
xpos1 = argument[2];
ypos1 = argument[3];
xpos2 = argument[4];
ypos2 = argument[5];
i = argument[6];
//Go through every item in the grid and check for collisions.
for(var j = 0; j < ds_grid_height(grid2); j ++;)
{
    if(ds_grid_height(grid1) > i)
    {
        if(ds_grid_get(grid2, xpos2, i) != ds_grid_get(grid1, xpos1, j))
        {
            if(point_in_circle(ds_grid_get(grid2, xpos2, i), ds_grid_get(grid2, ypos2, i), ds_grid_get(grid1, xpos1, j), ds_grid_get(grid1, ypos1, j), 16))
            {
                if(ds_grid_height(grid2) > 1) ds_grid_delete_row(grid2, i);
                else
                {
                    ds_grid_destroy(grid2)
                    (grid2) = ds_grid_create(6, 0);
                }
                ds_grid_add(grid1, 8, j, -2);
            }
        }
    }
}
The idea is that I can input the names of the two different grids and it will modify them. But as it turns out, it actually copies the grid into the variable I have inside the script, modifies that one, and leaves the original untouched. I have a workaround in mind, but its so complex I might as well not have the script. I figure there is a special function/command meant just for this that I just don't know about. Thanks to anyone who replies!
 
This doesn't add up for me. When you create a grid using ds_grid_create(), the value you get back is merely an index to the array. The index is simply an integer. So if you are passing that index to your script, you are passing an integer which is used to refer to the grid when you use the ds_grid functions.

I can't see any way your current script would be creating a copy of the grid and leaving the original untouched, because you're using the exact same index to refer to the same grid.

Ahah! Just re-read your code before hitting Post.

These lines:

Code:
ds_grid_destroy(grid2)
(grid2) = ds_grid_create(6, 0);
You don't need the () around grid2 in the second line, but that's not the real issue.

You are creating a new grid and assigning it to a local script variable (grid2 - as you declared it at the top of the script), this variable will only exist to the end of the script.

It won't modify the original variable that holds the index to the script from wherever you called this script from, I hope that makes sense.

You need a way to return the values of any data structures that you created newly within the script.

If you know you only will ever create the grid2 in this script, you can just return that value at the end of the script.

Then you would call it like this:

Code:
original_grid2 = server_collision(original_grid1, original_grid2,....etc)
At the end of your sever_collision() script, put "return grid2" as the last line.

That way, whether the grid gets destroyed and re-created or not, the original_grid2 variable will hold the correct index.
 
S

Samus

Guest
Thank you, that makes perfect sense! Yeah, I didn't have the parentheses there initially, but I added them later thinking it would help. I thought about using "return" but I got it in my head somehow that I needed to return both grids, so I would have to do other stuff. That was probably because I misunderstood the way ds_grids worked. I will try that, Thanks!
 
S

Samus

Guest
That was it! I also had to fix a human error I did where I mixed up grid1 and grid2. Thank you very much!
 
N

Nordwin

Guest
It seems strange to me that you destroy your grid and instantly recreate it afterwards... There are also resize and clear operations for such situations.. Then you would not even need to return your newly created datastructure. Furthermore if you want to "return" more than one value from within a script you can either return an array or a list containing all return values..
 
S

Samus

Guest
The reason I do that is because I have another script, you may have seen it, called "ds_grid_delete_row." This function finds the row you specify, moves all other rows up a bit, then resizes it. The problem is that you can't resize a DS_Grid to zero, attempting to do so will cause an error. I want to be able to do that. So I check to make sure it isn't only one block high first, and if it is, I delete it, and remake it with a height of zero. I actually tried returning an array at one point, but due to my misunderstanding of DS_Grids, it didn't work right. Now I know I can do that if I need to. But it isn't necessary in this case. :)
 
Top