Sniper's Paradise!


Config Files

Using Config FilesUnreal 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. 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 (UnrealTournament.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.



Spam Killer

Back To Top
2005 Sniper's Paradise
All logos and trademarks are properties of their respective owners.
Unreal™ is a registered trademark of Epic Games Inc.
Privacy Policy
Website by Softly
Powered by RUSH