GML Problems retrieving data from a ds_list

Hi guys. I've been working on a system to allow the player to customize the key bindings to whichever keys they want. I've been successful in that once the keys have been changed, using those new keys in-game works fine. The problem I'm having is that when I exit and start the game again, and attempt to load the saved (and changed) key bindings, I'm simply loading undefined values.

The following is in a script (run from a menu-type object) where I am changing the key bindings and saving the array into a ds_list. I then write that list to a variable and store it in a ds_map:
Code:
if(keyboard_lastkey != -1)
{
    inputs[mpos] = keyboard_lastkey;
    Input.inputs = inputs; //set the Input's array to the same value
    keyboard_lastkey = -1;
    changing_button = false;
    button_colour = c_white;
   
    //SAVE the input each time ************
    //var temp = ds_list_create();
    //ds_list_clear(temp);
    var i; 
    for(i = 0; i <= 11; i += 1)
    {
        ds_list_replace(temp,i,inputs[i]); //saves the inputs into a ds_list called "temp"
        show_message("Saving button " + string(i) + " as " + string(inputs[i]));
    }
    temp_book = ds_list_write(temp); //temp_book is  our ds_list saved in string form

    with(Input)
    {
        ds_map_replace(input_save_data,"controls",obj_key_helper.temp_book); //saves our ds_list (in string form) in a ds_map   
        ds_map_secure_save(input_save_data,"Saved_Input");
    }
}
And the following code is from the Create event of "Input", my object that will be loading the ds_map, reading the string I've previously saved, and creating an array from the resulting ds_list. (I'm using debug messages to try to help me figure why my final resulting array is full of undefined values)
Code:
input_save_data = ds_map_create();
input_name = "Saved_Input"; 

if(file_exists(input_name))
{
    show_message("we have previously saved controls");
   
    ds_map_destroy(input_save_data); //deletes the old save data
    input_save_data = ds_map_secure_load(input_name); //loads the data
    //room_restart();
   
    var con = ds_map_find_value(input_save_data,"controls");   
    if(!is_undefined(con))
    {
        temp = ds_list_create();
        ds_list_clear(temp);
        ds_list_read(temp,con); //saves the information from "controls" into a ds_list called temp
       
       
        inputs = array_create(12);
        var i; //we need to read the ds_list and transfer it to an array
        for(i = 0; i <= 11; i += 1)
        {
            if(!is_undefined(ds_list_find_value(temp,i)))
            {
                inputs[i] = ds_list_find_value(temp,i);
            }
            else
            {
                show_message("Undefined control " + string(i));   
            }
        }

    }
    else //if we don't have special controls saved, use the default controls
    {
        show_message("we didn't save any controls yet but we have the save file");
        inputs = array_create(12);
        inputs[0] = ord("W"); //up
        inputs[1] = ord("S");//down
        inputs[2] = ord("A");//left
        inputs[3] = ord("D");//right
        inputs[4] = vk_down;//A
        inputs[5] = vk_left;//B
        inputs[6] = vk_up;//X
        inputs[7] = vk_enter;//start
        inputs[8] = vk_backspace;//select
        inputs[9] = vk_right; //R
        inputs[10] = vk_rcontrol; //L
        inputs[11] = vk_rshift; //y
    }
}
else
{
    //use the default controls   
    show_message("we don't have a controls save file yet");
    inputs = array_create(12);
    inputs[0] = ord("W"); //up
    inputs[1] = ord("S");//down
    inputs[2] = ord("A");//left
    inputs[3] = ord("D");//right
    inputs[4] = vk_down;//A
    inputs[5] = vk_left;//B
    inputs[6] = vk_up;//X
    inputs[7] = vk_enter;//start
    inputs[8] = vk_backspace;//select
    inputs[9] = vk_right; //R
    inputs[10] = vk_rcontrol; //L
    inputs[11] = vk_rshift; //y
}
I've used similar logic before, when saving and loading lists of items, but I can't for the life of me figure out why the values I'm loading are undefined. Any help is much appreciated!!
 

2Dcube

Member
temp_book is not a local variable? (declared with var)
If so, obj_key_helper.temp_book would refer to a different (instance) variable.

I didn't see anything wrong so this is my wild guess...
 
Hey 2Dcube, thanks for responding. I tried making temp and temp book both local variables and non-local variables, but it didn't change the outcome. I went about it a slightly different way last night and managed to get it to work, but I ended up saving the inputs as individual entries into a ds_map rather than using a ds_list. I still don't know what is wrong, but I'm under the impression that GameMaker doesn't like taking keyboard inputs, adding them to a ds_list, converting it to a string, and then reversing this. The resulting "keyboard inputs" become undefined. Something goes wrong during that process. I've made due using ds_maps, but if anyone knows WHY GameMaker doesn't like doing this I'd be happy to learn. :)
 
Top