Making an array of Structs a UProperty

Hi,

I am having a bit of an issue making an array a UProperty; I am getting a compiletime error with no info.

Basically, I have a simple struct like so:

USTRUCT()
struct FItemData
{
	GENERATED_USTRUCT_BODY()

	// Item class to spawn
	UPROPERTY(EditDefaultsOnly, Category = Item)
		TSubclassOf<AActor> ItemClass;

	// Item name
	UPROPERTY(EditDefaultsOnly, Category = Item)
		FText ItemName;

	// Item image to be displayed within the inventory
	UPROPERTY(EditDefaultsOnly, Category = Item)
		UTexture2D*  ItemImage;

	// Flag to capture if the item has been selected
	UPROPERTY(EditDefaultsOnly, Category = Item)
		bool bIsSelected;

	// The slot the item is stored in
	UPROPERTY(EditDefaultsOnly, Category = Item)
		int32 InSlot;

	// Default constructor
	FItemData() { }
};

and then I am simply creating a public array using this type in my player class like so:

	UPROPERTY(Transient, Replicated)
		TArray<struct FItemData*> Items;

But it errors out if I have the UPROPERTY line there, even without any variables i.e. UPROPERTY()

I am lost why this might be as I can make other arrays (using a different type) a uproperty, so I am assuming it is something to do with the struct?

Any help would be greatly appreciated

USTRUCT types may not be pointers when used with a UPROPERTY.

thanks, any idea why that is?

We can’t factory a struct like we can with an object, so we’d never be able to de-serialise/replicate that property correctly since we wouldn’t be able to allocate the destination struct.

hmm I get what you are saying, but changing it from a pointer still doesn’t let me set it as a uproperty, unless I am doing something else daft?

UPROPERTY(Transient, Replicated)
		TArray<struct FItemData> InventoryItems;

I personally never say struct with USTRUCTS, either as variables or in an array.

Try TArray< FItemData >

Thanks, that actually got it to compile after I added the .h reference to the file I declared the struct (and changed it from using pointers). I wonder why the forward declaration didnt work with the uproperty; any ideas?

You can’t forward declare a type used by value, only by pointer or by reference.

i would love to know more about this. by “we cant factory a struct” do you mean that structs cannot have a constructor? and if a struct can be copied by value, why would a hypothetical pointer to a struct not be able to copy the value for replication?

i wish more info on structs was in the documentation, it doesn’t even mention its a value type that cant be pointed to:

Structs can having a constructor, and thinking about it more, we do in fact factory them in a few places (for example, data table rows), but factorying them is awkward since you need to get your hands on a UScriptStruct and then allocate a block of memory to hold the struct data.

The idea really is that structs are lightwight (compared to objects), and so you can’t have pointers to them since they lack the scaffolding that objects have to support that sort of thing.