How can I create a separate header for my Structs?

Pretty much like so:

#pragma once

#include "CoreMinimal.h"
#include "MyGlobalStructs.generated.h"

USTRUCT()
struct FMyFirstGlobalStruct
{
    GENERATED_BODY()

    UPROPERTY()
    AActor* SomeActorReference;

    UPROPERTY(EditDefaultsOnly)
    float DefaultActorHealth = 100.f;
};

Here’s what’s going on in the snippet above:

  1. The “…generated.h” include is only necessary if you have structs or enums in that header file that start with the USTRUCT or UENUM macros. If not, it’s not only not necessary, the compiler will complain about it being there.
  2. The GENERATED_BODY part only works inside structs that start with the USTRUCT macro. If you have a plain old struct you don’t need any of that. See the FInputModeDataBase struct in the engine source for example.
  3. Structs are not garbage collected, but they usually don’t have to be. Properties inside those structs might have to, though. Like the actor reference above. In order to tell the GC that there’s an actor pointer in there that needs to be garbage collected, you put the UPROPERTY macro in front of it, just like you would do in UObject-derived classes.
  4. You can choose to put UPROPERTY macros in fron of other properties in your structs if you want them to be editable in the editor. See the float property above.
  5. If you want to assign the struct to anything from within the editor or if you want to set properties on the struct in the editor, you need to put BlueprintType between the USTRUCT parentheses, like so: USTRUCT(BlueprintType).

That’s the basics I’d say.

5 Likes