Bug when creating an array as a child of a structure

I’ve come across an issue where I have a local structure variable that contains empty array children. In a loop, I initialise instances of this structure and then add them to a scene array.

What I’ve noticed is that if I inspect these children arrays in the debugger, they are represented as numbers, instead of arrays:

Adding items to these arrays converts them back to arrays:

The issue I discovered is that if I accidentally try to access the empty array via an invalid index, the engine will set the number value to the index and now the array length is reported as 1.

Note that the above does not happen if, instead of the local Struct variable, I use the TestStruct structure variable declared at the scene level:

No doubt that one should be checking that the array index is valid, but the behaviour of the engine here is definitely unexpected. I had a bug in my code where I was effectively accessing index -1 of an empty array but instead of throwing some sort of error or returning a null value the engine quietly added an item to the array, which made it much harder to track down and identify the bug.

I think the fact that the engine is reporting the array as a number indicates a issue that warrants investigation.

Here is the full event sheet:

The forum doesn’t allow me to upload zip files so I can’t share the project.

It took me a bit to wrap my head around what you were saying. Multiple layers of variables can be confusing.

I tested it and I got the same result.

Adding a local structure that contains an empty array to a scene variable defined as an array does create an array of structures with a child that is a number instead of a child that is an array.

Adding a local structure that contains an empty array as its only child.

My events

But using only scene variables creates an array of structures with a child that is an empty array.

The same events but both variables are scene variables.

While you can’t add a zip file. You can upload a zip to file sharing site and post a link.

1 Like

@Keith_1357 thanks for taking the time to replicate the bug.

Here is an example project that demonstrates the bug:

1 Like