(Solved that way...) Save/load is quite hard...

Even i read the forum (a lot and page by page because the search option can’t find “save” because are too many results…) i can’t understand everything, so it’s not working, i read that there is a example game in the platform, so i check it and use it most of the events… But i’m making something wrong… I’m just trying to save the “values” of the:

GlobalVariable(level1), GlobalVariable(level2), GlobalVariable(level3)… GlobalVariable(level99).
(Yes, i create 100 GlovalVariables one by one, i’m sure i can do it in a diferent way but seems quite hard :frowning:
So i’m trying this for the save…

I though that i can save the value number 0 (if the level is not clear) or 1 (if the level is clear) in VariableString(ID), in the document .txt, by extrating from GlobalVariable(level(VariableString(ID))), and then make "Do = +1 to variable ID… Probably totally wrong about how it works…

Any idea for just save the value of the 100 GlobalVariables(level1), GlobalVariables(level2) and so on :cry:

So close, yet so far :smiley:

Yes, you need to access variables in a dynamic way, but you can’t do it “level(VariableString(ID))”, because you’re saying that “level” is a function that accept a string parameter, of course this function doesn’t exist :slight_smile:

Create structures:
You’ve to use structures, while creating variables in the variable’s editor window, you can do a right click over a variable to append a child, it will turn the variable in a structure automatically. Your level structure should look something like this:

Levels level0 1 level1 1 ... level99 0

Access a structure child:
This structure allows you to access a level dynamically this way:

GlobalVariable( Levels[ "level"+VariableString(ID) ] )

The mean that you want to access a child with a string, here the string is the concatenation of a constant string “level” + a variable string “ID”.
You can access a child through dots as “Levels.level5”, but it doesn’t allow dynamic access, please do NOT try “Levels.level+Variable(ID)” or anything related :wink:

Create variables dinamically:
You can access a structure dinamically in both conditions and actions, so you don’t have to add the childs manually. You can create the variables at the beginning of the scene, when load the saved game, or even doesn’t create them (since the default value returned when a child doesn’t exist is 0, if you don’t set a level value to 1 (clear) it will return 0 (not clear)).

Save structures:
A strong point is that structures can be easily saved and loaded, you can save an entire structure with two actions, because you can:
Convert a structure into a JSON string (the action is in the network section I think).
Save this JSON string (just a very long string) as a string value, and you are ready :smiley:
A similar process is needed to load the structure, first you read the JSON string in a variable, and finally convert the JSON string in a structure => This is a string expression (expression editor for texts).

Finally, I just want to note that, if the game has the classic style where you need to clear the previous level to access the next one, you could simply save the maximum cleared level, it’s all the info you need.

Thanks lizard, still sounds a little hard for me now, i’ll check everything, hope in a few hours seems easier :laughing:

Seems that “Convert a structure into a JSON string” doesn’t exists no more in the action panel… I can’t see how to save it in json, only how to load 1 content, in the action options and also in the wiki: wiki.compilgames.net/doku.php/gd … jsonfile?s[]=json… Nothing about structure… :cry:

Ok, I have a PC with GD now, and it seems that I mixed which one is an action or expression, sorry :stuck_out_tongue:

To save a structure:
*Convert the structure into a string. It’s an expression, in a string expression editor use the function “ToJSON(StructureVariable)”, located in the Conversion section, and save this expression in a variable.
*Store the variable containing the string in the file.
To load a structure:
*Read the string value from the file and store it in a variable.
*Convert the string into a structure with the action “Network >> Convert JSON to variable”, in the expression parameter use the variable containing the JSON string, and put the structure parent variable in the second parameter.

Hope it has sense now :slight_smile:

Thanks Lizard, i’m gonna try and see if i can… Still quite hard for me right now :confused: , never use this kind of programation, only php and basic javascript

Sorry if i ask for help still, but i can’t find any example and even with your explantion i’m a little too lost :frowning: .

What i make in the picture is what i understand from what you explain me… But since i don’t really know what this actions and expresion are exactly doing, i don’t know what i’m missing… i thing i need a counter like variable(ID) and +1 to ID to make the count in the levels.level1, levels.level2 and so on… Almost the word “String” in “ToJSON(GlobalVariableString(Levels))” is doing it… can anyone make it simpler?? Sorry for this question… This is quite hard if you don’t know it, program the movements and the rest with if - else is easyer… :cry:
trying_save_load.JPG

I’m checking it now… and the “ToJSON” expression accept scene variables only, not sure way there is not a global form :frowning:
Plus there is no way to copy a structure variable into another yet, so it would be impossible to save a structure this way… you should save the structure dynamically, not easy :frowning:
I will ask for some of this now :slight_smile:

By the way, sometimes you use “GlobalVariable(variable_name)” when it is a variable parameters, so you only need the name “variable_name”, for example the action to read the value from the file is wrong :wink:

:astonished: :open_mouth: what a work… Thank you again, un the end, if it’s gonna be toó hard i can try yo make one by one, but i don’t know if this way the game will collapse por will take toó muchos time to run it… As soon as i hace the sabe/load ready i’ll be able to prove the game, i’be got the rest done, except the 100 stages, the créditos and the “send facenbok results” :smiley: :smiley: :smiley: :smiley: . Definetly i’ll put you un my créditos lizard :laughing:

Dear forum friends, i finally solved for me and… That way…

In my particular case, i need to save/load if the stage it’s not clear “GlobalVariable(levelx) = 0” or if it’s solved “GlobalVariable(levelx) = 1”.
After thinking and trying and compare with the example file project “level editor” i did this…


TO SAVE
In the stage i’m playing, level1, level2, etc… in the end, when the variable Enemys = 0 “I kill all of them so the stage is clear” shows, level clear, stops the count down, etc… AAAAnd:

Delete "level2" from the file "save.txt" (because i'm in the level1) 
Write 1 in "level2" of the file "save.txt".

That way, i save 1 "because now the stage is clear, and i delete it before because the player can play again this stage, so if plays and clear it, it erase and write, something like rewrite :smiley: .


TO LOAD
I have a text object “Load game” with:

Conditions: Touch or left mouse button down The cursor/touch in on load Actions: Read "level2" from the file "save.txt" and store value in level2. Do = Variable(level2) to global variable level2.

AND IT’S WORKING :mrgreen: :mrgreen: :mrgreen:

My problem is that i have to write the two load actions for each level… what means 2 x 99…

1 QUESTION ABOUT THIS!
Do you think that it can make the game slower so many text or it doesn’t affect? i suppose and hope not :laughing:

Thanks for your help and interes

Good to know you get it working :slight_smile:
I thought from the beginning that (maybe) save the entire structure was not needed.

could someone please attach an example file of this and also add it to gdevelop’s wiki? :unamused:

I think it is quite an important element that is missing in gdevelop’s examples.

Yeah, it is so necessary, i think the information it’s quite hard to find in the wiki or in the forum. In fact in the wiki i just find how to load from a JSON file, and nothing about export data and save on any format file… If any moderator can do it, becuase i think that in the end use a .txt file was easier than try with the json file… And yes, becuase of the new people using the gdevelop, as easy it could be, better :laughing: also in my case, i don’t know how, but i can’t take the information from the variable, i must be doing something wrong, fortunattely it was just one of the values, or 0 or 1 in my particular case so i can solve it this way :smiley:

An example file of a good saving/loading system should come with the engine itself too.

I wonder why nobody has made one yet :slight_smile:

Xai1985,
You aren’t kidding about the Save/Load being quite hard. It’s next to impossible to get it working correctly I don’t even want to say how long I spent on it. I just now got it almost fully working. I’m surprised computer survived. I thought for sure I was going to end up destroying the computer.
I had to do two sets of events too for the save aspect.
I just have to figure out how to address my player position now as my FOW (Fog of War) breaks it. May take a break from it and work on a other aspect though first.
The downside about this save/load is I cant use it with my FOW. At least not the way I came up with to do the FOW, basically I have the events generate all the black objects to cover the map as the player walks through and collides with them it removes them. I do have a little delay n loading up each level now as a result. But if I were to save all those with the Save/Load it takes a very long time and if causes crashes. I know their was a tutorial on Terrain destruction I just found, so maybe I will go through that and redesign the FOW system later.
But for now I need to work on having user save a game by custom name and load up through a clickable menu. I’m glad you figured out your save/load issue, but I concur, its way to hard to get working.