Help with saving game state

Hi everyone,

I’m not quit sure how to save the game state of a game. I have followed the tutorials on Gdevelop and this is what I can up with

Save%202

If the player exists the game at level 2, position x,when the player returns they should be at level 2, position x.

I used the save button, I don’t know if this is necessary or there’s an easier way to do it.

I don’t understand what exactly is the problem. ^^;; What part of that isn’t working? I can’t spot any obvious errors, and also, your explanation doesn’t make very clear what the problem is. I’d love to help once I know what you need help with!

Hi,

If there is nothing wrong with the coding then that’s alright.

I wanted the game state to be saved. So if the player exists the game at a certain level, say level 3, when they return to play the game, the game should resume at level 3. I wasn’t so sure about what I did and if it is correct.

I included a save button so that when it is pressed the game state is saved. I don’t know if this is necessary to include or if I can remove it. If I remove the save button would I have to change the coding or can I just leave it as it is.

I hope I’ve explained my problem well.

Yes, technically if you remove the “Save” button from the scene (but don’t delete the object itself from the editor) the events tied to the object will still be there, with the difference that it won’t be clickable, as it’s no longer in the scene. ^v^;;

And if you want to know if there’s a way to save it without needing the button, yes! You can trigger the “Save” events without a button - with variables, for example! But for that you WOULD need to change up the Events a little bit.
For example, if you want the game to Auto-Save, you could use a Timer and a variable, like:

If value of timer "AutoSave" is greater than 10 seconds
  do = 1 to scene variable "SaveNow"
  reset the timer "AutoSave"

If scene variable "SaveNow" = 1
Trigger once
  do (insert your tree of Save events here)
  do = 0 to scene variable "SaveNow"

This way, the “Save” events would be triggered by a variable instead of by the button, so you would no longer need the button in the scene. ^^; It can be triggered in many other ways, too. It’s really up to you to decide what triggers it!

I hope this is what you mean? I’m sorry if it isn’t!

Hi,

I have opted to remove the save button and use the timer instead. I replay the “save” section of my code (see below) with the Auto Save code you wrote above.

I’m not sure if I did the coding right. Here’s a screen shot of what I did

I also deleted the “save” button from the objects panel. Was I right to replace the coding under “save” and replace it with the code for “Auto Save”?

I didn’t delete the " Initialize" section of my code. Should I delete it?Save%201

Thanks for your help.

I’m not sure if I did the coding right. Here’s a screen shot of what I did

I don’t really understand what you did in the second screenshot. ^^;; It seems you’re resetting the position of Player, but not saving anything. The “Do = x, y to the position of Character” is not storing information, just sending your character all the way back to the beginning position.

If you want to write the X position and Y position into the Storage, you need the “Write” actions.
They can be found in “Storage > Write a value”.
Do you see that “Repeat for each Character object” sub-event in the first screenshot? That’s what is writing the information into the Storage file. Without it, you aren’t saving anything.
When SaveNow = 1 in your code, all it does is READ the position from the variable, but not actually save it as it should - because you didn’t add the proper actions.

Drag the “Repeat for each character object” sub-event I just mentioned - the one you see in the first screenshot - and put it below the “If scene variable SaveNow = 1” event, as a sub-event. That should solve the problem.

Also, there’s no need for the “Do = Variable(posX); Variable(posY) to the position of character” there either. You would just be sending the player all the way back to the first-saved position, if not to position 0;0. This is the opposite of what you want, it will be like restarting the game every 10 seconds.

Instead, the actions should be something like:

If scene variable "SaveNow" is = 1
Trigger once
  do = 0 to scene variable "SaveNow"
  For each "Character" object (subevent) 
    write character.X() in "characterX" + ToString(character.Variable(ID)) of storage "save"
    write character.Y() in "characterY" + ToString(character.Variable(ID)) of storage "save"

This should be basically it. ^v^;; It will properly write what you need in the storage.

Also, no, don’t delete ALL of the “Initialize” section - one of these sections are LOADING information, not saving. It has nothing to do with the “Save” button. I mean, you can, if you want to make your own custom way of loading the scene, but I don’t see a need to.
You can feel free to toggle the events tied to the “Save” button as disabled though, or just straight up delete them if you feel like it. Just don’t delete the entirety of the “Initialize” section unless you’re planning on redoing the loading system too.

I hope this helps.

Hi,

I edited the “AutoSave” section. This is how it looks like now

I hope it’s correct.

seems correct couldn’t tell without running the code bit how many characters do you have? as you do for each loop

1 Like

There are three characters. I’ll test it and let you know but thank you so much for your help.