Wednesday, December 4, 2013

[TUT] Settings file - Saving and loading .INI config info

These days i wanna show how you can load and save configuration in .INI files, this is quite valuable to set customizable factors in your script like Hotkeys, timeouts, offsets, and so forth..

Download the sample project here, let's use this project as the base for this tutorial.

  • Reading information

Let's start off with the basic, the simple is the ScriptHook native Settings object. to read data from the script default INI file we can use the following strategies:


As you can see we have distinct types of return, i generally use GetValueString because its easier to manage.
These approaches require some parameters:

OptionName: Set the name of the selection in the INI file
Category: Set the category exactly where the option will be
DefaultValue: Set the default value that will be returned when the selection never exists in the INI file

You can skip the Category parameter, but its not great concept.

Keep in mind that the structure of an INI file is some thing like:



So, to study an string from the INI file we can use:

The same notion with Integers, floats, and so forth..

  • Writing data
We have only one method to write data in the INI file:


We can use any of the Reading varieties in this method, the strategy will detect and create the correct information. The params necessary are:

OptionName, Category and the Value that can be a single of the types  listed in "Reading data" section of this tutorial.

Right after writing into the INI file we need to use the technique Save to save the info in the INI file, if the file never exists it will be automatically developed:


The name of the file will be the name of the script file significantly less .net.dll/.vb/.cs element.

In my scripts i constantly attempt to keep all settings offered in .INI file with no the need of release the mod with the INI file, to do this i constantly verify if the selection exists in the INI file, if never exists i set my default value. to do this i use an strategy:

This strategy will attempt to study an String from the INI, if nothing is located it will set in the INI the default value, then study it again and return the outcome as String. 

To use i get in touch with it like this:

We can use this approach to study double and integer values as well, we just require to convert the result to the preferred type:

To study Crucial worth i use yet another strategy that will constantly return an Key:

making use of it:

The variable need to be an Keys variety:

  • One more way to study and create
When i need to have to study and create INI file with custom names like i do in Air Combat IV exactly where i want to study ini files that are inside folder Jets and the names are custom (armor_jet_name.ini) i use the following strategies:

As you can see we want to import the Technique.Text to use the StringBuilder object :)
We are utilizing windows API's to read and write data from the INI file.

This strategy calls for that the ini file already exists, so i use this method to confirm the INI file existence before use it:

Also i use an International string variable (iniFile) to be the INI file name that im making use of, you can modify the approaches to add this as parameter but i favor to use this way. You should use the complete path to decide the INI file, it can be relative: 

iniFile = ".ScriptsMyCustomINI.ini"

or absolute:

iniFile = "C:Program FilesRockstar GamesGrand Theft Auto IVScriptsMyCustomINI.ini"

each performs fine :)

Example of use:

Download the script of this tutorial here
[TUT] Settings file - Saving and loading .INI config info
9out of 10 based on 10 ratings. 9 user reviews.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.