Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Stranga on Fri 02/10/2020 02:25:39

Title: Add two keys to one binding[SOLVED]
Post by: Stranga on Fri 02/10/2020 02:25:39
Hello everyone,

I'm having trouble trying to figure out something. I've set up my game to use keyboard only so I made a key binding script where each key press to be controlled another variable. The reason behind this is so the player can change keys if they don't like the default ones.

Now, what I'm having trouble with is I would like to set 2 keys to 1 binding. Are you able to add two different int's to one?

Here's the start of the code.
Code (ags) Select

eKeyCode KeyUp;
eKeyCode KeyDown;
eKeyCode KeyLeft;
eKeyCode KeyRight;
eKeyCode KeyAction;
eKeyCode KeyBack;
eKeyCode KeyMenu;
eKeyCode KeySprint;
eKeyCode KeySkip;
eKeyCode KeyInventory;

function game_start()
{
  KeyUp = eKeyUpArrow;
  KeyDown = eKeyDownArrow;
  KeyLeft = eKeyLeftArrow;
  KeyRight = eKeyRightArrow;
  KeyAction = eKeyZ;
  KeyBack = eKeyX;
  KeyMenu = eKeyEscape;
  KeySkip = eKeyEscape;
  KeySprint = eKeyC;
  //KeySprint = 406;//Right Crtl
  KeyInventory = eKeyE;
}


I've also tried this and not sure if it was supposed to work or not:
Code (ags) Select

  KeyUp = eKeyUpArrow || eKeyW;


Any help would be greatly appreciated.

P.S Sorry if this doesn't make much sense, it's a little hard to explain.
Title: Re: Add two keys to one binding
Post by: Crimson Wizard on Fri 02/10/2020 03:30:40
You cannot store 2 integers in one int variable*, you have to use array of two or more elements instead, like
Code (ags) Select

eKeyCode KeyUp[2];


And everytime addressing these variables you'd have to specify the element index, like
Code (ags) Select

KeyUp[0] = eKeyUpArrow;
KeyUp[1] = eKeyW;


and
Code (ags) Select

if (keycode == KeyUp[0] || keycode == KeyUp[1])



note that index begins with 0. The first element is #0 and second element is #1.


*PS. Okay, truth is, you *could* store 2 values in one integer using a technique known as "bit packing" and bitwise operations, but I won't recommend that without real need, as that may make your code look even more complicated.
Something like:
Code (ags) Select

KeyUp = eKeyUpArrow + (eKeyW << 8); // pack 2 values in an integer; values must be less than 256

int value1 = (KeyUp & 256); // unpack first value
int value2 = ((KeyUp >> 8) & 256); // unpack second value

if (keycode == (KeyUp & 256) || keycode == ((KeyUp >> 8) & 256)) // same inside an 'if'
Title: Re: Add two keys to one binding
Post by: Stranga on Fri 02/10/2020 03:56:02
Ah I see, thank you CW!

I thought that would have been the case. And you're right, it looks like a lot more work/code to store two integers into a variable. Arrays is the way to go! :D

EDIT: I've implemented the arrays and it looks all fine but when I run the game I receive this error:
(https://www.newgrounds.com/dump/draw/dcc89a86baa8ee0ca2964cd8900f0e42)
Any ideas as to what could be causing this?

DOUBLE EDIT: I forgot to export the functions, my bad :P

Thanks again CW! :D
Title: Re: Add two keys to one binding[SOLVED]
Post by: lafouine88 on Fri 21/05/2021 19:07:20
Hi guys, I'm not really sure this is really the best way to ask but I did'nt want to create a new topic for such a stupid question.

I'm trying to apply keycodes, but I can't find on the manual (and I think I tried most of them one by one) the keycode for the 2 key, the one that is not on the keypad. Since my keyboard is a french one I guess the signs are not the same but from what I found it's supposed to be either ~ or é, or sometimes @. But none of them works.

So sorry again but I 've been postponing this resolution for a while now and the deadline for my games starts to loom ahead.

Thanks
Title: Re: Add two keys to one binding[SOLVED]
Post by: Snarky on Fri 21/05/2021 19:43:24
I think the easiest way to identify the right code is to write a function that will report which key is pressed.

It depends a little bit on how you use these codes. If you use them in on_key_press(), just use that to report the key. Add this line to your on_key_press() function, run the game and press the key you need to identify:

Code (ags) Select
  Display("Key pressed was %d", keycode);

However, if you're planning to use them using IsKeyPressed(), the codes are a little different (mostly, it reports more keys, but on the other hand it doesn't distinguish upper and lowercase), so you should use IsKeyPressed() to figure out what the keycode for this key is. You'll need something like:

Code (ags) Select
function repeatedly_execute_always()
{
  for(int i=0; i<600; i++)
  {
    if(IsKeyPressed(i))
      Display("Key pressed is %d", i);
  }
}


(I don't remember what the max keycode reported by AGS is, but the SHIFT keys are 403 and 404, so I think 600 should be sufficient to cover everything.)

I would also note that the issues you are experiencing are likely to become issues for your players as well, if their keyboard is not set up like yours. You might want to make this keybinding user-configurable (using something much like the example functions here).
Title: Re: Add two keys to one binding[SOLVED]
Post by: lafouine88 on Fri 21/05/2021 21:22:41
You're so right snarky. This was waaaaay better than the limited eKeyCodes!
Thanks a lot :)