quarta-feira, 22 de outubro de 2014

Arrays and For loops in Blueprints

The use of Arrays allows grouping of variables of the same type. Creating an Array in Blueprints is very simple. Create a new variable and select the desired type. Beside the type of the variable there is an icon that must be clicked to make the variable an array, as in the image below:

To understand the use of an Array we will implement a simple Blueprint that turn on and off a set of lamps (Point Lights) when the player touches the Blueprint.

Create a new blueprint and create a new variable called "Lamps". This variable must be an array of type "Point Light". Select the "Editable" option so we can define in the Level Editor which will be the "PointLights" that will be handled by this Blueprint. The "Lamps" variable looks like this:

In the Components mode of the blueprint, you must add the "Box" component, so the Blueprint can identify the collision with the player.

After adding the "Box" is necessary to uncheck the "Hidden in Game" property so that it appears during the game. Let's keep it that way to simplify the test.

The actions of this Blueprint will be executed when the event "Actor Begin Overlap" occur. The "ForEachLoop" Action performs the Action "Toggle Visibility" for each element of the "Lamps" Array:

Click to enlarge

The "ForEachLoop" Action takes as input parameter an Array and performs the set of actions that are associated with the output pin "Loop Body" for each element of the Array that can be obtained from the output pin "Array Element". For example, if the array has four elements, the actions of the "Loop Body" will be performed four times. After that the execution flow is directed to the output pin "Completed".

To test this Blueprint, we need to add some "Point Lights" on the Level:

Place several "Point Lights" near each other. Add the new Blueprint to the Level in a location near the "Point Lights". In the "Details" tab of the Blueprint look at the variable "Lamps". 

Is it possible to add elements to the Array by clicking the icon with the "+" symbol. For each element added there is a ComboBox that lists the "Point Lights" that are part of the Level. The image below shows my example with 4 Point Lights selected.

Note that the index of an Array starts at zero. So an Array with 4 elements occupy positions 0, 1, 2 and 3 of the Array. The Point Lights that are part of this Array will be turned on and off by the Action of the Blueprint. To test, just run the game in the editor and touch the Blueprint using an Actor to activate the event "Actor Begin Overlap".

An Array and the "For Each Loop" can be used together with the action "Get All Actors of Class" to manipulate all Actors of a certain Class/Blueprint. Imagine that our Blueprint manipulate all Point Lights of a level, in this case it would not be need to manually specify the Point Lights. The script below shows how to toggle the visibility of all Point Lights of a Level:

Click to enlarge

The "Get All Actors of Class" action returns an Array of type "Actors" containing references to all the actors that are of the class that was selected in the "Actor Class" option. This Array can be used in the Action "For Each Loop" to loop through all the elements in the array. You must do the "Cast" of each element of the Array in order to access the actions of the "Point Light" class.

Let's do another example to show the use of another type of "For Loop". In this example we draw the number of lives of the player as icons on the screen, the image below shows the player with 3 lives.

To make this example it is necessary to create a Blueprint based on the HUD class and import an image that will be used to represent the lives on screen.

We will use a "For Loop" to draw the images on the screen. The number of times the image is drawn is equal to the number of lives of the player represented by the integer variable "Lives":

Click to enlarge

Next: Using Level Blueprints
Prev: Switchs and Enumerations in Blueprints
Table of Contents