• 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!

GameMaker [SOLVED] Ds List Collapse?

Neptune

Member
I think I'm just having a brain cramp with some code, but is this proper thinking?
I want to scan through the list. If an entry is == 3, I'd like to delete the index at 'i' and 'i-1', and then continue scanning with what would have been the next entry...

Code:
var list = ds_list_create();
ds_list_add(list,1);
ds_list_add(list,2);
ds_list_add(list,3);
ds_list_add(list,4);

for(var i = 0; i < ds_list_size(list); i++)
{
    if list[| i] == 3
    {
        repeat(2)
        {   
            ds_list_delete(list,i-1);
        }
        i -= 2;   
        if ds_list_empty(list)
        {
            break;
        }   
    }   
}

/*
    At this point, I think the list would contain:
    list[| 0] == 1
    list[| 1] == 4
*/
 

samspade

Member
Better way is to loop through it backwards.

Code:
for (var i = ds_list_size(list) - 1); i >= 0; i--) {
    //delete when you want it won't affect the loop
}
 

Neptune

Member
@samspade Ok, so the problem is I'm sifting through a list of "pairs": [enum] [timer] [enum] [timer]... I'm scanning for when a timer reaches 0, and then removing the timer and the associated enum from the list.
This tends to mess up the loop from whichever way I approach it.

Either that, or my code is fine, and I'm mucking up somewhere else!
 
H

Homunculus

Guest
If you are working with pairs, why not store arrays in the list (holding the two values)? Looks simpler to me and more structured
 

GMWolf

aka fel666
One more elegant (if slightly less efficient) solution is to actually store your elements as pairs.
You can do that with an array of length 2.
So you essentially would have a list of arrays. Entry 0 of the array is the timer, and entry 1 is the enum.


If you want performance however, you cannot beat using 2 lists.
One holds the timers, the other holds the enums.
When you remove an item, remove it from both lists.
This has the advantage that you only have to access the list of timers when you are checking / updating them. Better predicatibility, better memory locality => better performance.
And of course, you don't have to deal with removing pairs of values, just one at a time (from two lists)
 
Top