Creating Structs with C++ in Unreal Engine 5
Structs can helpful to keep data structured throughout your project. This is a very simple example of creating a struct and using it with a BP player.

Game Engine
Unreal Engine 5.5.0
IDE
Rider 2024.3
Project Name
MyProject
Structs are great when you want to combine data into one consistent group that multiple classes can utilize. In this example I create a simple FStruct
with a name, health, and image that our test players can use.
Creating a new struct wasn't completely clear to me first at first, but to create a new struct, first create a new empty UObject
class via the editor or IDE.

With the new empty class I removed everything from both the .h
and .cpp
files and updated the header file to use USTRUCT()
rather than UCLASS()
and filled out the macro. I called the struct MyStruct
and added a few properties to fill it out. For this example I added a FString
for name, a float
for health, and UTexture2D
that could be used for profile picture.
To keep this simple, I added the constructor in the header file to create some default values. Below is the .h
file, the .cpp
file is empty.
MyStruct.h
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "MyStruct.generated.h"
USTRUCT(BlueprintType, Blueprintable)
struct FMyStruct
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString Name;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Health;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UTexture2D* Texture;
// Constructor
FMyStruct() : Name(TEXT("Default")), Health(100) {}
};
To test out the new struct I created an actor called MyStructPlayer
. The actor includes the new struct at the top of the file and uses it to create a new struct using FMyStruct MyStruct
. I also added a skeletal mesh to fill out the actor. There's few more things happening in this actor that will be used in a later post regarding UI lists, but now let's just focus on the struct. In the MyStructPlayer
constructor is where I set the default values for the struct on the player.
MyStructPlayer.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyStruct.h"
#include "MyStructPlayer.generated.h"
class USkeletalMeshComponent;
class UWidgetComponent;
UCLASS()
class MYPROJECT_API AMyStructPlayer : public AActor
{
GENERATED_BODY()
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Sets default values for this actor's properties
AMyStructPlayer();
UPROPERTY(EditAnywhere)
USceneComponent* Root;
UPROPERTY(EditAnywhere)
USkeletalMeshComponent* SkeletalMesh;
UPROPERTY(EditAnywhere)
UWidgetComponent* Widget;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FMyStruct MyStruct;
};
MyStructPlayer.cpp
#include "MyStructPlayer.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/WidgetComponent.h"
#include "MyProject/Widgets/MyWidget_Health.h"
// Sets default values
AMyStructPlayer::AMyStructPlayer()
{
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
Root->bVisualizeComponent = true;
RootComponent = Root;
SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMesh"));
SkeletalMesh->SetupAttachment(RootComponent);
Widget = CreateDefaultSubobject<UWidgetComponent>(TEXT("Widget"));
Widget->SetupAttachment(RootComponent);
MyStruct.Name = TEXT("Name");
MyStruct.Health = 100.f;
}
Although this is a basic example, using structs can be useful to ensure data is consistent across multiple classes. This struct is being used in the project's gym where I tie to values to a widget.