My UStructs aren't being recognised.

Hello,

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

USTRUCT(BlueprintType)
struct FItem
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
		int Id;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
		FString Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
		FString Description;
};

This is what it looks like in my actor component class:

	UPROPERTY()
	TArray<FItem> ItemDataBase;

Thanks in advanced. :slight_smile:

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.

#include "MyHeaderWithStruct.h"

Hello, thanks for your reply.

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.

Okay then.

Change type of your Id variable to int32 (or something else that is supported by UPROPERTies).

Hey TroyAtkinson-

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. :stuck_out_tongue:

Okay, nvm. Just re- your comment.

Hello , thanks for your reply.

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?

Hi again, I updated it to int32 and I’m getting the same error.

This is the first header file with 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;
};

And this is the class that uses the struct

#pragma once
#include "Components/ActorComponent.h"
#include "GameSingleton.h"
#include "PlayerInventory.generated.h"

UCLASS( ClassGroup=(Custom), Blueprintable, BlueprintType, meta=(BlueprintSpawnableComponent) )
class GAMENAME_API UPlayerInventory : public UActorComponent
{
	GENERATED_BODY()
public:
	// Functions & constructors
	UPlayerInventory();
	virtual void BeginPlay() override;
	void AddInstanceToGameSingleton();

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Inventory")
	void CreateItems();
	void CreateItems_Implementation();

	UFUNCTION(BlueprintCallable, Category = "Inventory")
	void CreateItem(FString ItemName, FString ItemDescription);

	// Variables & properties
	TArray<struct FItem> ItemDataBase;
	int32 NumberOfDatabaseItems;

	TArray<struct FItem*> Items;
};

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.

#pragma once
#include "Components/ActorComponent.h"
#include "GameSingleton.h"
#include "PlayerInventory.generated.h"

UCLASS( ClassGroup=(Custom), Blueprintable, BlueprintType, meta=(BlueprintSpawnableComponent) )
class GAMENAME_API UPlayerInventory : public UActorComponent
{
	GENERATED_BODY()
public:
	// Functions & constructors
	UPlayerInventory();
	virtual void BeginPlay() override;
	void AddInstanceToGameSingleton();

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Inventory")
	void CreateItems();
	void CreateItems_Implementation();

	UFUNCTION(BlueprintCallable, Category = "Inventory")
	void CreateItem(FString ItemName, FString ItemDescription);

	// Variables & properties
	UPROPERTY()
	TArray<struct FItem> ItemDataBase;
	int32 NumberOfDatabaseItems;

	//TArray<struct FItem*> Items;
};

// TODO - Put this in it's own header file
USTRUCT(BlueprintType)
struct FItem
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category= "Inventory")
		int32 ItemId;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
		FString Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
		FString Description;
};

It works, you’re my hero. I can’t thank-you enough :slight_smile:
Re-reading what said, yeah it’s pretty much the same but I didn’t take out of it what I needed.

Have a great day, thanks again!

Okay, now I have this issue.

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.

@Down:
Yw, happy coding! :stuck_out_tongue:

It’s working, big thanks :slight_smile:

Have a great day!