Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jastorman on Sat 14/02/2009 23:39:21

Title: [Solved]Recieving key presses in a room
Post by: jastorman on Sat 14/02/2009 23:39:21
Hi, I am trying to create a computer interface in my game for which I have created a separate room. To begin with, I want the user to be able to select from several options using the keyboard (but not via a textbox or GUI), for example, if the user presses '1', I want him to be taken to option 1. I know this can be done with on_key_press, but I don't know how to do it within my room script. I have perused the manual for answers to no avail.
Sorry if this question is stupid or obvious, I'm not much of a programmer, but I would like to learn how to make games with AGS 
Title: Re: Recieving key presses in a room
Post by: Creator on Sun 15/02/2009 05:08:47
If you are trying to run a key press outside of on_key_press you need to use IsKeyPressed. Search for it in the manual, but here's a sample code anyway.


if (IsKeyPressed(65) == 1) { // If 'A' is pressed
  //run script here
}


Look in the manual for ASCII Code Table to see what key codes correspond to what keys.

I hope this has been helpful. It's harder to explain than I thought.  :P
Title: Re: Recieving key presses in a room
Post by: jastorman on Sun 15/02/2009 12:28:35
Thanks Creator, that works well for selecting options; For future reference, is it possible to get the ASCII code as an int rather than checking whether individual keys are pressed? Can On_key_press be used in the room script, or can you contrive a method that would have the same effect?
Title: Re: Recieving key presses in a room
Post by: Pumaman on Sun 15/02/2009 14:14:00
Yes, you can place a on_key_press function in the room script, and it will get run first, before the one in the global script.

Just paste something like this into the room script:

function on_key_press(eKeyCode keycode)
{
  if (keycode == eKeyA)
  {
    Display("You pressed A in the room script!!");
    ClaimEvent();
  }
}

The "ClaimEvent" tells it not to run the global script on_key_press afterwards -- take that out if you would want it to do so anyway.

If you're not using AGS 3.1.2, replace "eKeyCode" with "int" and "eKeyA" with 65.
Title: Re: Recieving key presses in a room
Post by: jastorman on Sun 15/02/2009 14:53:03
Thaks for the help guys, the script is now working as I want it to.