[Solved] Load/Save multiple variables in storage using loops

Can someone guide me the easiest way to Load/save multiple variables in Storage?

This is how I am doing now:


LOAD
Load “LevelStats.Level1” from storage “GameStorage” and Store value in “LevelStats.Level1Temp1”
Load “LevelStats.Level2” from storage “GameStorage” and Store value in “LevelStats.Level2Temp2”

Close structured data “GameStorage”

Change the global variable “LevelStats.Level1” set to “LevelStats.Level1Temp”
Change the global variable “LevelStats.Level2” set to “LevelStats.Level2Temp”

SAVE
Save GlobalVariable(LevelStats.Level1) in LevelStats.Leve1 of Storage"GameStorage"
Save GlobalVariable(LevelStats.Level2) in LevelStats.Leve2 of Storage"GameStorage"


Can these be used with variables in the following way? If yes, how can I implement the For loop:

LOAD with Variables:

For Var = 1 to N Do:
Load “LevelStats.Level{var}” from storage “GameStorage” and Store value in “LevelStats.Level1Temp{var}”

Close structured data “GameStorage”

For Var = 1 to N Do:
Change the global variable “LevelStats.Level{var}” set to “LevelStats.Level{var}Temp”

Jsonify the variable. Here’s a link on how it’s done

Thanks! I have already tried jsonifying the variables. However, after wasting too much time updating the event sheets, the game is still not bug free when storing/loading variables. I gave up finally and now decided to revert to the old save/load method and enhancing it now :frowning:

Not sure if this example is correct. Parsing json string and storing it into global variable does not works for me. However, storing the parsed json string into a scene variable works for me.

If that’s how it should be done (parsing and saving string to scene variable) then I don’t understand the purpose of “Parse json to global variable” action.

Can you screen shot the event that puts it into the global variable?

Here’s the screen shot of the load event. Storing it into global variable does not work:

While storing it into scene variable works:

Neither should work fully as you are loading the variable as plain text and not actually json. Your events do not show you jsonifying the variables at all. Unless you are jsonifying your variable somewhere else in your events, this is likely just not overwriting your scene variables, which is why it works there, and since the globals don’t exist they aren’t working there.

Your second action in the last screenshot should use the expression ToJSON(TemporaryVariable) within the action.

Just using VariablrString will just see the top level variable. Which may not gave any data if its a structure variable.

(You may need to show your save events too if using the above expression doesn’t work)

I see your point here. Yes, mine is a structure variable.

I did not use the expression ToJSON() because the example shared by @MrMen did not used it. May be I misinterpreted it.

I changed the loading events:
image

This is how I am currently saving all the variables:

Although, I am not sure how to save just a single variable (like GameStats.Level1.Unlocked) to Json. :frowning:

You really shouldn’t be saving or loading single variables.

That leads to bloating your load logic, and can lead to errors as you may mistype something along the way.

Generally youd have a single parent structure variable (Gamestate), with your game logic stored as child variables, and the you just save and load the main structure variable only. The child variables are loaded and saved as part of the parent.

There is no benefit I can see to manually loading an individual child variable, but if you really want to you could save and load to a different parent structure entirely then just set your destination variable to the value of the single child variable you want.

If you are really wanting to save a single child variable, you just type out the full dot path in the expression.

ParentVariableName.ChildVariableName, with more . And childvariablenames as you have more sub children.

Edit: if you are jsonifying upon save, you shouldn’t have to jsonify upon load. Should work fine unless your load is going weird. You should check in the debugger after you do a load and see what it puts into the global variable.

Yes, I do have multiple structures all within a single parent structure. By “different parent structure”, do you mean like this?

You have a valid point about saving/loading single variables. I am not in favor of loading single variable (since then I have to be extremely careful about variables/paths), but I have multiple requirements to save single variables based on game events. For instance, when a player dies without completing the levels, all the points he earned so far in the level does not add up to his global score. Only a single variable must be updated in this case. Also, there are some items in levels which are not available if they are picked up once even when the player dies without completing the level.

About saving a single child variable. Are you suggesting this:

Or this:

image

I am presuming that when I am using structures in GlobalVarToJSON, it is applies to all the children. So, when saving GameStats.LevelStats, it will save all the children under LevelStats.

I apologize, Your problem statement doesn’t make sense to me.

You shouldn’t be saving your save data when the player dies. You should just be updating your global variable for the current gamestate.

Every time your game state changes should not be saved to storage. You should be saving at specific defined intervals, such as when the player gets to a save point, or before the game exits, or every new level unlock.

None of this would preclude you from saving the top level variable at those times.

Your event logic would decide when to update the global score. Generally you would track the score as a scene variable and then add that to/update the global variable tracking the total score after defeating the level, as an example.

As far as your question, the bottom event would ensure you have unique saves per sub variable, yes, although as mentioned above I wouldn’t recommend doing that as a concept.

Thank you for your feedback!

Yes, I realized the flaw in saving the single variable after reviewing the code today. I reviewed the logic and cleaned my event sheet to save top level only. I have rearranged the logic to eliminate the need for saving individual variable by saving them only at checkpoints. The code is much clearer now.

However, I am still struggling with the json conversion.

This is how I am currently saving global vars at checkpoints/level completion:
image

And this is how I am loading them:

OR:

I can see in the Debugger that the variables values are getting updated:

Global variable shows correct values when I continue the game without exiting:

However, GlobalVariables don’t retain their values when I exit and restart the game. It shows only the default values:
image

The scene variable shows the saved values though:

I suspect that I am somehow messing up the scene variable conversion to Global variable while loading or saving the variables. : -(

This is old screenshot since you can see it have write and read instead of load and save
But functionality is the same
Look at what is in yellow boxes
Write is save
Read is load

Right now none of your events are loading into a global variable. They’re all scene variables

The “Convert JSON String Blah and store it in Blah” is a scene variable only event. There is a Global Variable version, but you’re not using it. The icon of the variable is another indicator. (Globals have a globe icon)…

@Silver-Streak: As I mentioned earlier in this thread saving the variables to a global variable didn’t work either. I have used the global variable version in this and you can see that it has globe icon.

EDIT: As per your suggestion, I also tried “ToJSON” and storing it to a global variable, but it didn’t work either.

I deleted the load/save events and changed them as below:

Load event:

Save event
image

It worked for me as long as the player don’t exit from the game. I finally shut down my machine, restarted it and (miraculously!!) now the “same” events are working exactly as intended. I checked the debugger and the values are populating correctly in the Global variables. I still have no clue how it worked; I tried the same actions with/without “TOJSON” or global/scene variable multiple times, but it never worked for me.

@Silver-Streak: Thanks to you my code is now much more lightweight. Thank you so much for being patient with me and helping me out here. I really appreciate your support.

@MrMen: Thank you for pointing me in the right direction. I cannot imagine loading/saving tons of variables in my game without jsonifying them. Now, the whole process of loading/saving global variables to storage has been reduced to three actions.

@ZeroX4: Thanks for sharing that great example!

2 Likes