x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.12
Tags:
more ▼

asked Jun 23 '16 at 09:43 PM in C++ Programming

avatar image

johnparkes
22 12 10 16

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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

more ▼

answered Jun 23 '16 at 09:46 PM

avatar image

Jamie Dale STAFF
7.5k 176 66 304

avatar image johnparkes Jun 23 '16 at 09:52 PM

thanks, any idea why that is?

avatar image Jamie Dale STAFF Jun 23 '16 at 09:54 PM

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.

avatar image ScottSpadea Jun 24 '16 at 06:05 AM

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: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Structs/index.html

avatar image Jamie Dale STAFF Jun 24 '16 at 10:47 AM

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.

avatar image johnparkes Jun 23 '16 at 10:02 PM

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;

avatar image Kowbell Jun 23 '16 at 10:05 PM

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

Try TArray< FItemData >

avatar image johnparkes Jun 23 '16 at 10:13 PM

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?

avatar image Jamie Dale STAFF Jun 23 '16 at 10:15 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question