How to save the position of a lot of objects

I’m making a game and the player is able to build structures out of a few types of objects. But it’s a bit inefficient to save all the player made objects one by one in their own variables and then save them to a file. I was wondering if there was a more efficient way to save and load the positions of massive amounts of objects

Exactly how do you iterate and register the objects you are going to save?

My only problem is having to make a new variable for every object I want to save. and then I can only save as many objects as there are variables to store them in

You need to use dynamic variables to store your data.

I made an example:

Saving data with dynamic variables

1 Like

While loading you can use the same scene variable for all of them.

Hi! thanks for the example. There’s just a few things that are confusing me

so when you press s it changes the variable blocks to 0 and blocks.count to 0. But I checked and there weren’t any scene variables. So are you able to create variables by changing a variable that doesn’t exist? Assuming this is the case, then in the repeat for each object loop you are creating a load of variables, blocks0.x, blocks1.x ect. But then, at the end you just save blocks without changing it’s value. When does blocks get the values of blocks.count and all the other variables we just created before it’s saved?

Yes, I’m creating variables that don’t previously existed.

The variables created are in the blocks.1.x format (not blocks1.x, the method creates that extra point, you can take a look at the variables at runtime in the debugger).

I make the entire blocks variable equal to 0 before register the data to wipe out any possible old data registered there (for example, if you saved 5 blocks before, and then clears the screen, makes 3 new blocks and save them without wipe the data, the 2 extra blocks data will still be in structure).

I’m creating and filling the variables at the same time.

If I say:

blocks.1.x = 123

Then, automatically there’s a variable called blocks that contains a variable called 1 that contains a variable called x that’s equal to 123. So at the finish of “repeat for each object” there’s a variable blocks with all the structured data.

The events nested under “Repeat for each object” happens once for each block object in the scene, and the count variable adds 1 in each iteration. That’s how the data is created.

To better understand all of this, I insist you run the game and take a look at scene variables in the debugger (after pressing “s” in the game).

1 Like

Ok. I think I get it now. Thanks so much for helping me!

1 Like

that helped me alot thanks

1 Like