Save and load ALL global variables to a file?

Is there a way to save all global variables to a file all at once instead of one event at a time? For instance: a “GlobalVariable(*)” wildcard or something?

Also, load all at once?

You can use structure variables similar to arrays.
Add a number as structure variable to a global variable like so:

variableName.1 = value variableName.2 = value variableName.3 = value

So instead of having multiple global variables, you going to have a single global variable with multiple structures.
The point is that you need to have all the values inside the same variable but in different structures.
In this case the structures are numbers 1, 2, 3, 4, 5, …etc

And then, you can write each structure variable in to file using the expression:

GlobalVariable(variableName[“structureName”])

and a counter variable can be used in place of structureName which is going to be incremented by 1 after each run.

For example:

If counter < 4 Write GlobalVariable(variableName[""+ToString(Variable(counter))]) in "groupName"+ToString(Variable(counter)) of File "file name" counter = counter + 1

Keep in mind the structure name need to be a string in the expression, that’s why you need to convert the counter variable in to string using ToString() expression.

This way you can write all the global variables in to file using a single event but you need to use the same variable name and numbers as structure names which is not practical as you need to keep remember which structure is used for what. Like Player.1 is life, Player.2 is score…etc
It not very practical as in case of many variables it difficult to remember which structure used for what.

Alternatively, you could assign actual variables to structures first like so:

variabelName.1 = PlayerHealth variableName.2 = PlayerScore variableName.3 = PlayerLives

And then write the value of structures in to file as I showed you above. This way, you can keep using variables with meaningful names (easy to remember) and write the value of all of them in to file using a single event :slight_smile:

You’ve been very helpful, ddabrahim. 2 of my questions in a row. I’m still not entirely sure how to make this work in my dungeon crawler game. Not because of function, but because of keeping track of so many things I don’t have the first dungeon complete yet, but there are already many variables that need to be saved: player stats like XP, HP, strength, defense, speed, agility, then there’s stored variables for the dungeons like which chests have been opened, doors unlocked, puzzles solved, boss defeated. Then there’s the inventory and much more. It seems like it would be much more organized if I do it the way you said, but it would become very difficult to keep track of it after 4 towns and 30 dungeons.

I also apologize because I worded my question wrong. I meant to say in a single action rather than single event.

Maybe there could be an extension made to create/load a game save file from global variables at some point?

No there is no single action to do that.

I think 4ian has mentioned some time ago his may going to add "save game " actions to save the state of the game and scenes and be able to load it later but no such feature yet.

What we got is “Scene Stack”. Using scene Stack you can sort of “pause” the current scene when you go to a different one, so when you go back to the paused scene, you can continue where you left off before.
It sounds perfect for a game with dungeons and staff, but I’m not sure if Scene Stack also works when you quit the game, and for multiple scenes. It may works only for the time the game running and maybe only for the previous scene. I have never used this feature of GDevelop, so I don’t know, but you can give it a try.

You can pause the current scene and go to an other one by using the “Pause and start a new scene” and when you go back to the scene you can continue where you left off, but as I have mentioned, I have never used it so I don’t know how well it going to fit your needs.

Well, writing an algorithm to save everything and load everything and only what you need, when you need it is never easy especially in such scale :stuck_out_tongue:
What I normally do is have a save file for each scene individually and a save file for global things, and I try to avoid using global variables too much, I use global variables only for things that need to be accessed during the whole game.
Such as:

Game stats (using global variables):
Levels unlocked
Score
Money
Items unlocked in the game, available to player
Player stats:
-hp
-speed
-strength
-inventory

For everything else I try to use scene and object variables whenever I can and write values in to individual save files for each scene and load the values from the save files at the beginning of each scene such as:

Scene stats (using scene variables):
-difficulty level
-missions, goals
-enemy types can be spawn (if it random)
-chance of item spawn (if it random)
-maximum value of item spawn (if it random)
Anything related to scenes.

