GML Loading/Saving ini files

K

King Gryzz

Guest
I have a problem where the ini files dont load properly. eg: if i get a score of 150 and type my name as "asd", and when I go to my ini file, it says asd and 150 however, when I re-open my game, the score isn't loaded and the ini file doesnt show asd or 150 anymore

Code that involves the ini file:

some room containing code
Code:
creation code:
ini_open("Save.sav");
for(i = 0; i < 10; i++)
{
    global.score_array[i, 0] = "unknown";
    global.score_array[i, 1] = 0;
}
ini_close()
room_goto(Rm_Level_1);
Submit button
Code:
left button event
if (can_click == true)
{
    can_click = false;
    //get a name from the user
    name = get_string_async("Enter your name:", "");
    get_name = false;
}

Code:
Asynchronous Dialogue event
//listen for async dialog event
var i_d = ds_map_find_value(async_load, "id");

//if the dialog is the one where we ask the player for a name
if i_d == name
{
    if ds_map_find_value(async_load, "status")//if the user clicked ok
    {
        //Checks if string length is not 5 digits or somethig long
        //if the name entered is not blank or long
        if (string_length(ds_map_find_value(async_load, "result")) > 5 || ds_map_find_value(async_load, "result") == "")
        {
            //determine in what way as to dislay the correct error message.
            if (string_length(ds_map_find_value(async_load, "result")) > 5)
            {
                show_message_async("Name too long");
                //get_name = true;
                name = get_string_async("Enter your name:", "AAA");
            }
            
            if (ds_map_find_value(async_load, "result") == "")
            {
                show_message_async("No name entered!");
                //get_name = true;
                name = get_string_async("Enter your name:", "AAA");
            }
        }//otherwise the input is correct and we proceed as usual if it is less than or equal to 5 characters AND not empty
        //if the name entered is not blank or too long
        else if (string_length(ds_map_find_value(async_load, "result")) <= 5 && ds_map_find_value(async_load, "result") != "")
        {
            //get the name entered
            global.name = ds_map_find_value(async_load, "result");
            
            //Opens Save.sav
            ini_open("Save.sav")
            for (i = 0; i<10; i++)
            {
                //fill the global scores array with values from the ini file. If no values are present, then fill with name = unknown and score = 0
                global.score_array[i, 0] = ini_read_string(string(i), "Name", "unknown");
                global.score_array[i, 1] = ini_read_real(string(i), "Score", 0);
            }
            ini_close();
            
            //search for first spot where player's score is greater
            for (i = 0; i<10; i++)
            {
                //if the player's score is greater than the spot in question
                if (global.Score > global.score_array[i, 1])
                {
                    //starting at the last element of the array, set each element to the one before it.
                    for (j = 9; j>i; j--)
                    {
                        global.score_array[j, 0] = global.score_array[j-1, 0];
                        global.score_array[j, 1] = global.score_array[j-1, 1];
                    }
                    
                    //now set the space to that of this player
                    global.score_array[i, 0] = global.name;
                    global.score_array[i, 1] = global.Score;
                    break;
                }
            }
            
            //now update ini with this new array
            ini_open("Score.sav")
            for (i = 0; i<10; i++)
            {
                ini_write_string(string(i), "Name", global.score_array[i, 0]);
                ini_write_real(string(i), "Score", global.score_array[i, 1]);
            }
            //Finishes creating and saves written data
            ini_close();
            
            //go to the room where the high scores table is shown
            room_goto(Rm_Highscore);
        }
   }
   //If clicked cancel, go to the Main Menu
   else
   {
        room_goto(Rm_Main_Menu);
   }
}

 
K

King Gryzz

Guest
so after I reopen the game, all 10 of the scores go back to unknown and 0
 
E

Erebus

Guest
Hey,
So i found your thread and i'm pretty sure you followed the same tutorial on youtube as i did.
I actually had the exact same problem.

I was hoping to find the answer here initially but nobody replied. However as of now i have figured out what the problem is so i'll put it here to help people that stumble upon it in the future.

So here goes:

Basically your array only loads once the function fires. Yet you're trying to display the variable before the array has been set to the .ini file.
The fix is simple. You need to make sure you set the array that's being drawn (which are global.score_array[i, 0] and global.score_array[i, 1] equal to the values in the .ini file.)


Code:
  ini_open("scores.ini")
for (i = 0; i<10; i++) {
global.score_array[i, 0] = ini_read_string(string(i), "Name", "unknown" )
 global.score_array[i, 1] = ini_read_real(string(i), "Score", 0)
 //show_message(global.score_array[i,0])
 //show_message(global.score_array[i,1])
}
ini_close();
Basically paste this code on the create event of the object that draws the scoreboard. In you're case that's obj_draw_scores.

The code i pasted will read the .Ini file when you enter the room, before you run the code.
Idk where exactly you should put it on your version because i heavily altered my version of the tutorial but i suggest trying to paste it in a few places untill it works.

Like i said what this code does is set the variable's you're drawing to the screen equal to the values in your .ini file.
Your version most likely is calling the arrays before they have a value equal to something resulting in them not drawing from your .ini file.
 
Top