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"

How would one create an array which holds any item that derives from a certain class?.

I have a TArray inventory which holds an "Item" struct. Inside the struct, upon construction, I'd like to create an instance of the actor picked up in game. This item could be AWood, AStone, ASpear, but will have a common child of AItem*.

Without casting the in game object to an AItem, how can I "dynamically(?)" add any item that inherits from AItem into the struct?

My Speculation is that some template, class or solution exists as this seems an immediate issue one would run in to.

Product Version: UE 4.18
more ▼

asked Mar 07 '18 at 02:50 AM in C++ Programming

avatar image

Perfect P
1 1

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

1 answer: sort voted first

When you say a common child, I believe you meant a common parent. AWood, AStone, ASpear all inherit from AItem right?

Not sure if I got what you're trying to do, but in that case you just need to store a pointer to a AItem as such:


     FItem() { ItemInstance = nullptr;}
     FItem(AItem* itemInstance) { ItemInstance = itemInstance;}
     /**AItem stored inside struct*/
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item")
     AItem* ItemInstance;


That way when you a construct it on blueprint or c++ you can always specify the AItem on construction, obviously it will only accept AItem references, not global Actors since this would probably result in sometimes the instance being set to nullptr again. but you could, at least on c++ add a constructor with a cast option:

 FItem(AActor* itemInstance) { 
     AItem* i = Cast<AItem>(itemInstance);
     if(i){ ItemInstance = i; }
     else{ ItemInstance = nullptr;}

Or did you mean you want to spawn the ItemType(The AItem instance itself) on struct construction?

more ▼

answered Mar 07 '18 at 04:25 AM

avatar image

46 1 3 7

(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