Variables

First up, are variables. Since we assume you have some previous programming experience, we won't be explaining the difference between an int and a float, but, at the same time, switching from traditional text to UE4's node-based visual programming can sometimes leave you a bit frustrated when trying to do basic things. Hopefully, I will be able to help with that part.

Pressing the plus sign on the right-hand side of the Variables section will create a new variable. By default, this will be a Boolean (red), or it will be the same type as the previously modified one if you've already created one:

If you click on a selected variable again, you can rename it. To the right of the variable is a shortcut for setting this variable to public or protected (it defaults to protected).

When a variable is selected, you will also see a Details panel on the far right-hand side of the Blueprint window:

Here, we will be able to set some properties for our variable:

  • Variable Name: This field is another way we can set the name our variable uses. Variable names can include spaces.
  • Variable Type: This covers everything from floats to vectors, enums, structs and actor references. Any custom Blueprint classes that we've created will be selectable as well. The popup also has a search bar to make it easier to find the class you're looking for. If you're selecting an object, hovering over it will give you the option of selecting between an actual object reference or just a class reference if you need that instead (for example, a variable that determines what type of weapon a player will spawn with).
  • Instance Editable: This sets whether the variable is public or protected.
  • Blueprint Read Only: This stipulates whether you can set this variable or only read from it (effectively, a const variable).
  • Tooltip: This will show a bit of text when you hover over the variable in the Variables section of the My Blueprint tab. This is particularly helpful when the Variable Name isn't self-explanatory.
  • Expose on Spawn: This allows this variable to be initialized when the object is created. It is equivalent to initializing variables in the constructor in C++.
  • Private: Although Private and Instance Editable can both be checked at the same time, Private takes precedence. If it is checked, child Blueprints will not be able to access this variable even if Instance Editable is checked.
  • Expose to Cinematics: This allows the variable to be changed within level sequences so that it can be changed during cutscenes or cinematics.
  • Category: This is incredibly useful for organizing your Blueprint classes. As your game's development progresses, some classes can have a lot of variables. This will let you organize them within the Variables section of the My Blueprint tab. Typing a new name into this box will create a new category.
  • Slider Range: If you click and hold the left mouse button in the variable's value box (where it says 45.0 in the preceding pic), you can adjust the value by moving the mouse left and right. Setting the Slider Range allows you to limit the range of this variable when adjusted this way. This will only apply to certain variable types.
  • Value Range: This sets the clamp values for the default value of this variable. This overrides the Slider Range if those values are outside the Value Range. Note that this only affects the default value of this variable. It can still be set to values outside this range in code. If you need the value clamped at all times, the best way to do it would be to make the variable Private and use custom Set/Get functions. (Creating functions is discussed after this Variable section).
  • Replication: In multiplayer games, this is used to set the replication property of the variable. Using variable replication will be discussed in Chapter 7Multiplayer Games.
There are a few other uncommon properties beneath the down arrow in this variable section for controlling things, such as config and transient variables. More information about those can be found in the UE4 documentation online: https://docs.unrealengine.com/en-us/Engine/Blueprints/UserGuide/Variables.

One final property that can be changed for variables is turning them into an array, set, or dictionary. This can be done by clicking on the icon to the right of the Variable Type:

Now that we know how to create our own variables and modify their properties, let's take a look at how they are used in Blueprint code.