Jibble

Author Topic: Save your game on exit... Can it be done?  (Read 1134 times)

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Save your game on exit... Can it be done?
« on: 06 Nov 2014, 02:20 »
Hi All Good Peeps of AGS.

I hope you are all well.

What I would like to implement within my game is a 'Save Game' feature that only occurs when you exit the game.
For those of you that have played 'Hidden Objects' games, then I trust you know what I mean.

For People who haven't...
When you exit the game then it is saved automatically at the point in witch you left.

Is there a way of accomplishing this or am I aiming too high here?

Yours.
Jay.
« Last Edit: 10 Nov 2014, 22:16 by RetroJay »
  Master Decorator

Vincent

  • Posts: 338
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
Re: Save your game on exit... Can it be done?
« Reply #1 on: 06 Nov 2014, 03:02 »
I'm not entirely sure of this kind of games, but I'd like to help out even if i'm not good at all,

but wonder if it can't be done (for example) like this,

Code: Adventure Game Studio
  1. Void on_event (EventType event, int data)  // DATA = room number they are leaving
  2.  
  3. if (event == eEventLeaveRoom) //here need for sure to add extra lines
  4. {
  5.   //called just after room Player Leaves Room event is run.
  6.   //maybe you can switch the player in a black room just before quit the game..
  7.  
  8.   /*
  9.   int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  10.   int i = 0;
  11.   while (i < lstSaveGamesList.ItemCount)
  12.   {
  13.     if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
  14.     {
  15.       gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
  16.     }
  17.     i++;
  18.   }
  19.   SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  20.   Wait(1);
  21.   QuitGame(0);
  22.   */
  23.  
  24.   SaveGameSlot(1, "Saved Game");
  25. }
  26.  

Forgive me, but maybe I misunderstood, probably.
Greet.
« Last Edit: 06 Nov 2014, 03:17 by Vincent »

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Save your game on exit... Can it be done?
« Reply #2 on: 06 Nov 2014, 03:41 »
Hi Vincent.

Nice to hear from you.

Thanks for your help.
I will have to try what you have suggested tomorrow, or as it is today, tonight. ;)

I will let you know how things went.

Much appreciated.

Yours.
Jay.
  Master Decorator

Crimson Wizard

  • Posts: 8,794
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    • Lifetime Achievement Award Winner
    • Crimson Wizard worked on a game that won an AGS Award!
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
Re: Save your game on exit... Can it be done?
« Reply #3 on: 06 Nov 2014, 08:25 »
You can save your game if you detect player clicked on your own "Quit" button in game menu.
However, you won't be able to do anything if player closes the window by clicking on X button on window title, nor when he presses Alt + F4 (standard Windows combination for closing application), because AGS does not allow to handle such events in script.

This problem is in TODO list:
http://www.adventuregamestudio.co.uk/forums/index.php?issue=612.0

You can use a workaround saving the game at the point of entering and/or leaving a room, like Vincent suggested, if this is enough for you (e.g. if you want player return to the last room he has been, but do not need to restore precise room state).
« Last Edit: 06 Nov 2014, 08:29 by Crimson Wizard »

Vincent

  • Posts: 338
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
Re: Save your game on exit... Can it be done?
« Reply #4 on: 06 Nov 2014, 13:03 »
Hi RetroJay.

It was a pleasure even though it was not much.
But thanks, let me know if it can work.


In the past I had seen the topic that Crimson Wizard shared before.
Would be a wonderful thing to add an event in the function (on_event) like eEventOnExit
or a command that would say that the game is being to leave..

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Save your game on exit... Can it be done?
« Reply #5 on: 06 Nov 2014, 22:44 »
Hi Crimson Wizard and Vincent.

I have decided to go down a different path and make a save point instead.:-\
Unfortunately, I do need the game to restore precise room state as I have a lot of objects and items that change state throughout.

You see the game I am trying to create at the moment is a platform/adventure.
After much deliberation I felt it would be better to have a save point that is in a safe area where your character cannot be harmed.
When you load the game you will then be in that safe area, ready to continue.

