Author Topic: SOLVED: on_key_press_always()???  (Read 567 times)  Share 

alkis21

  • AGS rocks
  • alkis21 worked on a game that was nominated for an AGS Award!
SOLVED: on_key_press_always()???
« on: 26 Jul 2007, 20:12 »
I find the repeatedly_execute_always() function extremely useful and I resort to it many times while scripting. Has anyone managed to make a similar function (or module) for on_key_press()? I want to be able to use a "magic" button when GUIs are open or when a conversation is playing but I can't figure out whether that's possible or not. For instance, I have an introduction in my game and I want the player to be able to stop watching it by pressing Escape. Or, keys 1-7 set the music volume and I want the player to be able to reduce the volume during a dialog. But I can do neither because you can't assign keyboard functions while a dialog is playing... or can you?
« Last Edit: 27 Jul 2007, 22:31 by alkis21 »

Iceboty V7000a

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
    •  
Re: on_key_press_always()???
« Reply #1 on: 27 Jul 2007, 01:57 »
In fact, the on_key_press() function is already called when a GUI (that will pause the game) is on, I think it won't work with blocking situations like during conversation or other blocking calls though.

If you examine the default on_key_press() function, you'll see the first line is:
[code]
if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
[/code]
So if you comment this line, all the following lines in this function will be executed when the game is paused.

If you really want the keys to be checked in other blocking situations, and you actually don't need to check many keys, just do something like below in repeatedly_execute_always():
[code]
if (IsKeyPressed(123)) XXXXXXX;
if (IsKeyPressed(124)) XXXXXXX;
blah bla bla
[/code]

alkis21

  • AGS rocks
  • alkis21 worked on a game that was nominated for an AGS Award!
Re: on_key_press_always()???
« Reply #2 on: 27 Jul 2007, 09:02 »
If you examine the default on_key_press() function, you'll see the first line is:
[code]
if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
[/code]
So if you comment this line, all the following lines in this function will be executed when the game is paused.

I'm aware of that, but what if I don't want all keys to work when any GUI is open, but just one key to work when a specific GUI is open?

EDIT: I gave it some thought, perhaps I could edit the line as:

[code]if ((IsGamePaused() == 1) && (GUI
  • .Visible=false)) keycode=0;[/code]

    and then amend my
IsKeyPressed lines accordingly.

Quote
just do something like below in repeatedly_execute_always():
if (IsKeyPressed(123)) XXXXXXX;

I never thought of that. I'll try it tonight, thanks.
« Last Edit: 27 Jul 2007, 09:12 by alkis21 »

Iceboty V7000a

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
    •  
Re: on_key_press_always()???
« Reply #3 on: 27 Jul 2007, 09:37 »
I'm aware of that, but what if I don't want all keys to work when any GUI is open, but just one key to work when a specific GUI is open?

EDIT: I gave it some thought, perhaps I could edit the line as:

[code]if ((IsGamePaused() == 1) && (GUI
  • .Visible=false)) keycode=0;[/code]

Yes, just try to think of the logic depending on the actual needs.
For example if you need only key a, key b, key c to be recognised when GUI x is active (and of course when the game is not paused as well) and void the detection of the other keys when the game is paused you may try something like:

[code]
if ((IsGamePaused() == 1) && (GUI
  • .Visible=false)) keycode=0;//your line, void all input when GUI x is not active and the game is paused.
  • [/list]
    if (keycode==a) blah;
    if (keycode==b) blah bla;
    if (keycode==c) blah bla bla;
    if (IsGamePaused() == 1) keycode=0;//void the remaining checks even if GUI x is active
    if (keycode == xxx) xxx.....
    [/code]

    Since there're many posisbilities, it all depends on how you want it to be implemented, so there's no universal solution.

    alkis21

    • AGS rocks
    • alkis21 worked on a game that was nominated for an AGS Award!
    Re: on_key_press_always()???
    « Reply #4 on: 27 Jul 2007, 21:35 »
    I'm trying to assign Escape in repeatedly_execute_always() so that the introduction is skipped if I press it... but in order for me to quit the introduction, I need to use a NewRoom() function. Unfortunately, I got the error message:

    Error: This command cannot be used within repeatedly_execute_always()

    Any ideas?

    Khris

    • Evil Dark Emperor Death-Kill
      • Lifetime Achievement Award Winner
      •  
      • I can help with play testing
      •  
      • I can help with scripting
      •  
      • I can help with translating
      •  
    Re: on_key_press_always()???
    « Reply #5 on: 27 Jul 2007, 21:52 »
    Make the introduction a cutscene.
    Put StartCutScene(eSkipEscOnly); at the beginning, EndCutScene(); at the end (from memory, so check the commands' spelling).

    As soon as Esc is pressed, AGS skips right to the line after EntCutscene();
    http://whathaveyoutried.com/

    The other day on yahoo answers:
    "Can you print colored images with black ink? If so tell me how please Thanx Kimberly"

    alkis21

    • AGS rocks
    • alkis21 worked on a game that was nominated for an AGS Award!
    Re: on_key_press_always()???
    « Reply #6 on: 27 Jul 2007, 22:19 »
    I was about to reply that it wouldn't work, as I had tried it before, but I thought I'd give it another shot. I used to put the EndCutScene() function inside dialog_request, as the last thing that occurs in the intro is a dialog line, and the game would hang. This time I put it inside the Player Enter Room (before fadein) section of the first room you end up in after the introduction and it worked!

    I'm not sure I understand how StartCutScene function works, if I have a number of SetGlobalInt functions inside the cut scene, will they be executed if the player skips it or not?

    Khris

    • Evil Dark Emperor Death-Kill
      • Lifetime Achievement Award Winner
      •  
      • I can help with play testing
      •  
      • I can help with scripting
      •  
      • I can help with translating
      •  
    Re: on_key_press_always()???
    « Reply #7 on: 27 Jul 2007, 22:25 »
    Yes, everything is executed as usual, but in practically zero time.
    E.g. a blocking character movement does take place, but the character is placed at the destination coords instead of taking a blocking walk there.

    That's why you have to make sure the command line processor gets to the EndCutscene() command, so AGS can switch back to normal, time-consuming execution.
    http://whathaveyoutried.com/

    The other day on yahoo answers:
    "Can you print colored images with black ink? If so tell me how please Thanx Kimberly"

    alkis21

    • AGS rocks
    • alkis21 worked on a game that was nominated for an AGS Award!
    Re: on_key_press_always()???
    « Reply #8 on: 27 Jul 2007, 22:31 »
    Awesome. Thank you.

    Ashen

    Re: SOLVED: on_key_press_always()???
    « Reply #9 on: 28 Jul 2007, 13:36 »
    Do you still need an on_keypress_always function as well, or did Gilbot's suggestions cover that?
    I know what you're thinking ... Don't think that.

    alkis21

    • AGS rocks
    • alkis21 worked on a game that was nominated for an AGS Award!
    Re: SOLVED: on_key_press_always()???
    « Reply #10 on: 28 Jul 2007, 15:00 »
    Gilbot's suggestions worked fine.