Single save state

Started by BowsetteGamer, Thu 02/02/2023 03:50:06

Previous topic - Next topic

BowsetteGamer

Guys can you help me? I have a question and I don't understand the manual very well. My idea is to create a single game state, in which the game can be saved and restored, but I don't really know how to do it, can someone help me, please? It's a game without a template, blank. The game is ready to publish its demo but it only lacks that

Snarky

#1
Since this is a new question, unrelated to your previous one, I have split it into a new thread.

Do you mean that instead of having the option to manually save the game in multiple slots, there should only be a single savegame, and that the game should autosave its state, and that the next time the game starts after quitting, it should automatically continue from that save, so that players never have to (and can't) manually save/restore?

You kind of just have to... do it.

-Remove the code that lets players bring up the save and load menus
-Pick a savegame slot that will be your autosave
-Make sure to save to this slot at suitable times, e.g. when the player enters a room, when a conversation ends, and of course, when quitting
-From the main game menu, have a "continue" option that loads the savegame

That's the basic idea. Then you can add some polish (e.g. to make sure that when you load, you don't bring up the "quit?" menu straight away just because that was your last autosave).

Khris

Use this to save:
Code: ags
  SaveGameSlot(1, "Autosave");
(the description doesn't matter)

To restore, use:
Code: ags
  if (Game.GetSaveSlotDescription(1) != null) RestoreGameSlot(1);

AndreasBlack

As for auto-saving do you just call the SaveGameSlot(1, "Autosave"); in a repeatedly execute function or is there additional code that needs to be done for an autosave feature?


Crimson Wizard

#4
Quote from: AndreasBlack on Thu 02/02/2023 09:33:32As for auto-saving do you just call the SaveGameSlot(1, "Autosave"); in a repeatedly execute function or is there additional code that needs to be done for an autosave feature?

The code should follow game design. First of all, you decide when you want your game to autosave. If you really prefer it autosave each game frame (40+ times a second), then sure put it in repeatedly execute function. If you like it to autosave in particular moments only, then you'd need to code that accordingly.

Khris

To be clear: do not put a SaveGameSlot() call directly inside repeatedly_execute :P
I'd be afraid to even just test this.

Depending on how your game works, suitable events for auto-saving are:
- each time the player enters a new room
- each time the player picks up new inventory
- each time the game's plot moves forward (i.e. the global game state changes)

The first two can be trivially solved using on_event, the third requires a manual call.

Snarky

I would also add:

-each time the player quits/exits to main menu/brings up the main menu

The only little wrinkle is that you should then ensure that when the game is loaded, it doesn't start off with those menus open. You can do this by closing them in on_event(), inside a check if(data==eEventRestoreGame).

eri0o

Quote from: Khris on Thu 02/02/2023 10:21:32- each time the game's plot moves forward (i.e. the global game state changes)

I think this is the correct and best single place for auto saving. Make a function for your auto save and call it everytime the plot moves forward.

BowsetteGamer

Quote from: Snarky on Thu 02/02/2023 06:46:15Since this is a new question, unrelated to your previous one, I have split it into a new thread.

Do you mean that instead of having the option to manually save the game in multiple slots, there should only be a single savegame, and that the game should autosave its state, and that the next time the game starts after quitting, it should automatically continue from that save, so that players never have to (and can't) manually save/restore?

You kind of just have to... do it.

-Remove the code that lets players bring up the save and load menus
-Pick a savegame slot that will be your autosave
-Make sure to save to this slot at suitable times, e.g. when the player enters a room, when a conversation ends, and of course, when quitting
-From the main game menu, have a "continue" option that loads the savegame

That's the basic idea. Then you can add some polish (e.g. to make sure that when you load, you don't bring up the "quit?" menu straight away just because that was your last autosave).
Thanks snarky, I was going to ask in a new thread, the fact is that I didn't want to fill the forum with 2,345,234 questions and that's why I asked in the one that I would have created, well... basically it's for the player to save a state and load a state manually without autosave, the game has nothing to save state or load state, that is, there are no GUIs

BowsetteGamer

Thanks guys, I really liked that autosave thing, I'll try it in a later game, since I'm not very experienced with the subject of GUIs, and less with save game and load game, what I plan to do is a button that saves a state and another that loads that same state and if the character dies, the GUI to load state and restart appears. This while I document more on this topic of saving and loading

Khris

#10
Use the commands I mentioned in my first reply.

To put saving and restoring on button keyboard keys, add code in the Global Script's on_key_press:

Code: ags
  if (keycode == eKeyF5) SaveGameSlot(1, "main save");

Edit: fixed wording

BowsetteGamer

#11
Quote from: Khris on Thu 02/02/2023 18:39:52Use the commands I mentioned in my first reply.

To put saving and restoring on buttons, add code in the Global Script's on_key_press:

Code: ags
  if (keycode == eKeyF5) SaveGameSlot(1, "main save");

Yes kris i used what you told me about save state and restore state as i got it right thanks to you Kris; I made a GUI with 2 buttons, one to save and one to restore. Kris's case is that when I restore without previously saving the game, it gives me an error. I use this code to restore when clicked.

if (Game.GetSaveSlotDescription(1) != null) RestoreGameSlot(1);

The error that it gives me, I have not taken a capture

Matti

What does the error message say?

BowsetteGamer

#13
Quote from: Matti on Fri 03/02/2023 08:42:06What does the error message say?

It happens when I press the "load state" button from the GUI without saving it first. I have not compiled the game, run it only from the test, not from the compiled .exe.
Is this error normal or should I program some command to prevent this from happening?

Snarky

The error means that the save is incompatible with the current game, because you have made changes to the game project.

Keep in mind that saves persist across each time you run the game, so if you don't save first on this run, it will load a save from the last time you ran the game. And if you've made changes to the game in the mean time, you will get this error.

Khris

You can add a hotkey to on_key_press:

Code: ags
  if (keycode == eKeyBackspace) DeleteSaveSlot(1);

This way you don't have to manually delete the save slot. You can of course also simply overwrite it instead.

BowsetteGamer

Quote from: Khris on Sat 04/02/2023 09:57:31You can add a hotkey to on_key_press:

Code: ags
  if (keycode == eKeyBackspace) DeleteSaveSlot(1);

This way you don't have to manually delete the save slot. You can of course also simply overwrite it instead.
Excellent, and look, can I make it so that when I press the save state button, I delete the previous one and save a new one? I don't get an error if I do it like this?

BowsetteGamer

Quote from: Snarky on Sat 04/02/2023 08:23:26The error means that the save is incompatible with the current game, because you have made changes to the game project.

Keep in mind that saves persist across each time you run the game, so if you don't save first on this run, it will load a save from the last time you ran the game. And if you've made changes to the game in the mean time, you will get this error.
Ah okay I understand why that error appears thanks for explaining it to me Snarky. One question about the saved state where it is located, that is, I save the game but where is that save located?

Crimson Wizard

Quote from: BowsetteGamer on Sun 05/02/2023 04:36:38Ah okay I understand why that error appears thanks for explaining it to me Snarky. One question about the saved state where it is located, that is, I save the game but where is that save located?

On Windows it is located in %USERPROFILE%\Saved Games\<Your game name>.

BowsetteGamer

Quote from: Crimson Wizard on Sun 05/02/2023 08:50:20
Quote from: BowsetteGamer on Sun 05/02/2023 04:36:38Ah okay I understand why that error appears thanks for explaining it to me Snarky. One question about the saved state where it is located, that is, I save the game but where is that save located?

On Windows it is located in %USERPROFILE%\Saved Games\<Your game name>.
Oh ok thank you very much Crimson

SMF spam blocked by CleanTalk