quarta-feira, 12 de agosto de 2015

Using structs in Blueprints

In this article we will learn more about "structure" (struct). We have already used various structures in our Blueprints without knowing what they really are.

In a simplified way, a Blueprint consists of "Data", "Actions" and "Components". However, sometimes we just need to gather in one place several variables. Rather than create a Blueprint containing only "Data", we can create a structure.


An example of a widely used structure is the Vector. A Vector is a structure containing three variables of "float" type with the names X, Y and Z. In the next articles we will better understand the mathematical concept of a Vector.

Open a Blueprint and in the EventGraph add the "GetActorLocation" function. The "Location" variable is a Vector. Right click on the "Return Value" and choose "Split Struct Pin":


This option will create an output pin for each element of the structure:


The variables that are part of a structure can be of different types. We can also have structures that are formed by other structures. An example is the "Transform" structure which has three variables that are structures.

The variables "Location" and "Scale" are of the "Vector" type and the variable "Rotation" is of the "Rotator" type which is also a structure. The image below shows the "GetActorTransform" function in its normal format and with the split structure:


We can even separate each of the structures that are part of the "Transform", the result would look like this:


To create a new structure, click the "Add New" button on the Content Browser, "Blueprints" category, "Structure" option:


Put the name of "ItemStruct" and double-click on the structure to edit its contents. Click the "New Variable" button to add variables to the structure. The image below shows a structure with three variables of different types.

Click to enlarge

On the right side you can set the default values of the variables. This structure that was created will be used as a new type of variable.

Create a new variable at any Blueprint. Put the name of "Inventory", the type is "ItemStruct". Click on the icon next to the type, to turn this variable into an Array. Check the "Editable" option.


Compile the Blueprint. In the "Default Value" category of the variable you can add elements in the array. Each element contains the variables defined in the structure:


When a structure is created, the Actions "Break" and "Make" of this structure are added for use in Blueprint. The Action "Break" gets a structure as input and separate its elements. The Action "Make" takes as input the separate elements and creates a new structure. The image below shows the Actions "Break" and "Make" of the structure "ItemStruct" we created:


The image below shows an example of using the Actions "Break" and "Make". Pressing the space bar, it modifies the Z value of the Actor's position that receives the value 0. The X and Y values are preserved.

Click to enlarge