Config Files

From Oldunreal-Wiki
Jump to navigation Jump to search
Config Files
Topic Coding
Series Chimeric

In this tutorial we're going to see how to handle INI config files in your code.

Overview

Unreal has a very simple method for allowing UnrealScript classes to store and load persistent data outside of the default properties of that class. This becomes useful for exposing allowing your classes to be configurable via editing the appropriate .ini file without having to recompile anything, and is also an easy way to save (limited) persistent data for your classes. By use of the config keyword, you can specify what variables should be stored and in what file.

Using Config Files

Take a look at the following sample class:

class MyClass extends Actor;

var string MyString;
var int    MyData[24];

defaultproperties
{
}

Quite a boring little class with just a couple variables, nothing special. Now say we want to make the contents of MyString accessible to a config file, we will need to change to the following:

class MyClass extends Actor config; // note the config in the class declaration

var config string MyString;  // and note the config in the var declaration
var int    MyData[24];

defaultproperties
{
}

Just by adding the config keyword, we have told Unreal that (a) this class has data to be stored in a config file, and that (b) we want the MyString variable specifically to be stored/loaded. Note that only MyString will be stored in the preceding declaration, you must place the config keyword in front of every variable you want stored. Now all that's left is to make an entry in the config file (Unreal.ini will be the default) and add a value for MyString.

[MyPackage.MyClass]
MyString="This is a test string"

And that is all there is to it - Unreal will automatically load this value and set it properly for every instance of MyClass that we make. Now say we want to save our data from within the code, we would implement a function similar to this:

function SaveMyString(coerce string newString)
{
	MyString = newString; // set new value
	SaveConfig();         // this saves all config variables in this class to the .ini
	// in this case only MyString will be saved
}

Now whenever the SaveMyString() function is called MyString is set to the new string and then saved to the config file, to be loaded for future use. You can also change what file the data is saved in by modifying your class declaration to something like this:

class MyClass extends Actor config(Test);  // notice the (Test)

This will cause your class to store all of it's config data in Test.ini now, and you can specify whatever file name you want. There is also the ResetConfig() function, which will reset the config variables in your class to the values stored in the config file, sort of a load function.

External links

<references />

See also

Chimeric tutorials