I’ve created a struct called ‘FItem’ and it stores some very basic variables, I’ve given it the USTRUCT() tag but whenever I create a variable of type FItem or, in my case, create an array of them with the UPROPERTY tag I get the error Error:
Unrecognized type ‘FItem’ - type must be a UCLASS, USTRUCT or UENUM
If you declared that struct in other header than the one where you’re trying to use it as property, then you have to include the header containing the declaration of that struct.
For example if you declared your struct in MyHeaderWithStruct.h and you’re trying to make a property in MyActorComponent.h header, then you need to add this on top of your MyActorComponent.h file.
The headers were correct, I probably should have mentioned that before, although my stuct is not in it’s own header file, it’s below another class. The intellisense recognises ‘FItem’ and if I remove the UPROPERTY() there’s no error but I need it to be a UPROPERTY.
Are you getting this error in Visual Studio when compiling or when trying to use the struct in the editor? I was able to copy the above struct into a class that compiled and was available in the editor. Can you provide additional information about your setup?
I just added your code to my project (with a change of Id to int32) and it works. O.o
Since your code in comment above doesn’t show where you exactly tried to use UPROPERTY and since you have array of items and array of pointers to items, then you should know that UPROPERTY doesn’t work with pointers to struct.
If that’s now what you wanted to do, then I guess we’ll have to keep thinking.
I’m getting the error in the Unreal message log when it fails to compile. ‘Unrecognized type ‘FItem’ - type must be a UCLASS, USTRUCT or UENUM’. If I remove the UPROPERTY() tag it compiles. Would you recommend I make a new header file just for the struct?
UCLASS(Blueprintable, BlueprintType)
class GAMENAME_API UGameSingleton : public UObject
{
GENERATED_BODY()
public:
// TODO - Get rid of this if it's not used
UGameSingleton(const FObjectInitializer& ObjectInitializer);
// Inventory
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
class UPlayerInventory* PlayerInventory;
};
// TODO - Put this in it's own header file
USTRUCT(BlueprintType)
struct FItem
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
int32 Id;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
FString Name;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
FString Description;
};
On Line 24 is where I’m creating the Array of FItems, right now I’ve taken the UPROPERTY() prefix off so it doesn’t error. But as soon as I put it on it doesn’t compile.
Structs need to be placed outside of the class declaration for whatever class it is added to. If your struct is inside the declaration, try copying it to just below the header’s include statements. Putting the struct would be another solution however it shouldn’t be necessary.
Sorry I didn’t mentioned where the UPROPERTY went, it was line 24. Could you try this code, it still doesn’t compile with the same error coming from line 25, if it works fine for you then I’m just going start a new project.
Edit: there was a mistype but I fixed it and it still comes up with the error.
The solution to fix it, is just to switch place of your class declaration and struct declaration. Every class/struct you use, has to be declared first, before you can use it. (i’m not totally sure if that’s what meant, so sorry if i’m simply saying that same what you said :p)
I see you tried to use forward declaration there, but it looks like it doesn’t work with structs and UPROPERTies (i just tested it). Odd.