Pages

terça-feira, 2 de dezembro de 2014

Tick Event and Latent Actions in Blueprints

The Unreal Engine 4 has an event called "Tick" that is generated every frame of the game. For example in a game that is running at 60 frames per second, the "Tick" event is generated 60 times in a second.

The "Tick" event provides a value known as "Delta Seconds" that is the elapsed time since the last frame. Using the Event "Tick" we can have precise control over the movement of a Blueprint in the game.

To illustrate the use of "Tick" Event, let's create a simple example that moves an Actor along the X axis. The speed of movement is stored in a variable of type float called "SpeedX". The default value that I'm using for this variable is 100, which is the speed of 100 centimeters per second, because in the Unreal Engine 4, one Unreal Unit is equals to one centimeter. (1 uu = 1 cm)

The script of this example became:

Click to enlarge

The Action "Add Actor World Offset" changes the location of the Actor based on the value of the input parameter called "Delta Location", which is a Vector containing the position values in the X, Y and Z Axes. In this example we only want to modify the X Axis, so the values of Y and Z Axes are zero.

We need to multiply the value of "Delta Seconds" by the speed that is in the variable "SpeedX" to find the value equivalent to the small step that the Actor need to move in the X axis for each frame.

To better understand this calculation let's do a simulation assuming the game is running at 50 frames per second. To calculate the value of "Delta Seconds" just divide 1 second by the number of frames:

Delta Seconds = 1 sec / 50 
Delta Seconds = 0.020 sec (equivalent to 20 milliseconds)

Now to find the X value that the Actor will move every frame, just multiply the value of "Delta Seconds" by the value of "SpeedX":

X = SpeedX * Delta Seconds
X = 100 cm/sec * 0.020 sec
X = 2 cm

On average, the Actor will move 2 cm every frame in this simulation.

The Unreal Engine 4 has a type of Action known as Latent. Latent Actions run in parallel to the normal flow of the script of a Blueprint. Thus, the conclusion of a Latent Action can occur several frames after its start.

The simplest and most common Latent Action is the "Delay" function, which performs the actions connected to the pin "Completed" only after passing the time contained in the parameter "Duration". The image below shows the action "Delay" with a brief description:


Another example of Latent Action is the "Timeline", which will be the topic of the next article.

To illustrate the use of "Delay" function, we will create a script that calculates in a simple way the frame rate that the game is running.

To begin, create a new blueprint containing the three integer variables that are in the image below. All of them are marked as "editable" for easy viewing of their values in the editor. I created a category called "FPS" to group these variables.


The script uses the "Tick" event together with the "Delay" function. The variable "Frames" counts the number of frames that have already been rendered. We use the "Delay" function to calculate the frame rate every 5 seconds.

The calculation is based on the subtraction between the variables "Frames" and "Checkpoint" that represents the number of frames that passed in 5 seconds. This result is divided by 5 to find the average frame rate per second. Then we store the current value of the variable "Frames" in the variable "Checkpoint" which will serve as a reference for the next calculation.

Here is the script of the "Tick" Event:


When running this script we can see that the flow of execution does not go through the "Delay" function until finish the internal count that is displayed in the editor:


The image below shows some values of the variables during the execution of my example which calculated an average of 32 frames per second.


Due to the parallel feature of the Latent Actions, they can not be used within a Blueprint Function. The image below shows the compile error generated when trying to use a latent action within a function that I created with the name "Test Latent".



Next: Using Timelines in Blueprints
Prev: Using Event Dispatcher in Blueprints
Table of Contents