How do I put structs in their own header?

I have a few simple structs and enums I am using in my project. I would like to separate these out into their own header files for readability. My issue is that, creating a new header in the source folder does not get built and seems to be ignored by Unreal. I could use the “create new C++ class” menu option, but I don’t know what additional code that generates and don’t need the cpp file. I should note that these are not USTRUCTS, they are just regular structs I am using to contain a few data types.

You only have to create a header file. Put USTRUCT() and UENUM() macros above your types when you want to use them as UPROPERTY()'s. Also very important, you have to add #include "YourFileName.generated.h" at the top. One more thing, make sure that the new file actually is in your source directory of your project. When you create a file in VisualStudio, the default path is somewhere in Intermediate folder.

Here is an example of a header file:

//ProcGenBlueprintTypes.h

#pragma once

#include "ProcGenBlueprintTypes.generated.h"

UENUM(BlueprintType)
enum class EStudentTypes : uint8
{
	General,
	Standard,
	Fat,
	PhoneCheater,
	PaperPlane,
	Punk,
};


USTRUCT()
struct FStudentProp
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Student")
	UStaticMesh* Mesh;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Student")
	UMaterial* MaterialOverride;

	FStudentProp::FStudentProp()
	{
		Mesh = NULL;
		MaterialOverride = NULL;
	}

	void SetMesh(UStaticMesh* NewMesh)
	{
		Mesh = NewMesh;
	}
};
2 Likes

Ah thanks, the intermediate folder was my problem. Can you explain a little bit about including the generated header? I am not sure of it’s purpose and currently attempting to do so just results in source file could not be opened.

Well, UnrealHeaderTool does things and stuff. Compilation kinda happens in two steps. Step 1: UnrealHeaderTool scans your code for unreal related data and puts extra code inside these generated header files. Step 2: standard c++ compiling of the generated code.