Unable to read .ini file correctly.

N

Nightcore

Guest
Hello again, GMC.

I am in a bit of a hard spot right now concerning ini_read_real. The problem is, even though the file is created properly with ini_write_real upon closing the game, it reverts to the default values I have set for the global variables after relaunching it.

I've tried several things, but nothing works. If anyone can explain what I'm doing wrong, that would be appreciated.
 

obscene

Member
We probably can't even guess what you're doing wrong without seeing your code, other than saying that make sure you read your INI file AFTER you initialize your variables.
 
N

Nightcore

Guest
For reference, here is the code I am currently using.

First, the globalvars I have set:
Code:
globalvar lvl;
globalvar max_xp;
globalvar xp;
globalvar current_xp;
globalvar hp;
globalvar max_hp;
Next, the code for creating and storing values inside an .ini file:

Code:
ini_open("player.ini");
ini_write_real("Player Values", "XP", xp);
ini_write_real("Player Values", "Max XP", max_xp);
ini_write_real("Player Values", "Base Level XP", current_xp);
ini_write_real("Player Values", "HP", hp);
ini_write_real("Player Values", "Max HP", max_hp);
ini_close();
And, finally, the code for reading from the .ini file:

Code:
if file_exists("player.ini")


{
ini_open("player.ini");
ini_read_real("Player Values", "Current XP", xp);
ini_read_real("Player Values", "Max XP", max_xp);
//current_xp = ini_read_real("Player Values", "Base Level XP", current_xp);
ini_read_real("Player Values", "Current HP", hp);
ini_read_real("Player Values", "Max HP", max_hp);
ini_close();
}

else

if !file_exists("player.ini")

{

lvl = 1;
xp = 0;
max_xp = 100;
hp = 100;

};

if (xp >= max_xp)
{
    lvl +=1;
    current_xp = max_xp;
    max_xp = max_xp + (max_xp * 0.35);
    max_xp = round(max_xp);
    max_hp = max_hp + 10;
}
}
 

Bingdom

Googledom
ini_read_real/string returns a value. Assign it to a variable. It doesn't magically set a variable.

The commented code line is the correct way to do it.

Also, the names you gave it for reading is incorrect. It must match the same name as you have written it.
 

andev

Member
As Bingdom has said, you are not putting what's read in the variable.
Code:
ini_read_real("Player Values", "Max HP", max_hp);
The last argument where you put "max_hp" is the 'default' value, which is what you want max_hp to be set to if the file is missing. So maybe you want:
Code:
max_hp = ini_read_real("Player Values", "Max HP", 100);
 
N

Nightcore

Guest
So, wait, what values am I missing, exactly? I can see that perhaps the values are being defaulted by the last bit in each line, but how do I get it to properly read the values from the .ini file?
 
Top