How to use Structs in Unreal Engine 4

Structures are useful for most aspects of Game Development as they are incredibly versatile. In UE4 this is no different.

When we are finished, our FPS example template character will print the ammo after shooting an will remove one ammo after every shot.

What is a Structure (Struct)

In Unreal Engine 4, the struct is an easy way to create your own variable type, giving you the ability to substantially improve the organisation and access of the data in your blueprints.

One example of using a struct in your UE4 game would be to have a single struct that contains your player’s position, health, ammo and lives.
This then can be saved and loaded as one variable therefore streamlining the process as your game gets more complicated.

In this guide we will be learning how to create a structure and how to use structures in Unreal Engine 4.

Creating the Struct

To create your struct, firstly right click in the content browser and in the bottom section click on the Blueprints tab.

Advanced asset menu

Once in the Blueprints tab, find and click on the option named Struct.

Creating the struct

Rename this new file something suitable and save. In this example, I named mine PlayerInfo.

New struct

Setting up the Struct

Before we can start to use our new struct we need to fill it with variable types and give them the names that we want that data to be known as.

Struct variable menu

In my case, I have added a Vector named ‘Player Location’, a Float named ‘Player Health’, an Integer named ‘Player Ammo’ and another Integer named ‘Story Progression’.

New variables inside the struct

This struct can now be added as a new variable in our main character blueprint.

Creating the Struct on our Player Character

To access the struct, simply create a new variable in our main character blueprint and set the type to the name of your struct created earlier.

Adding the struct to the player

Compile and save your blueprint to show the values of the struct.

I have set the initial values to 100 for the player’s health and 25 for the player’s ammo.

Adding values to the player struct

Accessing the Struct on our Player Character

Getting the Values

To get the values inside our struct, simply type get and then the struct variable name. In my case I typed ‘get PlayerValues’.

Get the struct

From this new get node, right click the pin and click the split struct pin button. This will now expose the values inside the struct.

Splitting the struct pin

From this get node, I access the PlayerAmmo value and can then print it to the screen using the Print String node.

This will now display my ammo on screen when the Left Mouse Button is pressed.

Printing the ammo value

Setting the Values

To set the values inside our struct, simply type set and then the name of your struct variable. In this case I typed ‘Set PlayerValues’.

Setting the player values struct

From there, right click the left pin of your Set PlayerValues node and click split struct pin. This will then expose the values of your struct variable which then can be set however you want.

Splitting the struct pin on the set node

For my character, I access the struct variable and set the Player Ammo value when I have fired my weapon using the Left Mouse Button.

Setting the ammo after every shot.
This blueprint for firing is included in the default first person template character.

When setting your values make sure to never leave any empty as they will be overwritten!

In the example above I set the location, health and story progression values to the values it already stored.

Example

Every time the player presses the left mouse button the bullet is shot and one bullet is taken away from the ammo. The remaining ammo is then printed on the screen.

Shooting with left mouse button reduces the ammo and prints the ammo on the screen.

Conclusion

Now you have a fully working struct in your project that can now be used to store your player’s current status and progress.

This setup makes your general player variables much more organised and prevents data from being hard to track down.

The struct is a key part of shrinking large areas of blueprints into compact and efficient systems.

With this setup and working you can now create your own new structs for other gameplay tasks and uses.

Further Examples

Structs are great for creating a quest system. Each quest uses the struct for its location, enemies to spawn/ be defeated, gold reward and more.

This then can easily be added to a UMG widget to display the info on screen. Furthermore this can then be saved easily to preserve the player’s progress through the quests in your game.

One essential use for structs is to create an utilize Data Tables.

To read the Unreal Engine 4 documentation on Structs click here.

8 Comments

    • Yes all blueprint classes can access the struct. This is done by accessing it like any other variable.

      Once you have a reference to the object with the struct variable use a Get STRUCTNAME node and you can access the data.

  1. Excellent guide on using structs in Unreal Engine 4! Structs indeed bring organization and efficiency to game development. Your step-by-step explanation and examples make it easy to implement. Thanks for sharing!

Leave a Reply

Your email address will not be published.


*