Variables are used to store data.
We want to store data all the time when programming. In games, we often find ourselves storing a player’s score, a character’s name, an actor’s location, a game state, etc.
The thing about data is that it comes in many forms. Data can be numbers, words, vectors, and even references to other actors. In order to handle all these types of data, variables must have a specific type. The type of variable tells you what type of data it stores.
These are the most commonly used types of variables in UE5. Depending on what data you are trying to store, you’ll want to choose the right type of variable.
The types of variables of the examples given above:
Variables are created inside of Blueprints, and thus are associated with that Blueprint. For example, a player’s Score would be stored in the player Blueprint.
To create the Score variable:
Now we can use the new variable in our code!
When dealing while data, we’re either trying to get the data, or set the data.
When Getting data, we want to use it for some reason. Let’s say we want to print our Score when the game starts.
We first have to set up the nodes:
Then we have to Get the Score data.
Now we know how to Get the variable’s data in order to use it. Let’s work through Setting the variable to update it’s data.
Let’s say we want to add 1 to our score every time the ActorBeginOverlap event fires.
In order to do this, we have to:
You now know:
You will use Variables all the time when programming. The best way to get comfortable with them is to keep creating and using them when needed.