GML ini load questions... if "file exists"

M

Mythi.T

Guest
Hello I have some questions about ini files

in my code I have this:
Code:
if !file_exists("aiSide.ini"){
    needsaves_side = 1
}
else needsaves_side = 0

if needsaves_side = 0{
    ini_open("aiSide.ini");
    global.testOneGen = ini_read_real("testOne", "gen", 0)
    global.testOneGrade = ini_read_string("testOne", "grade", "unsure...")
    global.testOneHigh = ini_read_real("testOne", "high", 0)
    ini_close()
}
Do I need to have the "file exists" if statement? will it work? Can I remove it and add something else? what would bet hat something else? if you can solve/answer these questions that would help bunches.

also must I do this "file exists" if statement saving, not just loading?

SITUATION (so I don't have to explain to the questioners later)
Loading is automatic when the game starts
Saving is automatic upon level end
I Know how to read/write inside ini files, I just don't know how if there will e an error because I never made a ini file prior

Thanks
 
B

brupibo

Guest
I think you could check if key exists, instead of file, because you're going to create it anyway.
Use ini_open and check if testOne key exists (don't remember exactly the code). If it does, make the loading logic, if doesn't, make it write a default value for the keys.
 
C

CedSharp

Guest
From the documentation :


So basically, when you read a value from an ini file using the ini_* scripts, you don't need to check if it exists because the other ini_ scripts allow you to provide a default value if the key doesn't eixsts.

Also not that until you actually write a value, the file will NOT be created, so you can read a file that doesn't exists without creating one!

In other words:
Code:
ini_open("aiSide.ini");
if(ini_read_string("testOne", "grade", "") == "") {
  // The ini file doesn't exists or wasn't saved properly
}
else {
  // The ini file exists
}
You only need to use the if condition if you want to avoid loading anything if the file doesn't exists.
Since the ini_ scripts have a default value parameter, then you can load data even if the file doesn't exists, the scripts will simply return the default value.

This was specifically made to make it super easy to load/save data with GameMaker.

I hope this answers your question.
 
Top