If the game was to save on exit then when you load that save in you might be in an area where you cannot escape being harmed. (that wouldn't be good.)

Crimson Wizard, I totally agree that a 'Game exit function' or something along those lines would be a very good idea.(nod)

Vincent, I have copied your script to my, ever growing, book of useful scripts.
It may come in handy if I change my mind... again.(roll)

Thank you ever so much for your help it is much appreciated.

Yours.
Jay. 
« Last Edit: 06 Nov 2014, 22:50 by RetroJay »
  Master Decorator

Vincent

  • Posts: 338
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
Re: Save your game on exit... Can it be done?
« Reply #6 on: 07 Nov 2014, 13:13 »
I think I understand what would you have in mind to do ..
I deliberately wrote an idea to be able to help you further ..
I had the day off today because it's raining :-D


Code: Adventure Game Studio
  1. //HEADER
  2. import bool CheckpointExist();
  3. import void SaveCheckpoint();
  4. import void RestoreCheckpoint();
  5. import function StepRegion(int reg);
  6. import function ExploringRegion(Region*r);
  7.  
  8. //GLOBAL
  9. #define CHECKPOINT_SAVESLOT     5    
  10. // How many you wish,
  11. // I am one of those people who prefer one state over to save the game
  12.  
  13. bool SafeArea = false, First_time = true;
  14.  
  15. int DangerMode = 0, oldregion; //globally would be nice
  16. // 0 = No Danger Mode
  17. // 1 = In Danger Mode
  18.  
  19. bool CheckpointExist()
  20. {
  21.   return (Game.GetSaveSlotDescription(CHECKPOINT_SAVESLOT) != null);
  22. }
  23.  
  24. void SaveCheckpoint()
  25. {
  26.   SaveGameSlot(CHECKPOINT_SAVESLOT, "Checkpoint");
  27. }
  28.  
  29. void RestoreCheckpoint()
  30. {
  31.   if (CheckpointExist()) RestoreGameSlot(CHECKPOINT_SAVESLOT);
  32. }
  33.  
  34.  
  35.  
  36. function StepRegion(int reg) {
  37.   if (reg > 0) { // walk on safe area
  38.     if (player.Room == 1 && reg == 1 ) //extra lines to add 'First_time' or 'SafeArea'
  39.     {
  40.        SafeArea = true;
  41.        SaveCheckpoint()
  42.        Wait(1);
  43.        First_time = false;
  44.     }
  45.   }
  46.   else { // walk off the safe area
  47.     reg = -reg;
  48.     if (player.Room == 1 && reg == 1) //extra lines to add 'First_time' or 'SafeArea'
  49.     {
  50.        SafeArea = false;
  51.     }
  52.   }
  53. }
  54.  
  55.  
  56. function ExploringRegion(Region*r) {
  57.   int currentregion = r.ID;
  58.   if (currentregion != oldregion) {
  59.     if (oldregion != 0 && region[oldregion].Enabled) StepRegion(-oldregion);
  60.     if (currentregion != 0 && region[currentregion].Enabled) StepRegion(currentregion);
  61.     oldregion = currentregion;
  62.   }
  63. }
  64.  
  65.  
  66. function repeatedly_execute_always()
  67. {
  68.   // ExploringRegion(Region.GetAtRoomXY(player.x, player.y));
  69. }
  70.  


Salute

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Save your game on exit... Can it be done?
« Reply #7 on: 07 Nov 2014, 20:18 »
Hi Vincent.

That's fantastic.8-0

What a thoroughly decent guy you are.(nod)
That's as good a reason, as I can think of, for taking a day off.(laugh)

Thank you ever so much for taking the time to do this for me.
I think your script may be just what I need.

Before I can try it, though, I need to make a save and load gui box.
Also finish some other areas before I implement your code.
I will let you know how things go.

Once again, Thank you for your help, it is really appreciated.;-D

Yours.
Jay.

« Last Edit: 07 Nov 2014, 20:35 by RetroJay »
  Master Decorator

RetroJay

  • Posts: 537
  • Frankly, my dear, I'm a Kerbal and Proud of it.
    • RetroJay worked on a game that was nominated for an AGS Award!
Re: Save your game on exit... Can it be done?
« Reply #8 on: 07 Nov 2014, 22:15 »
Hi Vincent.

I decided to test your code out.
It seems to work beautifully.;-D

The only things I changed were the region and room numbers.
My game actually starts in room 3 and the Checkpoint region is 3 (by some strange coincidence.)
Also, the line in 'Repeatedly_Execute_Always' I had to put it in 'Repeatedly_Execute' instead (the game wouldn't compile, otherwise.)

Other than that, Fantastic.;-D

Have a check of what I have below and see if you can see anything I have done wrong.

Code: Adventure Game Studio
  1. // VINCENT'S SAVE GAME SCRIPT.
  2. //----------------------------
  3. #define CHECKPOINT_SAVESLOT     5    
  4. // How many you wish,
  5. // I am one of those people who prefer one state over to save the game
  6.  
  7. bool SafeArea = false, First_time = true;
  8.  
  9. int DangerMode = 0, oldregion; //globally would be nice
  10. // 0 = No Danger Mode
  11. // 1 = In Danger Mode
  12.  
  13.  
  14. bool CheckpointExist()
  15. {
  16.   return (Game.GetSaveSlotDescription(CHECKPOINT_SAVESLOT) != null);
  17. }
  18.  
  19.  
  20. void SaveCheckpoint()
  21. {
  22.   SaveGameSlot(CHECKPOINT_SAVESLOT, "Checkpoint");
  23. }
  24.  
  25.  
  26. void RestoreCheckpoint()
  27. {
  28.   if (CheckpointExist()) RestoreGameSlot(CHECKPOINT_SAVESLOT);
  29. }
  30.  
  31.  
  32. function StepRegion(int reg) {
  33.   if (reg > 0) { // walk on safe area
  34.     if (player.Room == 3 && reg == 3 ) //extra lines to add 'First_time' or 'SafeArea'
  35.     {
  36.        SafeArea = true;
  37.        Display("Game Saved");
  38.        SaveCheckpoint();
  39.        Wait(1);
  40.        First_time = false;
  41.     }
  42.   }
  43.   else { // walk off the safe area
  44.     reg = -reg;
  45.     if (player.Room == 3 && reg == 3) //extra lines to add 'First_time' or 'SafeArea'
  46.     {
  47.        SafeArea = false;
  48.     }
  49.   }
  50. }
  51.  
  52.  
  53. function ExploringRegion(Region*r) {
  54.   int currentregion = r.ID;
  55.   if (currentregion != oldregion) {
  56.     if (oldregion != 0 && region[oldregion].Enabled) StepRegion(-oldregion);
  57.     if (currentregion != 0 && region[currentregion].Enabled) StepRegion(currentregion);
  58.     oldregion = currentregion;
  59.   }
  60. }
  61.  
  62.  
  63.  function repeatedly_execute() {  // This was the only thing I had to change. It wouldn't compile within 'Rep_Exc_Always' but seems fine here.
  64.   // put anything you want to happen every game cycle in here.
  65.   ExploringRegion(Region.GetAtRoomXY(player.x, player.y));
  66. }
  67. // END OF VINCENT'S SAVE GAME SCRIPT.
  68. //-----------------------------------

Just thinking to myself and...
I'm only going to need a 'Load' GUI, aren't I?
As my game now saves automatically then I don't need a 'Save' GUI.

EDIT:
Thinking some more. (This is very dangerous.)(laugh)
I decided that I didn't need a 'Load' GUI, either.
Let's face it, the game auto saves and only makes one save that keeps getting overwritten.
So all I did was add this line
Code: Adventure Game Studio
  1. if (keycode == eKeyL) RestoreCheckpoint(); // Pressing L key restores the saved Checkpoint.
to 'function on_key_press(eKeyCode keycode)'

It all works like a dream.

Thank you ever so much for your time, Vincent.

Yours.
Jay.
« Last Edit: 07 Nov 2014, 22:49 by RetroJay »
  Master Decorator

Vincent

  • Posts: 338
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
Re: Save your game on exit... Can it be done?
« Reply #9 on: 08 Nov 2014, 01:52 »
Hi RetroJay. :)

I'm just happy to being able to lend a hand, really.
I have been silly, because I didn't testing it before sharing with you. (roll)

Also, the line in 'Repeatedly_Execute_Always'

In 'Repeatedly_execute' is more than adequate and healthy. ;-D

the game auto saves and only makes one save that keeps getting overwritten.

I really do love those kind of games.

Code: Adventure Game Studio
  1.  if (keycode == eKeyL) RestoreCheckpoint(); // Pressing L key restores the saved Checkpoint.

And the last piece of code is graceful and smart.
Very well done over there. (nod)
Feel free to send me a message whenever you want.

Happy Twice.
Ciao Ciao