[Solved] Save/load created objects

I have a problem that I can’t solve in my game that is part of the save and load. When we create an object on the screen, dynamically through the game, this object is not saved by the game. At least not in the conventional way…

For example, if I already have this object on the screen, and I save it in some position, when loading the game, this object will be there.
But if I create this object through a command or even kill an enemy, the created object doesn’t seem to be saved.

Of course I know that I have to do the opposite later, when loading, the objects in question must be created again.

Download

I’ll leave an example here for you to check, and if you can help me I’ll be grateful.

Your problem lies with the load event :

What character is it iterating though? At the start of the scene, there are none, so it won’t ever load from storage.

Press C to create!

Red comment in source code
You create the character, and save. then load the game again.

Watch the video below: What I want to do is create the objects, save them and then load them all again, in the same places. That’s why I used “for each”

But that’s not what happens. When the scene starts up, there are no characters. So the loading from storage is not actioned, because there are no characters to iterate through, no matter how many characters were created previously or how many character’s details were saved. There are no character objects in the freshly loaded scene. It will never action.

If you’re lucky, this :

image

is evaluated as “characterX0 exists in storage”. But I believe the iteration does not happen because there are no characters in the scene when it is freshly opened.

When you create the characters, the load events have already been evaluated. The trigger once has been fired. It will not be triggered again.

Also, you need to save the data differently. As it stands, only one entry for characterX1 is ever saved. The “characterX1” is the storage key, and storage keys are unique. So every time GDevelop write the X value for charater1, it overwrites whatever was there.

There is no easy way that just purely writes and reads data that will fix this. To save individual character information requires a bit of data organisation.


The best solution to saving the character’s position is to iterate over each object, store it’s name, X and Y values in a structure variable, and push that variable onto an array variable. Then JSONify the array and save that in storage.

Something like :

(make sure the write to storage is a text write).


To load, read in the JSON string, JSON.parse it back into an array variable and then iterate over all the array elements (which will have the name, X and Y values). Only then can you create the character based on the name that’s been read back (because you have the characters in an object group, this is quite simple). Something along the lines of :

This works. I’ve tried it myself :

Storage

4 Likes

Wow, it’s quite different than I imagined. I’ll put it into practice and then report it here. Thank you for your help.

@MrMen,I changed your code to what I needed, and it looks like this:

And your code worked very well. your tip was very valuable, thank you!

1 Like

Can you give me this template please I want to make such a save in my game about balls