Unreal Development Kit Game Programming with UnrealScript:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – Creating config variables

  1. To let the game know that our class needs to save config variables, first we need to let it know which file to use.
    class AwesomeActor extends Actor
        placeable
        config(Game);

    This tells the game that our class' config variables will be defined in the Game ini files as opposed to Engine or Input and so on.

  2. Now, let's make a config variable.
    var config int MyConfigInt;

    Config vars can have parentheses to let level designers change them, but they can NOT be put in the default properties block. Doing so will give a compiler error. Instead, we define their default properties in the INI file we specified. Since we used Game, we would put the default in DefaultGame.ini. Let's open that up now.

  3. In DefaultGame.ini we can see a bunch of different sections, starting with a line surrounded by brackets. The inside of these brackets specifies the package and class that the section is defining defaults for, like this:
    [Package.Class]
  4. In our case our package name is AwesomeGame, and the class we need to define is AwesomeActor. At the end of DefaultGame.ini, make a new section surrounded by brackets.
    [AwesomeGame.AwesomeActor]
  5. Right after that we can define any default values we need.
    MyConfigInt=3

    Once we're done, our section should look like this:

    [AwesomeGame.AwesomeActor]
    MyConfigInt=3
  6. Let's see if it works! In AwesomeActor.uc, change PostBeginPlay to log MyConfigInt.
    var config int MyConfigInt;
    
    function PostBeginPlay()
    {
        'log("MyConfigInt:" @ MyConfigInt);
    }
  7. Compile and run, then check the log file.
    [0008.66] ScriptLog: MyConfigInt: 3

Have a go hero – Editable configurable variable?

Knowing what you know about different ways to define default values for variables, what do you think would take precedence: The config file or a value set by the level editor? Try adding a variable that's both configurable and editable and logging the result.

What just happened?

If we look in UDKGame.ini, we can see that the variable has shown up there as well. Remember that the UDK.ini files are built from the Default.ini files, and instead of changing the Default.ini files, the player and the game work with the UDK.ini ones. That way the game always has a fail safe with the Default.ini files. If the player or a setting menu in the game changed MyConfigInt to 5 for example, then the player changed their mind and used a settings menu to reset everything to the default value, we would be able to do that by using the Default.ini value for that variable.

Now that we've learned about the different types of variables and ways to set their values, let's take a look at some common operators we can use on our variables.