Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BowsetteGamer on Thu 02/02/2023 03:50:06

Title: Single save state
Post by: BowsetteGamer on Thu 02/02/2023 03:50:06
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
Title: Re: Single save state
Post by: Snarky on Thu 02/02/2023 06:46:15
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).
Title: Re: Single save state
Post by: Khris on Thu 02/02/2023 08:50:11
Use this to save:
  SaveGameSlot(1, "Autosave");(the description doesn't matter)

To restore, use:
  if (Game.GetSaveSlotDescription(1) != null) RestoreGameSlot(1);
Title: Re: Single save state
Post by: AndreasBlack on Thu 02/02/2023 09:33:32
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?

Title: Re: Single save state
Post by: Crimson Wizard on Thu 02/02/2023 09:41:54
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.
Title: Re: Single save state
Post by: Khris on Thu 02/02/2023 10:21:32
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.
Title: Re: Single save state
Post by: Snarky on Thu 02/02/2023 10:44:01
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() (https://adventuregamestudio.github.io/ags-manual/Globalfunctions_Event.html#on_event), inside a check if(data==eEventRestoreGame).
Title: Re: Single save state
Post by: eri0o on Thu 02/02/2023 12:49:46
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.
Title: Re: Single save state
Post by: BowsetteGamer on Thu 02/02/2023 16:31:55
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
Title: Re: Single save state
Post by: BowsetteGamer on Thu 02/02/2023 16:38:22
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
Title: Re: Single save state
Post by: Khris on Thu 02/02/2023 18:39:52
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:

  if (keycode == eKeyF5) SaveGameSlot(1, "main save");
Edit: fixed wording
Title: Re: Single save state
Post by: BowsetteGamer on Fri 03/02/2023 02:04:16
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:

  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
Title: Re: Single save state
Post by: Matti on Fri 03/02/2023 08:42:06
What does the error message say?
Title: Re: Single save state
Post by: BowsetteGamer on Sat 04/02/2023 07:14:53
Quote from: Matti on Fri 03/02/2023 08:42:06What does the error message say?
(https://i.postimg.cc/L6WxxCpD/Captura.png) (https://postimages.org/)
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?
Title: Re: Single save state
Post by: Snarky on Sat 04/02/2023 08:23:26
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.
Title: Re: Single save state
Post by: Khris on Sat 04/02/2023 09:57:31
You can add a hotkey to on_key_press:

  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.
Title: Re: Single save state
Post by: BowsetteGamer on Sun 05/02/2023 04:34:07
Quote from: Khris on Sat 04/02/2023 09:57:31You can add a hotkey to on_key_press:

  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?
Title: Re: Single save state
Post by: BowsetteGamer on Sun 05/02/2023 04:36:38
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?
Title: Re: Single save state
Post by: 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>.
Title: Re: Single save state
Post by: BowsetteGamer on Sun 05/02/2023 09:09:50
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
Title: Re: Single save state
Post by: BowsetteGamer on Sun 05/02/2023 10:08:18
Good guys thank you all very much for your help, I don't know if I should say this here. My game is already finished in its demo version, but I will be constantly making updates to it, I share them with you so that you can take a look and tell me what you think of the game. Any bugs, suggestions or comments please let me know, I'll be very grateful guys and so I can continue to improve my game, Thank you all. Currently I have problems uploading the 3 images of the game, can you tell me how to do it? They are in PNG but the images do not upload

https://www.adventuregamestudio.co.uk/site/games/game/2659-ultimalatin-demo-fangame/
Title: Re: Single save state
Post by: glurex on Mon 06/02/2023 19:51:35
Quote from: Snarky on Thu 02/02/2023 10:44:01I 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() (https://adventuregamestudio.github.io/ags-manual/Globalfunctions_Event.html#on_event), inside a check if(data==eEventRestoreGame).

Agree. That's what I do in Another Museum (my MAGS game).

In GlobalScript I create a bool

bool QuitTheGame = false;
Then in Repeatedly Execute Always I put:

if (QuitTheGame)
      {
        QuitGame(0);       
       }

And made a function on event:

function on_event(EventType event, int data)
{
  if (event == eEventRestoreGame)
  {
    gQuit.Visible = false;  //My quit the game GUI
    QuitTheGame = false;
  }
}

And last in the On Click script of the button of "Quit" in my quit GUI:

 gQuit.Visible = false;
      QuitTheGame = true;
      SaveGameSlot(1, "AutoSave");