Unreal Engine 4 Game Development Quick Start Guide
上QQ阅读APP看书,第一时间看更新

Using variables in the Blueprint window

If you click and drag one of the variables from the My Blueprint tab on the left-hand side of the Blueprint window into the main Blueprint area and let go of the left mouse button, the editor will give you an option to create either a Get or a Set for that variable:

To make a connection from any node in a Blueprint window, just click and hold on any input pin (left side) or output pin (right side) and drag a connection to where you need it.

In certain cases, it is possible to drag connections between different variable types, such as Float | String or Int | Bool. If a conversion is available, it will automatically create the conversion node between the connecting points.

There is no limit to the number of connections from a variable node to other nodes, but, in some cases, variable inputs are limited to one connection. Some functions, such as Set Actor Hidden In Game, allow you to connect more than one variable to its input, however, so check whether this is the case to save time and maintain cleaner Blueprint code.

For Set nodes, you can connect a Get node's output or the output of some other operation into the set's input, or leave the input unconnected and hard code a value. (For example, the Set Float and Set Int nodes in the preceding screenshot will set the value to zero if no input is connected.) In most cases, you will want to connect a variable, but in a few cases, there will be a good reason to leave it unconnected:

  • String variables: Leaving the Set input unconnected will clear the current value, making it an empty string.
  • Actor references: Leaving the Set input unconnected will null the variable.
Note that Set nodes for an array must have an input connection. In order to clear an array, use a Get node, and create a Clear node to connect it to.

If you drag an output pin into an empty area and let go, the editor will offer up a list of possible operations and functions that can use it (including the Clear node for arrays mentioned previously). Most of the variable type-specific functions, such as NOT for Booleans, or math operations for ints and floats, can be found under the Math and Utility sections of this pop-up tab:

The popup will also have a search bar to make finding functions and operations easier. In some cases, these nodes can be found in multiple ways (typing if will bring up the branch node for Bools, for instance):

Also included in this list are any functions that take that variable type. For Bools, typing in Set Actor Hidden will bring up that function, for example:

By default, Context Sensitive is checked in this popup. This will limit the search to functions that can use that variable type and functions that are specific to the Blueprint class that you are using. If you are accessing a variable from another class, functions specific to that class will show. If you need to use a function and you know it exists, but it is not showing up in this list, unchecking this box will help you find it. Take care when doing this, however. If a function isn't showing up in the Context Sensitive list, you may be trying to use it incorrectly.

Another shortcut to use when working with variables is Promote to Variable. In the Set Actor Hidden In Game function call previously, if we didn't already have a Bool to connect to it, we could simply right-click the New Hidden input node and select Promote To Variable. This will automatically create and connect a new Bool to that input.

One final option when working with variables on the input pin is the Select functionality, which acts as a ternary operator (Condition ? UseIfTrue : UseIfFalse) but can have more than two outputs (acting more as a switch statement):

Dragging a connection off an input and typing Select will bring up this node as an option.

For the Index input, we can attach an int, byte, Bool, or any type of enum. Attaching a Bool will limit the input to two options for true or false. An enum will provide an input for every option in that enumerator. For ints and bytes, the options will start at 0 and go as high as you need (press the Add Pin button until you have the amount that you need).