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"

Can i create an Actor class using c++ templates?

Hello, i'm trying to define a class to hold a table of items for my inventory. I would like to be able to specify which type of items that class supports using a c++ template so i can do something like this:

 FInventoryTable<class AItem> Inventory; // Holds all types of items
 FInventoryTable<class AAmmoItem> AmmoZone; // Holds only ammo items

I can this in pure C++, the problem is that if i want to replicate this information i can't, because this class does not derive from a Unreal Engine class like AActor. Is there a way to pull this off or i have to get rid of the c++ template and check the type at runtime? Thank you.

Product Version: Not Selected
Tags:
more ▼

asked Oct 29 '14 at 05:39 PM in C++ Programming

avatar image

xlar8or
6k 163 129 305

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

1 answer: sort voted first

Hi,

Not sure what your FInventoryTable is trying to achieve here, but you can have arrays of elements like this:

 class AItem;
 class AAmmoItem;

 UCLASS()
 class UInventory : public UObject
 {
     GENERATED_UCLASS_BODY()

     UPROPERTY()
     TArray<AItem*> Items;

     UPROPERTY()
     TArray<AAmmoItem*> Ammo;
 };

To answer your more general point, a UClass cannot be an instantiation of a template, nor can you have a UClass template.

Steve

more ▼

answered Nov 05 '14 at 12:17 PM

avatar image

Steve Robb STAFF
2.8k 58 31 101

avatar image xlar8or Nov 05 '14 at 01:50 PM

The purpose for the FInventoryTable was for my TArray to be of the exact type of the subitem i'm storing, so i can limit what i can add and also remove the need to cast when i get an item.

(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