When you’re working with Unreal Engine and doing programming or visual scripting in Blueprints, it’s really important to know how to use a special tool called ‘Variable‘ that can save and change information.
In this lesson, we’ll explore everything about Unreal Engine variables, especially how they work with blueprints.
What is a variable?
A variable is any data or information that can be stored and changed.
Variable are a type of data storage container that can hold a value or a reference to an object. Then, these containers may be modified or referenced.
Imagine you’re getting ready to move to a new house. You have a bunch of stuff to pack into boxes. Now, each box is like a special container that can hold different things. These boxes have labels on them, like ‘toys’, ‘books’, and ‘clothes’.
Now, here’s where the idea of a variable comes in: think of a box that doesn’t have a label yet. It’s just an empty box. You can use this box to put in whatever you want, like toys, books, or clothes. This box can change and hold different things at different times. We call this box a ‘variable‘.
So, in programming or when working with computers, a variable is like that empty box. It can hold different types of information, like numbers, words, or even unreal engine actors, and you can change what’s inside it whenever you need to. Just like you can use your empty moving box for different things, a variable is a way to store and use information in a computer program.
Why do we need Variable Types?
To make it easier to understand, let’s continue with the previous example.
When you’re packing for a move, you have specific types of boxes for different things. You use big boxes for things like clothes, medium boxes for books, and small boxes for fragile items. You don’t use a giant box for just a few small toys, right?
In programming, it’s similar. We have different types of boxes, or ‘variables’, to hold different types of information.
Now, just like you have a limited number of box sizes when you’re packing, in programming, we have a set number of these special boxes, or variable types, to keep things organized. This helps the computer understand what kind of information is inside each box and how to work with it.
So, just like you wouldn’t try to squeeze a big piece of furniture into a tiny box, we use specific types of boxes in programming to make sure everything fits and works correctly!
Any variable consists of three parts:
The Data Type (Box Size), the Name (Box Label), and the Expression (Box Content).
Variable Types in Unreal Engine
Byte, Integer and Integer64
A byte is a basic unit for storing data, capable of holding small whole numbers ranging from 0 to 255.
An integer is a whole number, like 3 or -5, without any fractions or decimals. It’s used for counting and keeping track of things in math and computer programming.
Normally, you’ll use integers, but if you need to store a really large number, know that integer64 exists.
|Whole numbers variables||Value||Memory|
|Byte (unsigned char)||0 to 255||1 byte|
|Integer (int)||−2,147,483,648 to 2,147,483,647|
|Integer64 (long)||−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
Float is a special number in programming that can have decimal points, allowing for more precise values than whole numbers. In Unreal Engine 5, all float variables are of double precision type and use a memory of 8 bytes.
String, Name and Text Variables
The ‘Name‘ data type offers an efficient way to handle text. It ensures that if a specific word is used many times, it’s only saved once in a data table, even if it’s used repeatedly. This variable is case-insensitive, meaning it will ignore whether letters are uppercase or lowercase. Once it’s created, it can’t be changed or altered.
Name variables are mainly used for giving names to things like assets, controlling special effects on objects, labeling actors and components, specifying bone names in skeletons, or when you need to use as little computer memory as possible, such as network transmission or data storage.
Text data types are often used to display text on the screen (such as the Player HUD or Quest Log) or when language translation is required. As this variable supports language localization.
A String is a sequence of alpha-numeric characters, like words or sentences, used in computer programming to handle text-based information. Strings can be searched, compared and modified.
|Text||“Listen to my story. This…may be our last chance.”||16 bytes|
|String||“Hello World”||24 bytes|
Read the official Unreal Engine documentation on String Handling to learn more.
A Boolean variable is a special type of variable in programming that can only have two possible values: true or false.
In programming, we use Boolean variables to make decisions. For example, if we’re making a game, we might use a Boolean variable to check if a character is jumping. If the variable is true, it means the character is jumping; if it’s false, the character is not jumping.
Boolean variables usually connect to Branch nodes in Unreal Engine for decision-making. Branch nodes act as gates, directing the flow based on true or false conditions. This is crucial for controlling game logic and optimizing code execution.
Transform, Vector, Rotator
A vector is a set of three numbers (X, Y, and Z) that work together to represent things in three-dimensional space, like positions or RGB colors in 3D worlds.
Imagine you have an asset that is located on the world’s map at X=-5, Y=2, and Z=10. This is -5 units to the left, 2 units to the front, and 10 units above ground.
Vectors are also handy for colors. For instance, (255, 0, 0) could represent a bright red color. The first number (255) stands for how much red is in it, the second (0) is for green, and the third (0) is for blue. In this case, since there’s a lot of red and no green or blue, you get a vibrant red color.
X is always presented in red, Y in green, and Z in blue.
A Rotator is a set of numbers that tells us how something is turned in 3D space.
Rotators are made up of three parts: Roll, Pitch, and Yaw.
Imagine Roll like tilting to one side, Pitch like nodding up and down, and Yaw like turning left or right. For example, if Roll is 180, it means something is tilted a lot. If Pitch is -90, it’s pointing really downward. And if Yaw is 45, it’s turned a bit to the right. These numbers help us understand how objects move in a 3D world.
The Transform variable holds three important aspects about an object: where it is in 3D space (called translation), how it’s turned (rotation), and how big or small it is (scale).
This data type contains two sets of vectors: one for the location and scale, and another for how it’s rotated. Use Transform variables when you want to store all these 3 elements about an object.
Object variables store Blueprint objects such as Lights, Actors, StaticMeshes, Cameras, and SoundCues. It’s generally ideal to reference an object if it’s going to be used often.
In Unreal Engine, a structure variable is a type of data container that allows you to group together different data types of information into a single package.
It’s similar to having a box with different compartments for different types of data. Each compartment can contain a different type of data, such as numbers, words, or even other structures.
For example, if you’re creating a game character, you could use a structure variable called “CharacterInfo” to store information such as the character’s name, health, and level. You can group these pieces of information together in a structure rather than having separate variables for each of them.
An interface variable is a bit different from other variables. It’s not used to store data like numbers or text. Instead, it’s like a special set of rules or promises that a class or object makes. These rules are there to make sure that different classes, which might not seem related, can all do the same set of actions.
For example, let’s say you have characters and weapons in a game. They might need to do similar things like “Attack” or “Heal.” An interface variable lets you say, “Any object that uses this interface must be able to do these actions.” So, if both characters and weapons use the same interface, you can be sure they both have the “Attack” and “Heal” abilities, even if they work in different ways.
An enumerator variable, often referred to as an Enum in programming, is a data type that assigns names to integer values.
Enums are especially useful when there are a limited number of possible values for a certain property. They help improve code readability and maintainability because they make it clear what each value represents.
How to create a variable in Unreal Engine?
Inside any Blueprint, such as the Level Blueprint, navigate to My Blueprint Panel and click the ‘+’ symbol next to variables.
Alternatively, right-click in the Variables panel and select ‘Variable‘.
This will create a new variable that you can name and set to the data type that you require.
After that, press the ‘Compile‘ button and go to the Details Panel to change the default value of the variable you just created.
Promote to Variable in Unreal Engine
If you see some Data Type as input or output pin that you want to use again, you can right-click on it.
Then, from the menu that appears, choose ‘Promote to Variable‘.
This action turns that piece of information into a variable that you can use throughout your project.
Get and Set Variables in Blueprints
There are two main ways to work with variables in Blueprints:
First, you can use the “Get” node. This is like asking for the current value of the variable. It’s like checking a thermometer to see how hot it is.
Second, there’s the “Set” node. This is like adjusting a thermostat to change the temperature. You use the “Set” node when you want to give the variable a new value.
So, in simple terms, you can either check what the variable’s value is (using the “Get” node), or you can change it to a new value (using the “Set” node). It’s like either looking at the current temperature or deciding to make it hotter or colder!
You can create a Set node or Get node for a variable by right-clicking in the graph and typing either Set (variable name) or Get (variable name).
Another method is to drag and drop the variable from My Blueprint variable list to the Event Graph, and then select Get or Set.