Restarting the game

Started by HandsFree, Tue 29/10/2013 13:20:18

Previous topic - Next topic

HandsFree

I know there are many threads about this but I still can't get it right. :(

The game starts in room 9 wich is just an empty room with a gui showing the main menu. When you click on 'new game' you're supposed to see room 10 where the main character walks in followed by a display message.
This works the first time but I don't understand how to get it working a next time. I did see the RestartPoint cannot be set while the script is running, but how to get this right...?

I have:
Code: ags

function btnNew_OnClick(GUIControl *control, MouseButton button)
{
  if (HasPlayerBeenInRoom(10)) RestartGame();
  else {
    cEgo.ChangeRoom(10);
    gMainMenu.Visible = false;
  }
}

function room_FirstLoad()// room 10
{
  SetRestartPoint();
  cEgo.Walk(150, 340, eBlock, eWalkableAreas);
  Wait(40);
  lblText.Text = "Text at the start of the game.";
  gText.Visible = true;
} 


clicking 'new game' for the 2nd time now lets the game start with the text display without showing the walking (cEgo is placed at 150, 340 though).
I tried using on_event with if (event == eEventRestoreGame && data == 999) but what ever I put there is done after the message display.

Any ideas how to get the restart right?
thanks

Khris

Quote from: manualNOTE: The restart point cannot be set while a script is running -- therefore, when you call this it will actually set the restart point at the next game loop where there is not a blocking script running in the background.

This should do the trick:
Code: ags
function room_FirstLoad()// room 10
{
  SetTimer(1, 2);
  SetRestartPoint();
}

function firstLoadCont() {
  cEgo.Walk(150, 340, eBlock, eWalkableAreas);
  Wait(40);
  lblText.Text = "Text at the start of the game.";
  gText.Visible = true;
}

// in room's rep_exec

  if (IsTimerExpired(1)) firstLoadCont();


Btw, instead of lines 10 & 11, you should use a custom function:
Code: ags
function ShowText(String message) {
  lblText.Text = message;
  gText.Visible = true;
  // wait for mouse key, turn off gui? 
}

// calling it:
  ShowText("Text at the start of the game.");

HandsFree

Yes it works. ;-D
Don't really understand why though. Do I need this to make sure it's different game loops?

Khris

#3
What happens is (I presume)

1st loop:
-timer is set to expire two loops from now
-saving to game slot 999 is queued
2nd loop:
-game is saved to slot 999
3rd loop:
-timer expires, intro is played

When the save game is loaded by restarting the game, the timer is already running and will expire in the very next loop, starting the intro

Maybe there's an easier way, but doing it without something like this will only work in situations where the player is supposed to have control right after restarting.

SMF spam blocked by CleanTalk