The USTRUCT() macro

A structure is a type of composite data that allows you to group variables of different types into a single type. The USTRUCT() macro allows the creation of structures that can be used in the Unreal Editor and in Blueprints.

In Unreal Engine, the structures have the prefix F in C++. To implement a structure in C++, create a header file (.h) with the name of the structure, for example, StructExample.h. The code below shows an example of USTRUCT() that can be used in Blueprints.

#pragma once

#include "CoreMinimal.h"
#include "StructExample.generated.h"

USTRUCT(BlueprintType)
struct FStructExample
{
  GENERATED_BODY()

public:
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  int32 IntegerVariable;
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  float FloatVariable;
  
  //Other variables...
};

The BlueprintType specifier allows the structure to be used as the type of a variable in a Blueprint. In this image, a variable with the name MyStruct is being created using the StructExample structure as type:

 

 

The image below shows how the variables of a structure are displayed in the Details tab of an instance.

Unreal Engine creates the structure’s Make and Break functions to be used in the EventGraph of a Blueprint:

Example usage:

Let’s create the FItemShop structure that contains information about an item that can be sold to the player. Create the ItemShop.h header file and add this code:

#pragma once

#include "CoreMinimal.h"
#include "Engine/Texture.h"
#include "ItemShop.generated.h"

USTRUCT(BlueprintType)
struct FItemShop
{
  GENERATED_BODY()

public:
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  FString Name;

  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  bool bShow;
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  float Value;
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  int32 Amount;  
  
  UPROPERTY(EditAnywhere, BlueprintReadWrite)
  UTexture* Image;
  
}; 

We will use the FItemShop structure in a C++ class. In the Unreal editor, create a C++ class with the name Seller using the Actor class as the parent class. The Seller.h file must have this content:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ItemShop.h"
#include "Seller.generated.h"

UCLASS()
class TUTOPROJECT_API ASeller : public AActor
{
  GENERATED_BODY()
	
public:	
  // Sets default values for this actor's properties
  ASeller();

protected:
  // Called when the game starts or when spawned
  virtual void BeginPlay() override;

public:	
  // Called every frame
  virtual void Tick(float DeltaTime) override;
	
  UPROPERTY(EditAnywhere, Category=Seller)
  TArray<FItemShop> Inventory;
	
  UPROPERTY(VisibleAnywhere)
  USceneComponent* RootScene;

  UPROPERTY(VisibleAnywhere)
  UStaticMeshComponent* StaticMesh;

};

We created a TArray to store FItemShop elements. TArray is a container that stores a sequence of elements. We’ll look at TArray concepts in more detail in another article.

In the Seller.cpp file, you need to create the components in the constructor. The StaticMesh used by the Seller will be defined in the Unreal editor.

#include "Seller.h"

ASeller::ASeller()
{
  // Set this actor to call Tick() every frame.
  PrimaryActorTick.bCanEverTick = true;

  RootScene = CreateDefaultSubobject<USceneComponent>("RootScene");
  RootComponent = RootScene;

  StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("StaticMesh");
  StaticMesh->SetupAttachment(RootScene);
}

...

Compile the C++ code and add an instance of Seller at the level. You can select a StaticMesh to represent the Seller in the Details tab of the instance:

 

 

 

Our TArray named Inventory is also available in the Details tab of the instance. The elements of this TArray are of the FItemShop type. You can add elements and define the content of each element.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed