Array variables declared through events are acting as structure variables instead of arrays

You can play with the index value in the 3rd line. I put 0 because i was thinking that if the bug put a value at index 0 and I check it then it won’t create another value at index 1. But it does.
It’s like anything that goes beyond a simple game will make a bug :frowning:
I’m no advanced user and I keep encountering illogical behaviors. To the point of having to check every new tool one by one.

Tested and it adds a value at index 6, which is the expected behavior.
image

When you try to access to a value beyond your array limit, it automatically creates the values before it, because an array is a sequential structure.

The value “0” you see in the Text is because GDevelop never display nulls, it always display 0.

Infokubarcade has it correct.

A few things:
In GDevelop, event expressions, conditions (save one), and actions will always create any variable (or child) if it doesn’t exist. This means if you have a condition or expression that checks the value of Structure Variable child A.first, and you have not set its value befor, this child now exists and is 0 (nulls are never created directly via events, so 0 is creates for values, and an empty string of “” is created for strings)

The only exception is the “check if child exists” condition.

Outside of that specific condution, your logic in your events accesses those variables before hand and therefor would always create those children before you append.

Separately, do not call arrays with Name.Childname, thats for structure variables. Calling them on that way may retype your parent variable as variables are not strictly typed. Arrays are always Name[ChildNumbee]. In your case you are fine since it is switching back to an array due to your append action, but it is important to call out so you are aware of proper syntax.

The illogical behavior I was pointing to here was that the text printed shows “0010000000” The value 1 is appended at index 2.

It will do that for any indexes at line 2 and 3 that are different. In my example it’s 5 and 0. It will print the same thing for 5 and 7. But for 5 and 5 it will print “0100000000”

I understand your explanations and thank you very much. But they don’t explain the behavior I get. Sorry to be a pain.

Unfortunately, none of what you’re listing in this post is mentioned in your original post. You would need to show what your seeing in order for people to help better.

That said, from trying to understand what you’re describing, your last event is going to run forever until ii = 10 or more, correct? This means you’re basically calling GlobalVariable(0 through 10).

As mentioned above, anything that doesn’t exist, and doesn’t have a value, will be created with a 0 since you’re using GlobalVariable() (which is for values). The engine does not allow null values when actually calling a variable during runtime to avoid potential endless loops or crashes. (Nulls can still exist until they’re processed via logic, as far as I understand)

Edit: Additionally, you’re not calling an Array with “Array.5” or “Array.0”, You’re calling a structure. So it’s not treating them as array children until you convert it to an array with the “Append to Array”. It’s treating them as unique structure children.

You will need to call them as arrays (Array[number]) in order for it to treat them as array entries.

I used array[5], it doesn’t change the results: giving “0010000000” The 1 is appended at index 2

Arrays and structures seem to behave identical on the surface. They’re stored differently. Sometimes, addressing them in different ways can change the type and erase the existing values. Consistency is a must.

I tried set variable a[1] … and it creates a structures. Two of the ways to guarantee a variable is created as an array is to either set the variable as an array in the variable panel and use variableName[number] or use one of the array add (append) actions.

I’m used to having to declare variables. Gdevelop is very flexible with variables although that can cause things to “break” as it changes variable type on the fly and creates variables when you just try to get their values. There is a method to the madness. It can just takes time to learn how Gdevelop handle variables. I like to do a lot of testing.

1 Like

Okay, after some testing, I think I see why Infokubarcade and I wouldn’t be able to reproduce your issue. And it is what @Keith_1357 mentions. If you’re not pre-declaring your global variable as an array, declaring it via events, even with proper syntax (name[childnumber]) is creating it as a structure first.

I had been testing with creating a global variable named “array” that was an array with no children, and it gets the same behavior infokubarcade was showing.

I don’t think that’s intentional, as variables declared via events are normally expected to be the same behavior as if they’re declared in the editors.

I’ll rewrite the topic title here, and get it submitted over on the github. As a workaround, precreate your global variable array’s based level in the global variable editor, and it should behave as you’re looking for. Edit: Submitted as Array variables declared via events are created as structure variables · Issue #5167 · 4ian/GDevelop · GitHub

1 Like

I was doing more testing. The 1st two lines create a structure. The add or append converts the structure to an array. With the existing structure variables becoming index 0 and 1 and then the appended value becomes index 2.

I’m still unsure if this is a bug or feature.

Edit: if you set the variable as an array first, it starts and stays an array.

1 Like