• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

(SOLVED) Changing values in all positions in ds_list

Smenkare

Member
for(i=-1; i < exampleListSize; i++)
{ds_list_replace(exampleList, i, 0)}

Will it work? And is there faster way? I have like 500 positions in ds_list and i want to be able to change them all to 0.
 

NightFrost

Member
Yes, it will work, but you should start your loop from zero as list has no negative indices. You could also use ds_list_set(), makes no technical difference in this case. And you also could write it with the list accessor:
Code:
for(i = 0; i <exampleListSize; i++){
    exampleList[| i] = 0;
}
Again makes no difference, a matter of preference which to use. Of course you could just simply empty the list, but I assume you need 500 set positions on it at all given times, so that's not an option.
 

FrostyCat

Member
You're starting one too early. In a for loop, the initialization (first part) comes before the increment (third part).
Code:
for (var i = 0; i < exampleListSize; ++i) {
  exampleList[| i] = 0;
}
This is by far the most straight-forward way to do it generally for a list.

You could try having another list with 500 0's and then just ds_list_copy() from that, or implementing a sparse array with unset entries treated as 0. But at your scale, I would recommend that you focus on being correct first, and being fast only after every other base is covered. Processing 500 entries won't be your project's unmaking, but doing it incorrectly could.

So many rookies these days want to be fast so ardently, they end up more wrong than they're fast. That just makes them look stupid faster. Don't be one of them.

Edit: Another issue is that you are declaring i in instance scope, instead of local scope like what I did (notice the additional var). It might not immediately matter in your current use case, but there are common use cases where having iterating variables in instance scope is detrimental. Again, don't look at this from a performance lens, look at it from a correctness lens. There's always time for you to adjust once you're doing the right thing instead of the fast thing.
 
Last edited:
Top