domingo, 29 de junho de 2014

GameHUD: Timer and Custom Event

The sample game has a time that is stored in the "Time" variable. This variable starts with the value 30 and decreases by one unit every second.

The value of this variable is drawn on the screen. A later article will show the actions of GameHUD that draw variable values on the screen.

A Timer and a Custom Event are used to decrease the value of the variable "Time" every second.

In blueprints there is an action called "SetTimer" that defines a Timer. To find it click the right button on the EventGraph of the GameHUD and type "timer" in the search box. The image below shows the options for actions related to "Timer".

A Timer is programmed to perform a given function (or custom event) after finishing the specified time.

In the previous article that defined the contents of the Macro "StartGame", we saw that the "SetTimer" action was called to initialize a Timer with the following values:

This Timer will continuously call a Custom Event named "Clock" every second.

The meaning of the parameters of the "SetTimer" function are as follows:
  • Object : Object that contains the Timer. The default value is the current blueprint.
  • Function Name : Function name (or custom event) that will be called when the time runs out.
  • Time : Time in seconds of the length of the Timer.
  • Looping : Indicates whether the Timer will continue its execution or executed only once.

Now we need to create the Custom Event named "Clock".

To create a Custom Event, right-click on the EventGraph, expand the "AddEvent" category and select "Custom Event..." as the image below. Put the event name as "Clock".

This new "Clock" event has the following responsibilities:
  • Decrease by one the value of the "Time" variable;
  • Check if the "Time" variable is zero. If true, do the following actions:
      • Define the Boolean variable "GameOver" with the value "true";
      • Clear the Timer to stop calling the "Clock" event;

The "Clock" Event should be this way:

Click to enlarge