UnrealScript Game Programming Cookbook

(Chris Devlin) #1
Chapter 5

131

class WanderBot extends GameAIController;

We only need one global variable, and that's TargetLocation.

var Vector TargetLocation;


  1. We'll need to override the GameAIController's PostBeginPlay() function and add
    the SetTimer() function. This tells our pawn to run the MoveRandom() function
    every 2.5 seconds. Without this, our bot would constantly be searching for a new
    target location without ever taking a break.
    /*=======================================================


* Called right after the map loads
========================================================*/
simulated event PostBeginPlay()
{
/** Calls all of the PostBeginPlay functions from
parent classes */
super.PostBeginPlay();
/** Frequency at which the bot will begin looking for a
new path */
SetTimer(2.5, true,'MoveRandom');
}

The timer creates a more natural movement. For example, if you wanted your bot to
move to a random location, and twiddle his thumbs for 5 seconds before contusing,
this function is what would allow that to happen.


  1. The Possess event tells our pawn to begin moving as soon as the map loads.
    SetMovementPhysics() is key here, as it automatically makes the pawn walk.
    Without this our pawn would need some sort of impulse, such as receiving damage,
    before moving.
    /*=======================================================


* Forces the pawn to begin moving as soon as the map
loads
=======================================================*/
event Possess(Pawn inPawn, bool bVehicleTransition)
{
super.Possess(inPawn, bVehicleTransition);
Pawn.SetMovementPhysics();
}


  1. We need a function to handle all of the math for where our bot currently is, and
    where we want it to be. We're using local variables for the offset, as we won't need
    them outside of this function.

Free download pdf