• 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!

GML Question about abstracting user input

S

Searous

Guest
I'd just like to know if my method of achieving this is "well implemented". Personally, I think it works great for the way I write my code, but idk.

Essentially, I'm using an enum for constants which are the different controls for the game. Then, when I check for a control, I simply use a control_check script, and pass it an enum value. This method also accounts for multiple input methods.

This is on the Create event for a persistent object created in a "loading" room when the game starts:
Code:
enum Control { // Internal input names
    MOVE_UP,
    MOVE_LEFT,
    MOVE_RIGHT,
    MOVE_DOWN,
    
    INTERACT
}
globalvar controls;
controls = ds_map_create(); // Keys = enum Control // Values = [<key id>, <is mouse>]
ds_map_add(controls, Control.MOVE_UP, [ord("W"), false])
ds_map_add(controls, Control.MOVE_LEFT, [ord("A"), false])
ds_map_add(controls, Control.MOVE_DOWN, [ord("S"), false])
ds_map_add(controls, Control.MOVE_RIGHT, [ord("D"), false])

ds_map_add(controls, Control.INTERACT, [ord("E"), false])

room_goto(test);
This is the control_check script:
Code:
/// @description Check a control
/// @param control The control to be checked--from enum Control

var c;
c = ds_map_find_value(controls, argument0);

if(c[1])  // Is mouse button
    return mouse_check_button(c[0]);
else  // Is not mouse button
    return keyboard_check(c[0]);
And, this is on the Step event of a test player object:
Code:
event_inherited();

x += spd * (control_check(Control.MOVE_RIGHT) - control_check(Control.MOVE_LEFT));
y += spd * (control_check(Control.MOVE_DOWN) - control_check(Control.MOVE_UP));
Is this kind of thing optimized in GML? Or is there a better, more "GameMaker-esk" solution?
 
Top