• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Best way to get pressed keys

Kezarus

Endless Game Maker
Hi everyone!

I am planing to make a rebindable keys screen that could be universal for any game and I am wondering on the best way to recover those said keys in the game.

I was thinking on get the keys with a function on the step even. So I will have to make an if for each key. Like so:

Code:
var pressedKey = get_pressed_key()

if(get_key_shoot(pressedKey)){
    //shoot
}
Is the a better way to do this? =]
 

Rob

Member
Depending on how many keys you have, it might be better to put a little more effort in the initial code, use an enumerator and an array that stores the key bindings and then check for something like:
if key_bindings[keys.fire]{
//fire
}

Making the array a list instead will make it easy to save/load key bindings (using ds_list_write/ds_list_read)
 

Nidoking

Member
You could consider using a map to link keys to functions. You could either enumerate the keys or use strings to set them apart, then map the key identifiers to those values and just grab the value from the map for the pressed key, or vice versa. The trick would be that you'd have to clear the map when you change the key bindings, but that's simple enough if you destroy the old map and make a new one.
 

Joe Ellis

Member
I currently just use global variables like global.key_jump, key_attack etc.
It's really easy to save those aswell, cus you can just make a text file, each line is like: "key_jump = " + string(global.key_jump)

Then when loading, you can read the name of each global variable and set it to the key index:
Code:
while !file_text_eof(file)
{
str = file_text_readln(file)
var_name = string_copy(str, 1, string_pos(" = ", str) - 1)
key_index = string_delete(str, 1, string_pos(" = ", str) + 2)
variable_global_set(var_name, real(key_index))
}
 
Last edited:

NightFrost

Member
I have a portable control system that I can drop into a project and then tweak to add specific controls the game requires. The controls are exposed to the rest of the game through an enumerator-indexed array. The enumerator describes all the actions (controls) of the game, and the array contains the current state of each action. For example,
Code:
enum Action {
   Left,
   Right,
   Up,
   Down,
   Shoot
}

State[Action.Left] = 0;
State[Action.Right] = 0;
State[Action.Up] = 0;
State[Action.Down] = 0;
State[Action.Shoot] = 0;
The State contents would usually be 0 or 1, unless it represents something like gamepad stick's output. Beneath this sits what I call an action binding system. It's job is to connect each defined Action to multiple control sources. For example, I could define bindings for Action.Shoot to keyboard "Space" button, gamepad "X" button and mouse left button. That is, if I was supporting all three control methods. The obj_control where all this stuff resides goes through the bindings array each step and combines its discoveries into the State array. An example of a binding definition would be:
Code:
// Action: left, binding to keyboard.
var _k1 = [];
_k1[Action_Data.Action] = Action.Left;
_k1[Action_Data.Method] = Control_Method.Keyboard_Button;
_k1[Action_Data.Binding] = vk_left;
_k1[Action_Data.Description] = "ARROW LEFT";
Binding_List[Action_Binding.Keyboard_Left] = _k1;
To actually change these controls, a settings page would go through this Binding_List and display all that have Control_Method.Keyboard_Button in them. On top of that would sit some code that lets player to pick one from the shown list and enter a new control key, which the settings page system would then save into the array that describes the binding. In this case, replacing vk_left with whatever user pressed.
 

Kezarus

Endless Game Maker
Hi good people!

Thanks for all contributions! At first I was resistent to an Enum idea, but this way you can get all actions in one place without messing it up. Nice.

So, I will make a map to set and verify all game keys and corresponding actions. Like Map[key] = action. I will do this to check for repeated keys biding attempts and alternate keys configurations. So I could have:
Code:
enum Action{
   Left,
   Right,
   Up,
   Down,
   Shoot,
   Run
}
keyBidings[? vk_left] = Action.Left;
keyBidings[? ord("A")] = Action.Left;
Will make it to be rebindable to a joystick too. Then, save and load them to an INI file and make a simple game to test it out.

All this will be posted on my Framework. Check out Git and the Marketplace. The idea behind it is to provide all features common to games like basic functions, GUI, Music engine, Screen Shake, Particles Lab and Language Support. Check it out, it's free! I will gladly accept feedback on it. =]


Cheers!
Kezarus
 
Top