How to copy structure/array values into an object variable

I’ve looked all over but can’t find a way to copy an array or structure variable into an object variable’s value.

For example, I have an array like this made up of 3 arrays, each with a set of 4 or more pairs of x,y coordinates:

AllCorners = [
[[0,0],[24,0],[24,24],[0,24]],
[[0,0],[24,0],[24,48],[0,48]],
[[0,0],[24,0],[72,24],[72,48],[48,72],[24,72],[0,48]]
]

As the game starts and creates objects, I want to iterate through the array and assign each child of the original to one of the objects.

something like:
Object1.corners = AllCorners[0]
Object2.corners = AllCorners[1]
Object3.corners = AllCorners[2]
and so on…

This way, Object1’s variable “corners” should contain: [[0,0],[24,0],[24,24],[0,24]] And then I should be able to iterate through that for other functions.

But anything I’ve tried to do just ends up with the “corners” value = 0.

I’ve used:


to convert the string into a global structure variable, but I don’t know where to go from there.

Any suggestions?

There is a concert Json to object variable action. The value you pass in should be a call to ToJSON() of the array or structure you want to copy.

Thanks. Could you show me a screenshot or example of how I would do that?

Can I call that action on the AllCorners global structure and have it copy just one child into the object variable, considering that child will also be an array of arrays?

Or does each child of AllCorners have to be converted to a string first and stored somewhere else and then re-convered back into an object structure variable?

Here’s how I do it:
Parse JSON string ‘GlobalVarToJSON(LevelData)’ and store it into ‘LevelData’

Which just copies the structure/array data from the global LevelData to the scene LevelData.

You could copy just one child at a time, you would just change the reference to be something like “LevelData[0]” for both the source and destination… but I would instead suggest you design your structures and arrays in a way that they are intended to be copied in full… otherwise you’ll end up with a lot more logic to pick and choose which pieces to copy.

I’m not aware of any method of copying complex data structures without first converting them to JSON in GDevelop… but they make it pretty easy to do just that.

Thanks, that worked.

First, I parsed the JSON string into global variable:

,

Then iterated through the children of global variable, wrote a string of each child to storage, then retrieved said child and parsed that into object variable:

Is that too many steps? Is there a way to do it without saving it in storage in between? Anyway, it works. Thanks.

Instead of iterating over it you can just convert the global variable to json directly and parse it directly afterwards