Object stats (including Player and using object variables):
-position
-scale
-angle
-layer
-animation
-variables (anything and everything really: is it opened, closed, used, available or unavailable, value, quality, friendly, natural, hostile, speed…etc)

Of course it different game to game, but this is the main concept I’m always start with to keep everything organised.

Scene stacking works very well for my game so far… when in town, it lets me enter buildings without losing my character’s position in the town scene, and loading battles when in dungeons. I don’t see it working for saving the game though since exiting the game closes all open scenes and starting the game opens fresh unless variables are loaded.

I never really considered loading scene variables from a saved file, but I have each room of my dungeons in individual scenes, so I could have 1 file for each dungeon, like a file for “D_A_0001” (short for “Dungeon”, “Region code”, and first dungeon) and set variables from it at the room level with “A01” (map grid code) like “A01_Chest_1”, “B02_Puzzle”.

I was planning on making it semi rogue-like by only saving once you leave a dungeon to return to town, meaning if you die in the dungeon, you lose your progress. I’m not real sure how to make that work by saving scene variables when going through the dungeon unless I use scene stacking through the entire dungeon. If someone keeps going back and forth between rooms, that’s a lot of stacked scenes. I could have it write the variables to a temporary save file, then write the temporary information to a permanent save file when leaving the dungeon.

Thanks again for your help and input on this. It’s greatly appreciated. :smiley:

You can always write any value in to file and read the value from file anytime you want.

When you enter a dungeon, probably you enter in to room 1, so you can load the stats of room 1, when player dead, you can save the stats of room 1 and also when player move between rooms, you can save stats of current room and load the stats of next room.

For example let say I’m going in to Dungeon1 so the scene is going to change to Dungeon1_Room1.
When you load the scene, you can load all the stats in to scene variables from file “Dungeon1_Room1.txt”.
If you want you can load global stats for the whole dungeon that is going to be used in all rooms across Dungeon1.
So you can have 2 files to save and load:

“Dungeon1.txt” = to load global stats for Dungeon 1, such as chance of enemy spawn or chance of item spawn and such if it the same for the whole dungeon in every room.

“Dungeon1_Room1.txt” = to load and save Room1 stats of Dungeon1, basically everything such as position, scale, animation, angle of objects in the room and different object stats, such as the chest in Room1 is open or closed, it got something in it or empty and staff like that.

So Dungeon1.txt can be used to load and save global information for the whole dungeon and the Dungeon1_Room1.txt is can be used to load local information exclusive to Room 1 of Dungeon 1, and you can have a save file for each room of the dungeon. So this is how it could work:

[code]If Enter to Dungeon1
Load scene “Dungeon1_Room1”
Load stats from file “Dungeon1.txt” in to global variable
Load stats from file “Dungeron1_Room1.txt” in to scene variables

If go to Dungeon1_Room2
Save stats in to file “Dungeon1_Room1” from scene variables
Load scene “Dungeon1_Room2”

If enter Dungeon1_Room2
Load stats from file “Dungeon1_Room2” in to scene variables

If player dead
If player is inside Dungeon1
If player is inside Room2
Save stats in to file “Dungeon1_Room2” from scene variable[/code]

This is a very raw and basic example, I don’t suggest to do it exactly like this, Dungeon to Dungeon and Room to Room. But this is the basic concept.
Instead, I suggest to use variables, to make it load and save dynamically like so:

[code]At the beginning of each scene:
currentScene = “get name of current scene”
Load stats from file " " + VariableString(currentScene) + “.txt” in to scene variables

When go to new scene:
Save stats in to file " " + VariableString(currentScene) + “.txt” from scene variables
Go to Scene “any scene”

When player dead:
Save stats in to file " " + VariableString(currentScene) + “.txt” from scene variables
Go to Scene “any scene”[/code]

This is the basic concept I hope it helps somewhat but to do something like this, I suggest to use:

Global variables: only to store information affecting the whole game
Scene variables: to store information affecting the currently running scene
Object variables: to store information about objects, everything actually: chest is open or not, enemy is fast or not, weapon is powerful or not…every object related stats should be stored inside object variables.