Save your game on exit... Can it be done?

Started by RetroJay, Thu 06/11/2014 02:20:48

Previous topic - Next topic

RetroJay

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.

Vincent

#1
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: ags

Void on_event (EventType event, int data)  // DATA = room number they are leaving

if (event == eEventLeaveRoom) //here need for sure to add extra lines
{
  //called just after room Player Leaves Room event is run.
  //maybe you can switch the player in a black room just before quit the game..

  /*
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  int i = 0;
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
    }
    i++;
  }
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  Wait(1);
  QuitGame(0);
  */

  SaveGameSlot(1, "Saved Game");
}


Forgive me, but maybe I misunderstood, probably.
Greet.

RetroJay

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.

Crimson Wizard

#3
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).

Vincent

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

#5
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. 

Vincent

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: ags

//HEADER
import bool CheckpointExist();
import void SaveCheckpoint();
import void RestoreCheckpoint();
import function StepRegion(int reg);
import function ExploringRegion(Region*r);

//GLOBAL
#define CHECKPOINT_SAVESLOT	5    
// How many you wish, 
// I am one of those people who prefer one state over to save the game

bool SafeArea = false, First_time = true;

int DangerMode = 0, oldregion; //globally would be nice
// 0 = No Danger Mode
// 1 = In Danger Mode

bool CheckpointExist()
{
  return (Game.GetSaveSlotDescription(CHECKPOINT_SAVESLOT) != null);
}

void SaveCheckpoint()
{
  SaveGameSlot(CHECKPOINT_SAVESLOT, "Checkpoint");
}

void RestoreCheckpoint()
{
  if (CheckpointExist()) RestoreGameSlot(CHECKPOINT_SAVESLOT);
}



function StepRegion(int reg) {
  if (reg > 0) { // walk on safe area
    if (player.Room == 1 && reg == 1 ) //extra lines to add 'First_time' or 'SafeArea' 
    {
       SafeArea = true;
       SaveCheckpoint()
       Wait(1);
       First_time = false;
    }
  }
  else { // walk off the safe area
    reg = -reg;
    if (player.Room == 1 && reg == 1) //extra lines to add 'First_time' or 'SafeArea' 
    {
       SafeArea = false; 
    }
  }
}


function ExploringRegion(Region*r) {
  int currentregion = r.ID;
  if (currentregion != oldregion) {
    if (oldregion != 0 && region[oldregion].Enabled) StepRegion(-oldregion);
    if (currentregion != 0 && region[currentregion].Enabled) StepRegion(currentregion);
    oldregion = currentregion;
  }
}


function repeatedly_execute_always()
{
  // ExploringRegion(Region.GetAtRoomXY(player.x, player.y));
}



Salute

RetroJay

#7
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.


RetroJay

#8
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: ags
// VINCENT'S SAVE GAME SCRIPT.
//----------------------------
#define CHECKPOINT_SAVESLOT     5    
// How many you wish, 
// I am one of those people who prefer one state over to save the game

bool SafeArea = false, First_time = true;

int DangerMode = 0, oldregion; //globally would be nice
// 0 = No Danger Mode
// 1 = In Danger Mode


bool CheckpointExist()
{
  return (Game.GetSaveSlotDescription(CHECKPOINT_SAVESLOT) != null);
}


void SaveCheckpoint()
{
  SaveGameSlot(CHECKPOINT_SAVESLOT, "Checkpoint");
}


void RestoreCheckpoint()
{
  if (CheckpointExist()) RestoreGameSlot(CHECKPOINT_SAVESLOT);
}

 
function StepRegion(int reg) {
  if (reg > 0) { // walk on safe area
    if (player.Room == 3 && reg == 3 ) //extra lines to add 'First_time' or 'SafeArea' 
    {
       SafeArea = true;
       Display("Game Saved");
       SaveCheckpoint();
       Wait(1);
       First_time = false;
    }
  }
  else { // walk off the safe area
    reg = -reg;
    if (player.Room == 3 && reg == 3) //extra lines to add 'First_time' or 'SafeArea' 
    {
       SafeArea = false; 
    }
  }
}

 
function ExploringRegion(Region*r) {
  int currentregion = r.ID;
  if (currentregion != oldregion) {
    if (oldregion != 0 && region[oldregion].Enabled) StepRegion(-oldregion);
    if (currentregion != 0 && region[currentregion].Enabled) StepRegion(currentregion);
    oldregion = currentregion;
  }
}


 function repeatedly_execute() {  // This was the only thing I had to change. It wouldn't compile within 'Rep_Exc_Always' but seems fine here.
  // put anything you want to happen every game cycle in here.
  ExploringRegion(Region.GetAtRoomXY(player.x, player.y));
}
// END OF VINCENT'S SAVE GAME SCRIPT.
//-----------------------------------


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: ags
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.

Vincent

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)

Quote from: RetroJay on Fri 07/11/2014 22:15:37
Also, the line in 'Repeatedly_Execute_Always'

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

Quote from: RetroJay on Fri 07/11/2014 22:15:37
the game auto saves and only makes one save that keeps getting overwritten.

I really do love those kind of games.

Quote from: RetroJay on Fri 07/11/2014 22:15:37
Code: ags
 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

SMF spam blocked by CleanTalk