• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [Help] ds_list_find_index - compare array?

bbbower

Member
I've simplified my code example. Basically without using a for loop... I was trying to use ds_list_find_index to determine if a pair of coordinates were already inside the ds_list so that duplicates did not get added.

Gamemaker says that values inside of a ds_list can be an array. However it is unable to check them even if they are already in the list ds_list_find_index still returns -1 and duplicates continue to be added.
Is this a bug or am I overlooking something here?

My conclusion is that you cannot directly check array values using this function but again.. the function help states the value checking can be an array.

Example : coords 1,1 get added to list, ds_list_find_index checks for coords[1,1] , its in the list already, but -1 is returned anyway and then 1,1 gets added a second time.

Code:
var coords;
coords[0] = ex; // The x value of the item being added for removal
coords[1] = ey; // The y value of the item being added for removal

if (ds_list_find_index(list_remove,coords) > -1) {
    show_debug_message("Duplicate Remove @ " + string(coords));
    exit;
}
ds_list_add(list_remove,coords);
 
H

hunijkah

Guest
I'm completely guessing on how game make works with this but this is my thoughts:

Arrays being passed as parameters would be too memory intesive because it would need to copy the entire array every time the variable was passed into a function. Time consuming and inefficient.

Instead, it probably passes them as pointers to the spot in the computer memory that the information was stored.

If this were the case, you would be comparing the space in memory that the array was stored originally compared to the space in memory you stored the local variable "coords".

These would come out as not the same therefore returning a -1 to you.

Fix it by comparing the internal values of them using a for loop.
 
Last edited by a moderator:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
I'm completely guessing on how game make works with this but this is my thoughts:

Arrays being passed as parameters would be too memory intesive because it would need to copy the entire array every time the variable was passed into a function. Time consuming and inefficient.

Instead, it probably passes them as pointers to the spot in the computer memory that the information was stored.

If this were the case, you would be comparing the space in memory that the array was stored originally compared to the space in memory you stored the local variable "coords".

These would come out as not the same therefore returning a -1 to you.

Fix it by comparing the internal values of them using a for loop.
This isn't wrong - not necessarily pointers, but certainly references. So doing ds_list_find_index would look up if there's that specific array, not array with same values.
A loop with array_equals checks would help.
 

bbbower

Member
This isn't wrong - not necessarily pointers, but certainly references. So doing ds_list_find_index would look up if there's that specific array, not array with same values.
A loop with array_equals checks would help.
That is what I ended up doing. I was trying to steer clear of the loop. However I realize the loop is trivial considering I'm doing a match 3 game that doesn't require a lot of power to run as I only check for matches after items settle not on a per step basis like I would assume many people might start out. Most of my roadblocks are just me not wanting to take an inefficient cut, however this seems to be the only way.

edit: It definitely only compares the memory address not the contents when checking array data. It makes it impossible to use efficiently but the loop through the list works fine. The list only ends up being 3 to about 15 in size so it isn't much of a problem I was just trying to be overly efficient as to be able to run on a toaster lol.

Code:
for (var i = 0; i < ds_list_size(list_remove); i++;) {
    coords_check = ds_list_find_value(list_remove, i);
    if (array_equals(coords,coords_check)) {
        show_debug_message("Duplicate blocked @ " + string(coords));
        exit;
    }
}
 
Last edited:
Top