A Neat Player Controller Trick

Of all the UnrealScript classes, GameInfo and PlayerController are perhaps the most important. One handles nearly all aspects of the game being played, while the other handles many aspects of controlling a player or a character. With this in mind it makes sense that these classes often reference each other (and almost inevitable, as I quickly found out while coding my latest project).

It’s relatively easy to grab a reference to GameInfo from a Controller just by doing MyGame(WorldInfo.Game), but how about the other way around? Not quite as easy because many controllers may be operating in the world at the same time; if you wanted a specific controller, you’d likely have to iterate through WorldInfo.AllControllers().

But suppose you were making a single-player game? Then you’d only ever have one controller to keep track of, so you could do something like this in the Controller class:

simulated event PostBeginPlay()
{
super.PostBeginPlay();
MyGame(WorldInfo.Game).RegisterController(self);
}

and this in the GameInfo class:

var MyPlayerController MyController;

function RegisterController(MyPlayerController PC)
{
MyController = PC;
}

This offers easy access to the player’s controller, and it’s how I solved a number of nasty little problems in my game.

Advertisements

One thought on “A Neat Player Controller Trick

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s