Key Input Manager

Learn how to make certain types of games and use gameEditor.

Key Input Manager

Postby Waerloga » Mon Mar 20, 2017 10:04 am

Instead of separate scripts for each button pressed, try putting all input into one script. For example, if using the W,A,S,D keys for movement, create a variable called ThisKey (Global, Integer) to store the value of the key pressed, then another variable called Btns (Actor, Integer) which will store a number value based on the key or combinations of keys pressed.

Now make this script run on your Player Actor for a Key Down Event for any of the four keys pressed set to the following:

Key Down Event: a w s d. Execute when (At least one key is pressed.) Repeat: Disable.

Here is the Key Down script:

Code: Select all
ThisKey = getLastKey();

switch (ThisKey)
{
case KEY_a: Btns+=1;
break;
case KEY_w: Btns+=2;
break;
case KEY_s: Btns+=4;
break;
case KEY_d: Btns+=8;
break;
}


And then do separate Key Up events for each button (w,a,s,d) for when it is released to deduct the value that was added when the button was pressed.
The script for button (a):

Code: Select all
Btns-=1;


Each button has a number assigned to it and each number in this sequence is double the previous one so that any button combination produces a unique value, so if you press (w) and (a) together, the Btns variable becomes '3' (1+2=3), the values of (w) and (a) combined, then if you hold them down and also press (d), the value then adds up to '11'.
Using this system will allow for multiple keys to be pressed simultaneously without conflicts.

Now on Draw Actor Event for the Player Actor, put something like this script which will determine the current value of the Btns variable and respond by moving the player accordingly (Using compass directions of North, West, East, South in this case):

Code: Select all
switch (Btns)
{
case 0: directional_velocity=0;//No keys pressed
break;
case 1: angle=180; directional_velocity=5;//Button (a) is being pressed so move player West.
break;
case 2: angle=90; directional_velocity=4;//N
break;
case 3: angle=153.43; directional_velocity=5;//NW
break;
case 4: angle=270; directional_velocity=4;//S
break;
case 5: angle=206.56; directional_velocity=5;//SW
break;
case 6: directional_velocity=0;
break;
case 7: angle=180; directional_velocity=5;//W
break;
case 8: angle=0; directional_velocity=5;//E
break;
case 9: directional_velocity=0;
break;
case 10: angle=26.56; directional_velocity=5;//NE
break;
case 11: angle=90; directional_velocity=4;//N
break;
case 12: angle=333.43; directional_velocity=5;//SE
break;
case 13: angle=270; directional_velocity=4;//S
break;
case 14: angle=0; directional_velocity=5;//E
break;
case 15: directional_velocity=0;
break;
}


This movement script was designed for an isometric game so change the list of responses to suit your needs. Here is an example .ged file to demonstrate how it works:
Attachments
InputManager.zip
(405.48 KiB) Downloaded 134 times
Last edited by Waerloga on Wed Mar 29, 2017 8:46 am, edited 3 times in total.
User avatar
Waerloga
 
Posts: 5
Joined: Sat Apr 16, 2011 8:47 am
Score: 1 Give a positive score

Re: Key Input Manager

Postby lcl » Tue Mar 21, 2017 2:24 pm

Great tutorial, and a very clever way to avoid problems with having multiple keys pressed! Good job! +1
By the way, the first line in the Key Down script is not needed, you never actually use the variable 'key' for anything, so you could just as well leave the whole line out.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Key Input Manager

Postby Waerloga » Mon Mar 27, 2017 10:31 am

Thanks for pointing that out. Now I wonder why I did that. It was a while ago I came up with this key-input solution so I can't remember the reason behind it. Very clumsy of me not to have removed it before posting it here. I have now ammended the code. +1 for spotting the error and giving useful feedback.
The guy MrJolteon is so much in love with that he wears my name as his signature. So gay.
User avatar
Waerloga
 
Posts: 5
Joined: Sat Apr 16, 2011 8:47 am
Score: 1 Give a positive score

Re: Key Input Manager

Postby lcl » Mon Mar 27, 2017 3:52 pm

Waerloga wrote:Thanks for pointing that out. Now I wonder why I did that. It was a while ago I came up with this key-input solution so I can't remember the reason behind it. Very clumsy of me not to have removed it before posting it here. I have now ammended the code. +1 for spotting the error and giving useful feedback.

No problem. :) You might want to remove the unnecessary code from the post itself, too.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest