Continue script on mouse click (SOLVED)

Started by Mugs, Thu 16/03/2006 06:53:55

Previous topic - Next topic

Mugs

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.
Cool stuff I found out: Men are four times more likely to be struck by lightning than women.  Wow, really? [dirty joke] Maybe this has to do with the fact that us men have "lightning rods"? [/dirty joke]

Khris

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

Mugs

There's just one problem. PauseGame doesn't pause the scripts.

For example,

Code: ags

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.
Cool stuff I found out: Men are four times more likely to be struck by lightning than women.  Wow, really? [dirty joke] Maybe this has to do with the fact that us men have "lightning rods"? [/dirty joke]

Gilbert

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


Khris

Like this:
function noloopcheck wait_click() {
 Ã, while (!mouse.IsButtonDown(eMouseLeft));
}


Is the Wait(1); really necessary?

Gilbert

Yes, very important, otherwise you'll be stuck in the same game frame.

Khris

So mouse.IsButtonDown() is only updated every other frame?
Good to know, I guess this is the case for all (similar) functions?

Mugs

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.

Code: ags

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;


Cool stuff I found out: Men are four times more likely to be struck by lightning than women.  Wow, really? [dirty joke] Maybe this has to do with the fact that us men have "lightning rods"? [/dirty joke]

Khris

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:
Code: ags
function noloopcheck wait_click() {
Ã,  while (!mouse.IsButtonDown(eMouseLeft)) Wait(1);
}


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


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

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;

Mugs

Almost!

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

Cool stuff I found out: Men are four times more likely to be struck by lightning than women.  Wow, really? [dirty joke] Maybe this has to do with the fact that us men have "lightning rods"? [/dirty joke]

Gilbert

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

Mugs

#11
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:

Code: ags

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!!!!
Cool stuff I found out: Men are four times more likely to be struck by lightning than women.  Wow, really? [dirty joke] Maybe this has to do with the fact that us men have "lightning rods"? [/dirty joke]

Khris

Sorry, I was tired :)
Just change the original function:
Code: ags
function noloopcheck wait_click() {
Ã,  while (!mouse.IsButtonDown(eMouseLeft)) Wait(1);
Ã,  while (mouse.IsButtonDown(eMouseLeft)) Wait(1);
}


You won't need wait_unclick then.

Gilbert

Why's the second while needed? So the wait will end when the button is released?

Khris

Yes, otherwise
QuoteAlmost!

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

SMF spam blocked by CleanTalk