Friday, October 12, 2012

Cookbook: Referencing objects

"I'm trying to call a certain function of Controller from a different class. How can I do it?"

There are situations when you need to call a method of a different class (update stats, test some conditions, display something on the HUD) from your custom actor. As in UDK the centerpiece of all action handling is  the Controller class, you may need to store reference to it in your actors that change somehow gameplay status.

To set this reference you can use a simple iteration approach (iterate over all actors of some class):


   1:  var array<CustomActor>    Actors;
   3:  //========================================================================
   4:  event PostBeginPlay()
   5:  {
   6:     local CustomActor actor;
   8:     foreach WorldInfo.DynamicActors(class'CustomActor', actor)
   9:     {
  10:      actor.Controller = self; //set myself as controller
  11:          Actors.AddItem(actor); //store reference
  12:     }
  13:  }
  14:  //========================================================================
  15:  function Disable()
  16:  {
  17:     local PuzzleActorBase actor;
  19:     //disable all parts (use stored references)
  20:     foreach Actors(actor)
  21:     {
  22:      actor.DisableActor();
  23:     }
  24:  }

This way you can later call methods of you controller when actor is touched/triggered/hit, etc.:

   1:  var CustomHandler Controller;
   3:  //========================================================================
   4:  function UpdateTriggered() //some method
   5:  {
   6:     if (Controller != none)
   7:     {
   8:        //use reference
   9:        Controller.TriggerAction(self);
  10:     }
  11:  }

This can be of course generalized and reused for other situations. Let's say we have a handler class that stores references to all objects of some type (or inside some volume) and evaluates some logic (push platforms that are a part of some puzzle and handler that checks if they are pushed in some order, etc.). This handler can also iterate over all necessary objects and store references to desired objects....

No comments:

Post a Comment