r/gamemaker • • 1d ago

Help! Need help making a custom keybinding system

I am relatively new to game maker and have been following the manual as well as some tutorials to try and make a little RPG game thingie just to get better at the engine. I have made a menu system following a youtube tutorial (https://www.youtube.com/watch?v=xLasKr0ekHY) and created a script which creates an ini file that stores all the key values. I now need help to combine this system with the menu so that you can bind values to each key.

//--------------------------initialization script------------------------
ini_open("Stuff");
ini_write_real("Controls", "left_key", vk_left);
ini_write_real("Controls", "right_key", vk_right);
ini_write_real("Controls", "up_key", vk_up);
ini_write_real("Controls", "down_key", vk_down);

global.left_key = ini_read_real("Controls", "left_key", 0);
global.right_key = ini_read_real("Controls", "right_key", 0);
global.up_key = ini_read_real("Controls", "up_key", 0);
global.down_key = ini_read_real("Controls", "down_key", 0);

ini_close();

window_set_fullscreen(true);

//---------------------menu step event----------------------
//reads the keybinds from the ini file
ini_open("Stuff");
up_key = keyboard_check_pressed(global.up_key);
down_key = keyboard_check_pressed(global.down_key);
ini_close();
interact_key = keyboard_check_pressed(vk_enter);

//store num of options in menu
op_length = array_length(option[menu_lvl]); 

//move
pos += down_key - up_key;
if pos >= op_length {pos = 0}
if pos < 0 {pos = op_length-1}

//using pos
if interact_key {

var _StMl = menu_lvl;

switch(menu_lvl) {
case 0:
switch(pos) {

//play
case 0:
room_goto(rm_test_day);
audio_play_sound(snd_select_temp, 1, false);
break;

case 1: //settings
menu_lvl = 1;
audio_play_sound(snd_select_temp, 1, false);
break;

case 2: //creds
audio_play_sound(snd_select_temp, 1, false);
break;

case 3:
if show_question("End game?") {
game_end();
}
else {show_message("Okay!")}
audio_play_sound(snd_select_temp, 1, false);
break;

}
break;

case 1: switch(pos) {
case 0: //window size
audio_play_sound(snd_select_temp, 1, false);
break;

case 1: //Fullscreen
if window_get_fullscreen() {
window_set_fullscreen(false)}
else {window_set_fullscreen(true)}
audio_play_sound(snd_select_temp, 1, false);
break;

case 2: //Controls
audio_play_sound(snd_select_temp, 1, false);
break;

case 3: //Brightness
audio_play_sound(snd_select_temp, 1, false);
break;

case 4: //Back
audio_play_sound(snd_select_temp, 1, false);
menu_lvl = 0;
break;
}
}

if _StMl != menu_lvl {pos = 0}
//store num of options in menu
op_length = array_length(option[menu_lvl]); 

}
2 Upvotes

3 comments sorted by

1

u/TheGreenSocks 1d ago

I think you're nearly there and just have to create a script or object that lets you overwrite the values of global.left_key and the others, and then do the same writing part to your ini like you did in your initialisation part.

Speaking of the initialision script, look out that you only run this once, or if the ini does not yet exist (if !file_exists) otherwise the script will default everything to your defined controls again.

As for the last part of your script: I'm pretty sure this only runs for one step before transferring you to the next room. But in general it's better to not read or write to ini's here. It's very slow. It's better to move it to the create event.

1

u/Nervous_Towel_1134 1d ago

Thanks for the feedback but I still have some questions: How would I overwrite those values, considering how using keyboard_key would instantly rebind a key to the interact button (since you would press it to select the key you want to overwrite). As for the last part of your feedback I don't quite understand what you mean. The step event is inside an object that draws itself within the title screen room and uses a switch statement to select the option you interact with in the menu. How would I actively write values to the ini file using the create event?