[SOLVED/EXAMPLE] Dynamic structure to file, file to struct

H̶i̶!̶

̶I̶’̶m̶ ̶b̶u̶i̶l̶d̶i̶n̶g̶ ̶t̶h̶e̶ ̶s̶a̶v̶e̶ ̶g̶a̶m̶e̶ ̶s̶y̶s̶t̶e̶m̶ ̶f̶o̶r̶ ̶m̶y̶ ̶g̶a̶m̶e̶.̶ ̶I̶ ̶n̶e̶e̶d̶ ̶t̶o̶ ̶s̶a̶v̶e̶ ̶t̶h̶e̶ ̶v̶a̶l̶u̶e̶s̶ ̶o̶f̶ ̶l̶o̶t̶s̶ ̶o̶f̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶s̶ ̶t̶o̶ ̶a̶ ̶f̶i̶l̶e̶.̶ A̶l̶l̶ ̶o̶f̶ ̶t̶h̶i̶s̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶s̶ ̶a̶r̶e̶ ̶o̶r̶g̶a̶n̶i̶z̶e̶d̶ ̶i̶n̶ ̶a̶ ̶s̶t̶r̶u̶c̶t̶u̶r̶e̶ ̶t̶h̶a̶t̶ ̶c̶o̶n̶t̶a̶i̶n̶s̶ ̶s̶u̶b̶s̶t̶r̶u̶c̶t̶u̶r̶e̶s̶ ̶(̶a̶n̶d̶ ̶s̶o̶ ̶o̶n̶)̶ a̶n̶d̶ ̶m̶a̶n̶y̶ ̶c̶h̶i̶l̶d̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶s̶ ̶a̶r̶e̶ ̶r̶e̶c̶u̶r̶s̶i̶v̶e̶l̶y̶ ̶g̶e̶n̶e̶r̶a̶t̶e̶d̶ ̶d̶u̶r̶i̶n̶g̶ ̶g̶a̶m̶e̶p̶l̶a̶y̶.̶

̶I̶ ̶c̶a̶n̶ ̶b̶u̶i̶l̶d̶ ̶a̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶t̶h̶a̶t̶ ̶w̶r̶i̶t̶e̶s̶ ̶e̶a̶c̶h̶ ̶v̶a̶l̶u̶e̶ ̶t̶o̶ ̶a̶ ̶f̶i̶l̶e̶ ̶a̶n̶d̶ ̶a̶n̶o̶t̶h̶e̶r̶ ̶t̶h̶a̶t̶ ̶r̶e̶t̶r̶i̶e̶v̶e̶s̶ ̶i̶t̶ ̶b̶u̶t̶.̶.̶.̶ i̶s̶ ̶t̶h̶e̶r̶e̶ ̶a̶ ̶n̶a̶t̶i̶v̶e̶ ̶w̶a̶y̶ ̶t̶o̶,̶ ̶f̶o̶r̶ ̶e̶x̶a̶m̶p̶l̶e̶,̶ ̶c̶o̶n̶v̶e̶r̶t̶ ̶a̶ ̶s̶t̶r̶u̶c̶t̶u̶r̶e̶ ̶i̶n̶t̶o̶ ̶a̶ ̶J̶S̶O̶N̶ ̶o̶r̶ ̶X̶M̶L̶ ̶f̶o̶r̶m̶a̶t̶,̶ w̶r̶i̶t̶e̶ ̶i̶t̶ ̶t̶o̶ ̶a̶ ̶f̶i̶l̶e̶ ̶a̶n̶d̶ ̶r̶e̶t̶r̶i̶e̶v̶e̶ ̶i̶t̶ ̶l̶a̶t̶e̶r̶?̶ ̶I̶ ̶m̶e̶a̶n̶,̶ ̶a̶ ̶s̶i̶m̶p̶l̶e̶ ̶w̶a̶y̶ ̶t̶o̶ ̶R̶E̶C̶U̶R̶S̶I̶V̶E̶L̶Y̶ ̶c̶o̶n̶v̶e̶r̶t̶ a̶n̶d̶ ̶w̶r̶i̶t̶e̶ ̶A̶N̶Y̶ ̶s̶t̶r̶u̶c̶t̶u̶r̶e̶ ̶t̶o̶ ̶a̶ ̶f̶i̶l̶e̶?̶ ̶H̶o̶w̶ ̶i̶t̶ ̶w̶o̶r̶k̶s̶?̶
H̶a̶s̶ ̶a̶n̶y̶b̶o̶d̶y̶ ̶a̶c̶h̶i̶e̶v̶e̶ ̶i̶t̶?̶

[size=150]SOLUTION:[/size]

Ok, maybe there is not a simple way to write and read an entire complex data structure (I mean, structure with sub-structures, dynamic generated child variables and simulation of arrays) but is not impossible using the same principles of file format design.

This project can generate a random complex data structure, write it to a structured file, read it from the file and display the data on screen.

It’s useful if you have to save the status of complex games with dynamic generated data organized in structures (for example, the save-game system of a strategy game where you have to save the data of different units generated at runtime, so it is not possible to know in advance how many variables you will use or the amount of data that must be saved).

But remember, this is just an example, you need to adapt it to your own purposes.

dynamic_structure_to_file.zip (3.85 KB)

The project don’t use any kind of graphics, just text.
Instructions are displayed on screen.

Hope it will be useful! :wink:

I wish gdevelop had support for arrays. Also loading and saving array files.

I need to stop quoting by error my own posts every time I pretend to edit them :neutral_face:

I didn’t test your example, but did you consider converting your variable to JSON (ToJSON(MyVariable)), storing the result (which is a plain text) in another variable (call it “SerializedVariable”) and saving this variable to a file? :slight_smile:

Then to load the variable, just load the content into “SerializedVariable”, then use the action to transform JSON to a variable (in network category if I’m not mistaken) and boom, you get your structure :smiley:

Should be done in 5/6 actions at most and it handles arbitrary complex structures having any number of childrens :slight_smile:

1 Like

That’s what I was looking-for since the beginning!

I didn’t know about the ToJSON() function!

Thanks.