Showing posts with label event. Show all posts
Showing posts with label event. Show all posts

Wednesday, October 3, 2012

Cookbook: UDK timers

Timers in UDK are very useful if you want to call a postponed method (e.g. in 5seconds) or call something regularly (e.g. doing some statistics update each 10 seconds).

Syntax is very simple:


   1:  //set timer: (time to trigger,
   2:  //            true - repeated/false - once only,
   3:  //            method name to call in '')
   4:  SetTimer(2.0,,'MyTimerName');
   5:   
   6:  //stops timer execution
   7:  ClearTimer('MyTimerName');

Let's take a look at a simple example. Let's say we want to have a button that resets itself after given amount of time and additionally updates HUD with time left regularly (for HUD messaging see previous tutorial Cookbook: UDK cascading HUD messages, for kismet events see previous tutorial Cookbook: Triggering events, uscript -> kismet ).


   1:  class ButtonActorTimer extends ButtonActor;
   2:   
   3:  /** time for button to stay activated (then reset) */
   4:  var(Game) float fTimeToReset;
   5:   
   6:  /** time for tick (display message) */
   7:  var(Game) float fTimeToTick;
   8:   
   9:  var float fTimeLeft;
  10:   
  11:  //========================================================================
  12:  function TimeOut()
  13:  {
  14:      //trigger event with myself as instigator
  15:      self.TriggerEventClass(class'SeqEvent_ButtonReset', self);
  16:      Reset();
  17:  }
  18:   
  19:  //========================================================================
  20:  function TimeTick()
  21:  {
  22:      fTimeLeft -= fTimeToTick; //update time
  23:      Controller.StringToHUD(fTimeLeft$"..."); //show HUD message
  24:  }
  25:   
  26:  //========================================================================
  27:  function Set()
  28:  {
  29:      SetTimer(fTimeToReset,,'TimeOut'); //set new reset timer (called once)
  30:      fTimeLeft = fTimeToReset;
  31:      SetTimer(fTimeToTick,true,'TimeTick'); //ticking message (repeatedly)
  32:  }
  33:   
  34:  //========================================================================
  35:  function Reset()
  36:  {
  37:      ClearTimer('TimeOut'); //reset timer!
  38:      ClearTimer('TimeTick');
  39:  }

Button has two independent timers, one for reset (re-enable button, and send reset event to kismet) after time is up (door closes, etc.), second timer is for HUD update and sends time left message onto HUD.

Here is some ingame screenshot:


Monday, October 1, 2012

Cookbook: Triggering events, uscript -> kismet

Imagine you want to have a custom unrealscript actor (e.g. button). Obviously you want that button to introduce some interactivity to your scene/level. Let's say you want it to trigger some event for kismet (universal event that you want to be able to hook to any action available - play a matinee movie, toggle light, etc.)

What do we need?

1. create simple Seq_Event

All we need is a super simple event without any internal functionality. It's a good habit to organize it into some category (ObjCategory).


   1:  class SeqEvent_ButtonSet extends SequenceEvent;
   2:   
   3:  defaultproperties
   4:  {
   5:      ObjName="ButtonSet"       //kismet event name
   6:      ObjCategory="MyGame" //kismet event menu category
   7:      bPlayerOnly = false       //only pawn based actor can trigger?
   8:  }


2. trigger it from script

In our ButtonActor we need to trigger the given event somewhere. In this example it is triggered whenever touch event occurs (it'd be better to have some checks whether it's triggered by our pawn, some specific actor, etc.). To make our life easier, event is added to SupportedEvents in defaultproperties (this way it'll show in kismet right click menu when buttonActor is selected in scene).



   1:  class ButtonActor extends Actor
   2:      ClassGroup(MyGame) //group for actor classes menu (content browser)
   3:      placeable;
   4:   
   5:  ...
   6:   
   7:  //========================================================================
   8:  event Touch( Actor Other, PrimitiveComponent OtherComp, vector HitLocation, vector HitNormal )
   9:  {
  10:      //trigger event with myself as instigator
  11:      TriggerEventClass(class'SeqEvent_ButtonSet', self);
  12:  }
  13:   
  14:  ...
  15:   
  16:  defaultproperties
  17:  {  
  18:      SupportedEvents.Add(class'SeqEvent_ButtonSet') //to see it in kismet (selected actor, add event using actor...)
  19:  }

3. connect it to some kismet action

Here we need to insert an instance of our button into scene and (with button selected) in kismet right click Add event using actor -> you'll see our new event in submenu, add it! After that, all that is left is to connect it to whatever action you want. Example image shows button that plays open the door animation and turns matching color lights connected to door and button off.