[Solved]Recieving key presses in a room

Started by jastorman, Sat 14/02/2009 23:39:21

Previous topic - Next topic

jastorman

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 
Bren,

From such crooked wood as that which man is made of, nothing straight can be fashioned.

Creator

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.

Code: ags

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

jastorman

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?
Bren,

From such crooked wood as that which man is made of, nothing straight can be fashioned.

Pumaman

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.

jastorman

Thaks for the help guys, the script is now working as I want it to.
Bren,

From such crooked wood as that which man is made of, nothing straight can be fashioned.

SMF spam blocked by CleanTalk