Elvis Martini Elvis Martini - 1 year ago 136
Ini Question

How do I store C++ variables in an INI, CFG, or XML file?

My script.cpp file contains the following lines:

bool mod_enabled = true; //Enable/Disable
BYTE display_delay = 15; //Redisplay delay

Ideally, the values for mod_enabled and display_delay should be pulled from an external configuration file which the user can edit. (The code itself is being compiled into an GTAV ASI plugin, which runs at the start of the game.)

I have no need for saving or changing these values mid-game. What is the simplest solution?

Answer Source

Turns out the solution is simple.

INI file (XXX.ini):

mod_enabled=1           // Enable/Disable
display_delay=1         //Redisplay delay


bool mod_enabled = GetPrivateProfileInt("Settings", "mod_enabled", 1, "./XXX.ini") != 0;
int display_delay = GetPrivateProfileInt("Settings", "display_delay", 15, "./XXX.ini");

The !=0 is necessary to cast an integer to a boolean. Otherwise, Visual Studio will give you errors.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download