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:
This is the control_check script:
And, this is on the Step event of a test player object:
Is this kind of thing optimized in GML? Or is there a better, more "GameMaker-esk" solution?
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);
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]);
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));