Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mugs on Thu 16/03/2006 06:53:55

Title: Continue script on mouse click (SOLVED)
Post by: Mugs on Thu 16/03/2006 06:53:55
Is there a way to pause the script until the player or user clicks?

I am looking for a function similar to WaitMouseKey, without the timeloops.
Title: Re: Continue script on mouse click
Post by: Khris on Thu 16/03/2006 07:29:49
Try this:

function on_mouse_click(MouseButton button) {
Ã,  if (IsGamePaused()) {
Ã,  Ã,  if (button==eMouseLeft) UnPauseGame();
Ã,  }
Ã,  else {
Ã,  Ã,  ...
Ã,  }
}
Then, whenever you want to pause the script, call PauseGame();

If that's not enough, use this:
while (!Mouse.IsButtonDown(eMouseLeft));
and disable the while check first which ends the game when the while loop reaches 150'000 iterations. (possible since 2.71 beta 4).
Title: Re: Continue script on mouse click
Post by: Mugs on Thu 16/03/2006 08:03:33
There's just one problem. PauseGame doesn't pause the scripts.

For example,


gDisplay.visible = true;
PauseGame();
gDisplay.visible = false;


Even if the game is "paused", the script will go on and close gDisplay.

I want the game to await the player's click before closing gDisplay.
Title: Re: Continue script on mouse click
Post by: Gilbert on Thu 16/03/2006 08:35:44
Well KhrisMUS's second suggestion should "sort of" work, as it missed some important thing:

while (!Mouse.IsButtonDown(eMouseLeft)) Wait(1);

(and you should disable teh while loop check as mentioned, or else the game will be terminated after 150000 or so loops, which would be like, er... an hour under default game speed of 40FPS).

Title: Re: Continue script on mouse click
Post by: Khris on Thu 16/03/2006 08:39:06
Like this:
function noloopcheck wait_click() {
Ã, while (!mouse.IsButtonDown(eMouseLeft));
}


Is the Wait(1); really necessary?
Title: Re: Continue script on mouse click
Post by: Gilbert on Thu 16/03/2006 09:04:10
Yes, very important, otherwise you'll be stuck in the same game frame.
Title: Re: Continue script on mouse click
Post by: Khris on Thu 16/03/2006 10:34:09
So mouse.IsButtonDown() is only updated every other frame?
Good to know, I guess this is the case for all (similar) functions?
Title: Re: Continue script on mouse click
Post by: Mugs on Thu 16/03/2006 19:39:07
I'm not quite sure where this is going.Ã,  What does (!Mouse.IsButtonDown(eMouseLeft)) Wait(1); have to do with what I'm trying to do?Ã,  The function probably makes sense , but I don't know what to do with it.

Basically, I'm having a GUI appear with a label inside it. I want so that every time the user clicks on the screen, the label changes it's caption.
I want to be able to do this multiple times in the game.


gDisplay.Visible = true;
Labelname.SetText("Hello.");
//Then the user clicks
Labelname.SetText("My name is Joe.");
//Then the user clicks
Labelname.SetText("I like Jell-O.");
//Then the user clicks
gDisplay.Visible = false;


Title: Re: Continue script on mouse click
Post by: Khris on Thu 16/03/2006 19:47:55
The function calls Wait(1); until mouse.IsButtonDown(eMouseLeft) equals true i. e. the player presses the left mouse button.

So, put this somewhere in the global script:
function noloopcheck wait_click() {
Ã,  while (!mouse.IsButtonDown(eMouseLeft)) Wait(1);
}


Import the function in the script header:
import function wait_click();

Then call it everytime you want the game to pause until the player clicks:

gDisplay.Visible = true;
Labelname.SetText("Hello.");
wait_click();
Labelname.SetText("My name is Joe.");
wait_click();
Labelname.SetText("I like Jell-O.");
wait_click();
gDisplay.Visible = false;
Title: Re: Continue script on mouse click
Post by: Mugs on Thu 16/03/2006 22:35:18
Almost!

If I click once, the GUI turns off.Ã,  The game executes all three wait_clicks at the same time.

Title: Re: Continue script on mouse click
Post by: Gilbert on Fri 17/03/2006 01:43:49
Quote from: KhrisMUC on Thu 16/03/2006 10:34:09
So mouse.IsButtonDown() is only updated every other frame?

I don't know, probably not, but you SHOULD add wait to it, otherwise the while loops will be stuck in the same game loop that may cause slowdowns and other problem (most possibly freeze your game as the screen won't update during it, and background fucntions like rep_exe_always() won't run).
Title: Re: Continue script on mouse click (SOLVED)
Post by: Mugs on Fri 17/03/2006 02:09:54
Quote from: Mugs on Thu 16/03/2006 22:35:18
Almost!

If I click once, the GUI turns off.Ã,  The game executes all three wait_clicks at the same time.



Solved it!Ã,  I inserted...

function noloopcheck wait_unclick() {
Ã,  while (mouse.IsButtonDown(eMouseLeft)) Wait(1);
}

...in the global script.

Then I did the following:


gDisplay.Visible = true;
Labelname.SetText("Hello.");
wait_click();
Labelname.SetText("My name is Joe.");
wait_unclick();
wait_click();
Labelname.SetText("I like Jell-O.");
wait_unclick();
wait_click();
gDisplay.Visible = false;


Thanks for all the help!!!!
Title: Re: Continue script on mouse click
Post by: Khris on Fri 17/03/2006 04:51:01
Sorry, I was tired :)
Just change the original function:
function noloopcheck wait_click() {
Ã,  while (!mouse.IsButtonDown(eMouseLeft)) Wait(1);
Ã,  while (mouse.IsButtonDown(eMouseLeft)) Wait(1);
}


You won't need wait_unclick then.
Title: Re: Continue script on mouse click (SOLVED)
Post by: Gilbert on Fri 17/03/2006 06:32:29
Why's the second while needed? So the wait will end when the button is released?
Title: Re: Continue script on mouse click (SOLVED)
Post by: Khris on Fri 17/03/2006 07:06:35
Yes, otherwise
QuoteAlmost!

If I click once, the GUI turns off.Ã,  The game executes all three wait_clicks at the same time.