Can you access an ustruct from another class

So I am trying to make an struct that I want to use in different classes, but I don’t want to have the decleration of the struct in every class. How can I achieve an ustruct that is accessable from multiple classes and possibly even blueprints?
This is what I tried in an empty class in which I removed everything except for the headers and #pragma once:

/**
 * HELLSKRIEGCPP_API 
 BlueprintType
 */
USTRUCT()
struct FCoverStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool InUse;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		FVector Location;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		FRotator Rotation;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		AActor* OwningActor;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool InLowCover;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool IsNextToEdge;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
};

I have tried the blueprintype, GENERATED_USTRUCT_BODY(), GENERATED_BODY(), the GAME_API, the struct name with and without F. But whatever I try I can’t access it from another header file to declare a variable.

Any help would be greatly apreciated.

All you have to do is include the header that holds the struct definition in whatever source you want to have access to the struct; just like if you want a class to have access to an external class you have to include the header file defining the external class.

You can create a header file that only holds your struct (or multiple structs, and/or enums) if you don’t want to include the class as well, and then you don’t need to create a .cpp file associated with it.

If you want it accessible in blueprints you need to use the BlueprintType specifier in the USTRUCT macro:

#pragma once

#include "MyStruct.generated.h"

USTRUCT(BlueprintType)
struct FMyCustomStruct
 {
     GENERATED_USTRUCT_BODY()
 
     //...
     //define the struct
     //...

};

With the above struct, in whatever class you want to have access to it include:

#include "MyStruct.h"

If you use it in the header (like if you define a class variable using the struct, or have a function that returns your struct or accepts the struct as an argument), include it in the header. If you just use the struct within a method but it’s never exposed outside, you can just place it in the source file.

Same with enums, or any other datatype you may define.

1 Like

It now is complaining about that there is no semi colon before the first variable. I don’t see what I am doing wrong.

USTRUCT(BlueprintType)
struct FCoverStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool InUse;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		FVector Location;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		FRotator Rotation;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		AActor* OwningActor;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool IsLowCover;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		bool IsNextToEdge;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Cover")
		float Distance;
};

What is complaining?

Bear in mind that a lot of the default warnings and errors that come from VS do not apply. If you have a successful compile, you are good.

Intellisense is particularly bad with FStructs; the first property in an FStruct often doesn’t appear in intellisense at all. If you’re just getting the squiggly lines and intellisense warnings, you can ignore them. The only thing that matters is if it compiles.

Use the output tab, do not use the Error tab in VS to evaluate compile results. UE4 uses so many macros VS often gives misleading error messages.

found the last issue, I forgot to include the #include “CoverStruct.generated.h” in
the CoverStruct.h