"on_key_press" while blocking

Started by TheJBurger, Sun 09/04/2006 22:11:58

Previous topic - Next topic

TheJBurger

Is there any way to get "on_key_press" to work while the game is blocking?

I set spacebar to pause the game (it works fine and well) but I'd also like it to work during cutscenes and blocking sequences.

Is this possible?

monkey0506

It's possible through the use of "IsKeyPressed" and "repeatedly_execute_always", but not through "on_key_press".

Gilbert

Unfortunately if it's stuff like "pausing the game" it's not easy to do with repeatedly_execute_always(), since blocking functions like Wait() are not allowed there.

monkey0506

Making the GUI a Popup modal GUI would pause it.

TheJBurger

This is the code I added to my global script:

Code: ags

function repeatedly_execute_always() {
Ã,  Ã,  // Pause game with spacebar
Ã,  Ã,  if (IsKeyPressed(32) == 1 && IsGamePaused() == 0) { // spacebar - pause the game
Ã,  Ã,  PauseGame();
Ã,  Ã,  gPaused.Visible=true;
Ã,  Ã,  }
Ã,  else if (IsKeyPressed(32) == 1 && IsGamePaused() == 1) { // spacebar - UNPAUSE the game
Ã,  Ã,  UnPauseGame();
Ã,  Ã,  gPaused.Visible=false;
}
Ã,  }


It works during blocking sequences but I still have a problem with it.

When I press the spacebar, it pauses my game, and unpauses, and then pauses, unpauses, and repeats over and over.

Even when I barely tap the space bar it does this at least a couple times. So I need the spacebar to only pause the game ONE TIME when I press it. Right now it's pausing/unpausing over and over and over when I press it.

Khris

Add this function somewhere above the rep_ex_always:
Code: ags
function noloopcheck waitforspacekeyrelease() {
Ã,  while (IsKeyPressed(32)) {
Ã,  Ã,  Wait(1);Ã,  
Ã,  }
}


Then call it after detecting the keypress.

TheJBurger

Ok, I added it but now I get an error message.

---------------------------
Adventure Game Studio
---------------------------
An error has occured. Please contact the game author for support, as this
is likely to be a scripting error and not a bug in AGS.
(ACI version 2.71.894)

in Global script (line 257)
from Global script (line 264)

Error: A blocking function was called from within repeatedly_execute_always

---------------------------
OKÃ,  Ã, 
---------------------------

monkey0506

#7
(Khris posted before me!)

Uh, Khris, sorry to break it to you, but you cannot call a Wait() statement from within rep_ex_always...

(I've decided this code would probably work better:)

Code: ags
int KeyHeld = 0;

function repeatedly_execute_always() {
Ã,  if ((KeyHeld) && (IsKeyPressed(KeyHeld))) return;
Ã,  KeyHeld = 0;
Ã,  if (IsKeyPressed(' ')) { // Spacebar (32)
Ã,  Ã,  gPaused.Visible = (!gPaused.Visible); // this inverts gPaused.Visible (if gPaused is turned on, this will turn it off, and vice versa)
Ã,  Ã,  KeyHeld = ' '; // KeyHeld = 32
Ã,  Ã,  }
Ã,  // else if (IsKeyPressed(/* OTHER KEY */)) { /* blah */ }
Ã,  }


For this bit of code, you can check for several different keycodes like I did key 32 (spacebar, ' '), just make sure you set KeyHeld correctly inside each set of braces.

Let me know if that works out (as it's untested).

(And now TheJBurger has posted before me...)

See the top of this post.Ã,  You can't call Wait() from inside of rep_ex_al.Ã,  But my code doesn't use Wait().Ã,  So...try it out!

[EDIT:]

I forgot a closing parenthesis.  The code is fixed now.

TheJBurger

I copied your code and replaced it with my old code and now I get a new error message. yay.

---------------------------
Compile Error
---------------------------
There was an error compiling your script. The problem was:

In: 'Global script'



Error (line 258): end of input reached in middle of expression



Do you want to fix the script now? (Your game has not been saved).
---------------------------
YesÃ,  Ã, NoÃ,  Ã, 
---------------------------

Line 258 is :
Code: ags

if ((KeyHeld) && (IsKeyPressed(KeyHeld)) return;

Gilbert

Remove the ( before IsKeyPressed:
Code: ags

if ((KeyHeld) && IsKeyPressed(KeyHeld)) return;


TheJBurger

Done. It works perfectly.

Thanks everyone for your help.

Khris

#11
Heh, it was late... :) I didn't dun testin' it.
Thanks for breaking it to me, I feel way better now. :=

Cino

I tried something similar too a few weeks ago, but I discovered that PauseGame won't pause everything. I had a room with a simple script:
Code: ags
Wait(150);
player.ChangeRoom(1,160,200);


Now when I pause while the code is executing it still changes the room after 150 gamecycles, I'm curious if there's a way to pause such things too?

Radiant

Popping up a modal GUI should do the trick.

Cino

Nope, thought of that already and it didn't work.

strazer

Depending on where you put the script, try putting the ChangeRoom command in a second "Run script" action for the event, i.e.

the event
  -> Run script (Wait in here)
  -> Run script (ChangeRoom in here)
another event
  ...

Cino

Didn't work neither. And it would be kinda uncomfy anyway to put each script line in separate run script, if I was dealing with bigger scripts.

SMF spam blocked by CleanTalk