Saturday, November 5, 2011

GameInfo Class (UnrealScript)

The GameInfo class defines the features and rules of the game. It's a kind of manager class that keeps references to another classes that are responsible for different aspects of the game. For example, the following code was extracted from the UTGame class that is a subclass of GameInfo.
//variables, functions, states definitions...

defaultproperties
{
    HUDType=class'UTGame.UTHUD'
    PlayerControllerClass=class'UTGame.UTPlayerController'
    ConsolePlayerControllerClass=class'UTGame.UTConsolePlayerController'
    DefaultPawnClass=class'UTPawn'
    PlayerReplicationInfoClass=class'UTGame.UTPlayerReplicationInfo'
    GameReplicationInfoClass=class'UTGame.UTGameReplicationInfo'
    DeathMessageClass=class'UTDeathMessage'
    PopulationManagerClass=class'UTPopulationManager'
    BotClass=class'UTBot'

    //...
}

Each game type in Unreal Tournament (Deathmatch, Capture the Flag...) is specified by a subclass of GameInfo, as can be seen in the class hierarchy below.


When we create and compile a new subclass of GameInfo it is automatically listed as a Game Type that can be accessed through the UDK Menu: View -> WorldProperties -> Game Type.


The Actor class can access the GameInfo object that represents the current game. This access happens through another class called WorldInfo. The WorldInfo class contains information regarding the current map/level and has a GameInfo variable called game. In previous tutorials, we were already using this GameInfo variable to write messages on the screen using its Broadcast() function as in the example below:
class MyActor extends Actor;

event PostBeginPlay()
{
 worldinfo.game.broadcast(self, "Using GameInfo");
}

There is only one problem, this variable game references the base class GameInfo. If we create a new subclass of GameInfo with new variables and new functions, the variable game will not be able to access the new variables / functions and will generate a compiler error if we try. To solve this problem, we need to cast the variable game to the subclass of GameInfo we are using.

Code sample:
//In MyGameInfo.uc:
class MyGameInfo extends GameInfo;

var vector vBonusPosition;

//In MyActor.uc:
class MyActor extends Actor;

var vector vGoal;

event PostBeginPlay()
{
   local MyGameInfo myGameInfo;

   //Cast the GameInfo object to MyGameInfo   
   myGameInfo = MyGameInfo(worldinfo.game);
   
   //Check if it works
   if(myGameInfo != None )
   {
     vGoal = myGameInfo.vBonusPosition;
   }
}

For more information about GameInfo class:
Gametype Technical Guide