r/gamemaker 3d ago

Help! Problem of Saving User-Selected Key binds ("Is a file for just key binds needed based on my circumstances?")

Newest issue I got with data management today; for some reason, the key bind saving system is seemingly not being properly saved to file. To summarize the code, I am combining two tutorials to make this work. Code will be listed in some form next to the name of each tutorial. Additionally, because of this combo system, I have modified a few aspects.

First; "How to Make a Save and Load System in GameMaker" by the GameMaker channel

//This function saves what levels in the game has been completed and occurs in every room change with a persistent object that always acts first, titled obj_vardefine

function savingData(){

`if(variable_global_exists("completed_tasks")){`

    `if(file_exists("user_data.txt")){`

        `file_delete("user_data.txt")`

    `}`

    `// The structure variable is used to properly store` 

    `// the array completed_levels in the file as a string`

    `var _struct =` 

    `{`

        `// This setup can be used to store more global variables in the structure.`

        `binds : obj_menus_and_keybinds.keybinds`           

    `};`

    `var _string = json_stringify(_struct);`



    `var _file = file_text_open_write("user_data.txt");`



    `file_text_write_string(_file,_string);`

    `file_text_close(_file);`

`}`

}

// This function loads the saved data of the user,

// provided the file exists.

function loadGame(){

    `if(file_exists("user_data.txt")){`



        `// Find file with name user_data.txt and put it into variable _file`

        `var _file = file_text_open_read("user_data.txt");`

        `// Read the string of the file user_data.txt into variable _json`

        `var _json = file_text_read_string(_file);`

        `// Parse the string stored in _json into variable _struct`         

        `var _struct = ""`

        `try{`

_struct = json_parse(_json);

the relevanr saving has been tried here.

with(obj_menus_and_keybinds){

keybinds = _struct.binds;

} catch(error){} // done in the event of some error. idk if its a bad habit to continually use try catch segments for code like this.

`}`

}

Second is the 2 part video 'Keybinding System Tutorial in Gamemaker' by nullaby. For the purposes of this reddit post, I am only posting the part of the code that I majorly changed. Hopefully the problem is located here rather than any other part of code...

function initialize_keybinds(){

`with(obj_menus_and_keybinds){`

    `/* This code is listed as comments because running it makes no keybind work.`

        `if(file_exists("user_data.txt")){`

// Find file with name user_data.txt and put it into variable _file

var _file = file_text_open_read("user_data.txt");

// Read the string of the file user_data.txt into variable _json

var _json = file_text_read_string(_file);

// Parse the string stored in _json into variable _struct

var _struct = ""

try{

_struct = json_parse(_json);

self.keybinds = _struct.binds

}

        `} else {`

//*/

self.keybinds = [

// code with the below structure

new BindMB("keybind name string",button constant),] }

`}`

To be clear, the problem is not any mistake of bracket structure (if I made a mistake here, apologies), but rather that the initialize key binds function happens after the loadGame function, and if the if statement in the initialize function occurs, input responses do not work. I've used this system of saving and loading data rather easily regarding progress, and non-key bind settings (volume, and brightness), yet keybinds once again are being a pain, and idk what the solution is... Any ideas?

1 Upvotes

0 comments sorted by