[Solved] How to copy an array of an array (nested array) into an array

I have an array that has nested arrays (arrays in an array),
I want to copy a nested array into another array.

Array1=[
[
[1,2,3,“text”],
[1,2,3,“text”],
],
[
[1,2,3,4,“text”],
[1,2,3,4,“text”],
],
]

I want to copy for example array1[1] into array2.

Solution

Why do you want to copy it? Maybe there’s a way to do what you want to do without copying it.

You might be able to do it with the Array Tools extension. If not you could properly copy it through JSON. Convert array to a JSON string and then from the JSON string back to a variable.

This works.

Array tools does have a function. It appends the array. So, you might need to clear the children from the target array first.

Array tools

1 Like

This is why :arrow_up:

Shouldn’t this be supported by the engine?
I tried method two using extension before asking and it doesn’t work.
I will try a new project and see how it goes.
Thanks.

Can you show us the events? I use the array tools extension to copy arrays and I haven’t had any problems.

However, you don’t really need to copy the array in order to read from it, as long as the size of the nested arrays is all the same you can just store the indexes as a variable and increase them appropriately.

Index1 = 0
Index2 = 0

SomeValue = Array[Index1][Index2]
Index2 += 1
NextValue = Array[Index1][Index2]
Index2 += 1
AnotherValue = Array[Index1][Index2]

(move to next array)
Index2 = 0
Index1 += 1
(repeat)

Or, don’t bother with the second index if you don’t need it:

Index = 0

SomeValue = Array[Index][0]
SomeOtherValue = Array[Index][1]
LastValue = Array[Index][2]

Index += 1
repeat

1 Like

I just tested my methods again. I even added another array level and it worked. Are you using scene or global variables? With the recent changes to GD, it shouldn’t matter as much. I tested both methods with scene and global variables. The extension uses different functions for global and scene variables and both arrays seem to need to both be either scene or global.

This is what I used. I added a button to trigger the for each child so I could check values before and after and do other tests.

I didn’t use both methods at the same time. I disabled one of them to test the other.

Variables are easy to “break” or modify unexpectedly. GD does some things to prevent crashes that can sometimes cause unexpected things to happen. Like creating variables if they don’t exist. It can be safer to check if arrays and structures exist before trying to access them. Otherwise, if they’re not there the 1st time then there might be an empty variable there the 2nd time.

1 Like

It works if I use Array1[0] but not when using Array1[-1]

Scene variables

They are already created in the scene variables editor.

No, every nested array has a different size.

Indexes start at 0.

A variable can exist but it might have the right children names or indexes. So, while Array1[0] could exist Array1[10] might not.

You might need to clear the target array first depending on your needs. If needed you can use for each child starting with any child not just the top level.

I want to copy the last nested array. In Python -1 means the last item in a list

I don’t know python. Just a remember, since the first index is zero, the last index is the count minus 1.

Array1[VariableChildCount(Array1)-1]
Array1[0][VariableChildCount(Array1[0])-1]

There are probably other methods.

1 Like