[SOLVED] Save and Load Global Variables in Windows App

So I’ve been working on a very large game for a while now and have been hoping to put out a demo, but have encountered quite a few problems when it comes to getting the game to save and load content or export into a workable state.

I’ve included images of the save a load mechanics. The first image shows all the global variables being written once you click the proper button. The second image is on the same Event page and simply shows how the data is read once another button is clicked. The final image shows one of my other Event pages and how it should be reading the necessary data. The scene variable I’ve tied all this to is called “SAVEDEMO” and I have added it to every level in the same manner as you see in image #3.

Despite this, the game refuses to save global variables when I entirely close the game, reopen it, and then press load. I suspected that using the ‘Local Folder’ export method would make this tactic more viable, but the game is too large to be played via browser window and in rare cases where I have gotten it to play on browser windows, the game is extremely laggy and prone to freezing, especially when I attempt saving or loading. So my only other option appears to be turning the game into a downloadable app as mentioned before, though nothing I’ve found online has confirmed if saving or loading in the app version is even something that’s possible.

So my questions are specifically as follows:
Is my programming of the save and load features correct or is something off?
Can downloaded Windows apps save and load material at all or does it have to be a local file?
If it does have to be a local file, is there any safe way to reduce the number of files so the game isn’t a laggy/crashing mess?
Failing all that, is there some other export setting that I haven’t tried?

I’d immensely appreciate any help or advice on this matter. I am still relatively new with GDevelop, so the clearer/more detailed the response, the better.

What you want to do while saving is putting every variable in a structure (like savedemo) and save that structure in a single write to storage action.

I don’t think I’ve ever tried that before and don’t necessarily see an option that fits your description unless you’re speaking of the “child existence” condition.

Set variable structurename.childname
Please read the wiki entry about structures.

I think I understand the purpose from reading that, but as far as actually creating child variables and incorporating them into this save and load process, I’m don’t think I see anything definitive.

I discovered you can create child variables beneath the scene variable, as included in the image below, though does this mean I would have to enter the name of every global variable here with the same numerical value of 0? And how would that look when placed on the condition/action bars?

Sorry for the confusion, this is just a side of GDevelop I’ve not at all worked with.

Here you are basically writing all values in the same variables, so only the last write will work. Either use another name for each variable and load all values individually or store every variable in a child of a structure, and save that structure, in that case the load is unchanged.

So it would look more like this? Meaning each global variable I want to save has to have a different scene variable?

if you read the wiki page attentively it says local storage works only with scene variables. but of course you can write any kind of variable to it, but when you load them back they will be loaded as scene variables. you should write them in their place with hand. for example:

Interesting. I’ll give this a shot tomorrow. If anyone knows of any tutorial videos or example games that display this type of layout, I’d love to take a look.

there is a save-load example somewhere.

Most of the ones I’ve come across contain only the write and read variables examples, which is essentially what my three images above were based off of.

The issue appears to be finding games that actually support saving and loading after a game has entirely been closed out and reopened rather than simply changing scenes in the same play session.

Am I understanding correctly that every global variable I want to save & load has to have it’s own corresponding scene variable to set itself to?

This is a little tricky for me cause I have over 20 levels and roughly 40 global variables, and it seems awfully tedious to go through every level and add the proper scene variables.

Also, does the name of the ‘group’ have to be different for each variable?

technically no, you can save any kind of variable directly, just the loading part needs this buffer scene variable. but after you do the global variables value to the loaded values, the buffer scene variables no longer needed in that session.

[Ctrl] + [C] => [Ctrl] + [V]. you only have to do it once, put all of the related events in an event group, and you can copy-paste the whole group at the same time. or you can use a variable structure as arthuro555 suggested.

yes. the “group” represent your variable in the local storage. so as you use different name to your variables in the code, you should use different neme to your groups in the local storage. you can organize your local storage with the storage name. like a folder structure in your hard drive.

I’m getting a better idea of it now.

Here’s what I have. Top image shows how I laid out my scene variables. They only look like this on one level though. I didn’t want to add them to other levels until I knew for sure that they were set up properly. That’s assuming scene variables even need to be on the other levels at all.

Second image is how the various data is saved. Once again, this is only present on this level right now until I’m told otherwise.

Third image is how the content is loaded. Again, it’s only applied to this level.

Fourth image is the loaded progression being applied. I wasn’t sure if all of these actions could be dropped onto the same action bar as the loaded content in the third image, so I put them in a separate action bar. Once all the actions in the third image have completed, I just altered a random object variable that triggers the content you see here in the fourth image. Not sure if that’s a good approach or not.

As it stands, in the preview window, clicking load doesn’t seem to change anything after I’ve closed the game and reopened it, but I’m not sure if that’s simply because all the content from these four images is only applied to one level instead of all of them, or if that’s because my coding is wrong or preview windows don’t actually have the capability to save and load content whatsoever.

Any further insights into cleaning this up would be much appreciated.

FULL

the preview use the local storage perfectly. if you have issues, you should use the debugger function to check the variables, and/or the developer tools to check the local storage.
your actions looks OK, but I’m not sure about the conditions. you should know what are you want to do.
just a comment: the events wich I posted earlier are part of my “at the beginning of the scene” group, so I didn’t use any condition to load and apply the values. it’s ok if you use conditions to load, but may be better practice apply the values instantly after load, without any condition because they actually belong together.

when you are done and everything works fine, you can create an external event from the loading events, and you can use that in the other scenes (if the used variables are the same).

Thanks! Is it alright for those scene variables in the top image to have no numbers attached to the right of their rows? Or will leaving them blank cause issues down the line if global variables change?

arthuro555 was also mentioning that my scene variables should include “structures”, which they don’t have here. Is that problematic at all?

no problem if you fill their values first with the loading, after that “make them global”.
the variable structure is not neccessary.

I’m getting closer. Next problem I’m running into is setting up the external event properly. When I make a new external event, it asks me to choose an associated scene (seen in the bottom image). I can only select one, even though I’d prefer the event to apply to every level.

Just to test things out, I selected the first level ‘Alpha1’ and added the code you see in the top image. This includes the ‘read’ information and the load applications, both in the same actions column since you said that would be a good idea (I just cut off the bottom so it doesn’t look outrageously long). I also successfully added the external event link to all the necessary scenes.

Upon pressing play, some of the games global variables seem to no longer exist or operate properly, whilst others seem fine. Pressing save or load on the necessary page doesn’t seem to have any effect either. I’ve checked the code several times and don’t spot anything off. I’m hoping it’s not an issue that requires I enter the debugger mode, since that’s another facet of GDevelop I know very little of.

Hi, what i do in my games is this logic:

One External event sheet with all Globals and LocalStorage i call this MainGlobals
A GameScene to rule all levels, hud, and other external layouts, and in this GameScene a link of MainGlobals, later in my levels i just add a link to the GameScene and it’s own logic.

MainGlobals

GameScene

Level_1

2 Likes

don’t worry about it. when you add the link in other scene, this window will pop-up again.

I’m afraid this is it. if you are serious, you have to learn sooner or later. it’s very helpful, and easy to understand in GDevelop. and only you can check runtime, when the value of a variable changes. I can’t see from here what happens and why.