Thursday, June 21, 2012

Platformer Kit: SPG_GameInfo class (UnrealScript)

The main adjustments of the SPG_GameInfo class were done to enable the use of Archetypes of the player's Pawn and Weapon classes.

This class contains the references of the Archetypes in the DefaultPawnArchetype and DefaultWeaponArchetype variables.
class SPG_GameInfo extends SimpleGame;

// Variable which references the default pawn archetype stored within a package
var() const archetype Pawn DefaultPawnArchetype;
// Variable which references the default weapon archetype stored within a package
var() const archetype Weapon DefaultWeaponArchetype;

If you do not know the GameInfo class, take a look at my article, "GameInfo Class (UnrealScript)".

The SPG_GameInfo class overrides the SpawnDefaultPawnFor() function so that the instance of the player's Pawn class is created based on the Archetype.
function Pawn SpawnDefaultPawnFor(Controller NewPlayer, NavigationPoint StartSpot)
{
    local Rotator StartRotation;
    local Pawn SpawnedPawn;

    // Quick exit if NewPlayer is none or if StartSpot is none
    if (NewPlayer == None || StartSpot == None)
    {
        return None;
    }

    // Only the start spot's yaw from its rotation is required
    StartRotation = Rot(0, 0, 0);
    StartRotation.Yaw = StartSpot.Rotation.Yaw;

    // Spawn the default pawn archetype at the start spot's location and the start rotation defined above
    // Set SpawnedPawn to the spawned
    SpawnedPawn = Spawn(DefaultPawnArchetype.Class,,, StartSpot.Location, StartRotation, DefaultPawnArchetype);

    // Return the value of SpawnedPawn
    return SpawnedPawn;
}

The SpawnDefaultPawnFor() function uses a local variable of type Rotator called StartRotation to get only the rotation on the yaw axis from the StartSpot of the game. For more information about rotation, read my article: "Using Rotators in UnrealScript".

After that, it creates a new instance of Pawn based on the Archetype "DefaultPawnArchetype" using the Spawn() function.

Another function that is overridden in the SPG_GameInfo class is the AddDefaultInventory(). In this function is obtained a reference of the SPG_InventoryManager class in order to create the weapon based on the Archetype DefaultWeaponArchetype.
event AddDefaultInventory(Pawn P)
{
    local SPG_InventoryManager SPG_InventoryManager;

    Super.AddDefaultInventory(P);

    // Ensure that we have a valid default weapon archetype
    if (DefaultWeaponArchetype != None)
    {
        // Get the inventory manager
        SPG_InventoryManager = SPG_InventoryManager(P.InvManager);
        if (SPG_InventoryManager != None)
        {
            // Create the inventory from the archetype
            SPG_InventoryManager.CreateInventoryArchetype(DefaultWeaponArchetype, false);
        }
    }
}

In the defaultproperties block are specified the Archetypes of the Pawn and Weapon, besides the classes responsible for the HUD and the PlayerController.
defaultproperties
{
    // What player controller class to create for the player
    PlayerControllerClass=class'SPG_PlayerController'
    // What default pawn archetype to spawn for the player
    DefaultPawnArchetype=SPG_PlayerPawn'StarterPlatformGameContent.Archetypes.PlayerPawn'
    // What default weapon archetype to spawn for the player
    DefaultWeaponArchetype=SPG_Weapon'StarterPlatformGameContent.Archetypes.LinkGunWeapon'
    // What HUD class to create for the player
    HUDType=class'SPG_HUD'
}