Remove variable by index don't remove

I have an array declared as a scene variable, in the middle of the code, every time I press a button a series of commands is executed, among them I want that every time the button is pressed, the 0 position of the array is eliminated.

For example, if I have the array [23,45,12] when pressing the button once it would stay in [45,12]

To do this I use the function Remove variable by index, to delete the 0 position and it does it correctly.

The problem is when only one value remains in the array, [12], when executing the function sometimes it does it correctly and converts the array to empty, and other times what it does is convert that last value to [0], but does not delete it.

Any idea why this behavior can happen? Am I using the right function?

Can you provide a screen shot of the events?

ok I found the reason, but still dont know why…

The problem is this second line, That serves to assign the value poolLevels[0] to a variable. It does it correctly but when the array is empty, what it does is create a new value to read it, thats why never remove this last item, because “changue de variable” creates again with zero value. is it a mistake or am I going crazy?

image

(I have put this second line here followed to facilitate reading, but it was very much within an external event code…)

Yes, if you reference an array that’s empty, GDevelop will return 0 and I think it’ll add that value to the array.

A way around this is to have a condition that checks the number of array children of poolLevels is more than 0 before accessing poolLevels[0].

1 Like

OMG, hours spent on this XD. I could imagine that it returned error or 0, but not that it added anything to the array. Is this a normal and plausible behavior?

Now that I know, I can do the check before.

It’s a normal behaviour of GDevelop. Previous versions of GDevelop had it so if a variable was referenced and didn’t exist, it would be created with a default value of 0.

Yes, I can understand that if a variable does not exist, create it on the fly and assign it a default value.

But in this case the array is already declared, only it is empty.

I find it strange, but anyway, yes it is, it is so.

Yes, the array is declared and exists, but the first element doesn’t exist. And it’s that what gets created.