How to implement a C++ Struct in UE4/UE5?

Greetings!

I’m trying to familiarize myself more with UE’s C++ side of things. What code body should I generate for creating classes or structures? For example, let’s say I want to create an item class/struct for an inventory system. Do I create a new C++ file from within UE? If so, what type? Or do I just create an empty file inside of Visual Studio? What is the recommended method? Should I use the function library for this or struct factory? Does it matter?

I read the documentation here, but it says nothing about how to generate the file.

Thanks!

I usually create an empty file from the unreal editor called “GenericStructs” and put all the structs inside it (I do the same thing with enums).

There is no official rule on where to store them, my personal rule is “if it’s needed only in one class I Place it on the top of the class file, if it’s used in more than one class it goes inside GenericStructs file” (so you have to #include it in order to use it).

5 Likes

with empty file you mean this?

nice tip! thanks

1 Like

Yes, I mean an empty class.
Another important thing to remember is to mark both enums and structs as “BlueprintType”, otherwise they won’t be visible in the editor (Only in C++, but this could be a desired behaviour in certain cases).


Example for structs:

USTRUCT(BlueprintType)
struct FDirectionConnections
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	int32 AdjacentRoomIndex;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class ADungeonRoom *AdjacentRoom;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	EBorderTypes BorderType;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	EDoorTypes DoorType;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	bool bShowRoadOnMap;

	FDirectionConnections()
	{
		AdjacentRoomIndex = -1;
		AdjacentRoom = nullptr;
		BorderType = EBorderTypes::EBT_Wall;
		DoorType = EDoorTypes::EDT_Wall;
		bShowRoadOnMap = false;
	}

};

You can declare functions inside structs, in this case you can see a constructor, but you can also init values directly with “=”, you can also have more than one constructor (for example FVector is a struct that you can init with a single float or with three floats obtaining different results)


Example for enum:

UENUM(BlueprintType)
enum class EDirections : uint8
{
	ED_N UMETA(DisplayName="North"),
	ED_E UMETA(DisplayName="East"),
	ED_S UMETA(DisplayName="South"),
	ED_W UMETA(DisplayName="West"),
	ED_MAX UMETA(Hidden)
};

If you want to implement a function you can do it inside the class:

class YOURPROJECTNAME_API GenericEnums
{
public:
	GenericEnums();
	~GenericEnums();

	static EDirections GetOppositeDirection(EDirections Direction);
};

Implementation (.cpp file):

EDirections GenericEnums::GetOppositeDirection(EDirections Direction)
{
    switch (Direction)
    {
        case EDirections::ED_N:
            return EDirections::ED_S;
        case EDirections::ED_E:
            return EDirections::ED_W;
        case EDirections::ED_S:
            return EDirections::ED_N;
        case EDirections::ED_W:
            return EDirections::ED_E;
        default:
            return EDirections::ED_MAX;
    }
}
3 Likes

Wow! If it were a snake, it would have bitten me :rofl: Thanks!

1 Like

Very thorough! Thank you!

1 Like

If my answer helped you please mark it as solution, in this way you’ll help other user interested in this topic (seeing it marked as “resolved” is an incentive to click)

Have a nice day!

1 Like