GMS 2.3+ For Loop not working

demonipo

Member
Just updated to the most recent version (2.3.1) and my For loops have suddenly stopped working and I don't know why. I found it odd that the new one I did wasn't working, even though I had one running perfectly fine last week or two, but even that one is broken right now.

Checked the usual suspects of i being out of range or the code not being processed but it's all working, it just skips as if it was nothing.

Code:
GML:
for (var i = array_length(decorativeList); i < 0; i--)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
Does anyone understand what's wrong?
 

Simon Gust

Member
I'm suprised this even ran in the first place.
Maybe you meant to write
Code:
for (var i = array_length(decorativeList) - 1; i >= 0; i--)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
I personally stick with
Code:
for (var i = 0; i < array_length(decorativeList); i++)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
It's much more common.
 

TheouAegis

Member
I'm suprised this even ran in the first place.
Maybe you meant to write
Code:
for (var i = array_length(decorativeList) - 1; i >= 0; i--)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
I personally stick with
Code:
for (var i = 0; i < array_length(decorativeList); i++)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
It's much more common.
The larger the array, the longer that second code takes to run, because the middle condition gets handled on each loop, meaning you call array_length() i times. Not a big deal in most cases, but that's why that style is dropping off in popularity.
for(var lim=array_length(arr), i=0; i<lim; i++) speeds things up a tad and is the proper way to loop up if the limit is based on a subroutine.

I think the count-down loop just carries over from array initialization habits. Once the array has been initialized, though, you can loop whichever diirection is more comfortable for you. I've seen both directions used frequently in old game code, but that could be attributed to --i tripping status flags when it reaches 0 and again when it reaches -1, so no comparison checks were needed. Different times, different practices.
🤠

But yeah, the error is the alligator is trying to eat the wrong value.
 
Last edited:

demonipo

Member
I'm suprised this even ran in the first place.
Maybe you meant to write
Code:
for (var i = array_length(decorativeList) - 1; i >= 0; i--)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
I personally stick with
Code:
for (var i = 0; i < array_length(decorativeList); i++)
{
    instance_destroy(decorativeList[i])
    decorativeList[i] = noone
}
It's much more common.
I'm doing it in reverse. It may be cause of Unity, but I had to do it in reverse so there would be assets that weren't deleted. It's more of a safe check in case the array is reduced when i destroy the instance

And yeah, I should do the -1. I don't know why it doesn't crash though, but that's why I never corrected that. It was also fear that it didn't cover the entire array, even though it does.


variable "i" must be greater than 0...
it doesn't? I've made loops like this in the past and always worked since I'm not adding, but going backwards. Again, if it worked before then what you pointed out can't be the problem, or else I wouldn't have done this thread in the first place
 

Fanatrick

Member
for (var i = array_length(decorativeList); i < 0; i--)
i is set to array_size (never negative), your loop iterates if i is lower than 0, which is never.
 

demonipo

Member
for (var i = array_length(decorativeList); i < 0; i--)
i is set to array_size (never negative), your loop iterates if i is lower than 0, which is never.
You're right I didn't notice that lmao thanks. I was super annoyed at that point (for other reasons than code) so I didn't understand that it was flipped on the condition
 
Top