Add two keys to one binding[SOLVED]

Started by Stranga, Fri 02/10/2020 02:25:39

Previous topic - Next topic

Stranga

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

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

  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.

Crimson Wizard

#1
You cannot store 2 integers in one int variable*, you have to use array of two or more elements instead, like
Code: ags

eKeyCode KeyUp[2];


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

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


and
Code: ags

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

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'

Stranga

#2
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:

Any ideas as to what could be causing this?

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

Thanks again CW! :D

lafouine88

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

Snarky

#4
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
  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
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).

lafouine88

You're so right snarky. This was waaaaay better than the limited eKeyCodes!
Thanks a lot :)

SMF spam blocked by CleanTalk