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