When I run the game I can go into my main character and add a list of objects manually in the editor, however, now I’m at the point where I want to pick up a object through code and I can’t work out how to add it.
I have an actor I can cast to ALoot so I thought it would simply be;
Inventory.Add(Cast<ALoot>(ClosestActor));
ClosestActor is the closest ALoot actor to the character when I hit the pick up button and is defined like so;
AActor* ClosestActor = nullptr;
This will not compile though and I get this error;
Severity Code Description Project File Line Suppression State Error C2664 'int TArray<TSubclassOf<ALoot>,FDefaultAllocator>::Add(const TSubclassOf<ALoot> &)': cannot convert argument 1 from 'To *' to 'TSubclassOf<ALoot> &&' InventoryTest E:\UnrealProjects\InventoryTest\Source\InventoryTest\InventoryTestCharacter.cpp 212
TSubclassOf is a class reference not an object reference like your AActor ClosestActor reference. TArray<ALoot*>> Inventory; is probably what you want instead of TArray<TSubclassOf<ALoot>> Inventory;
If I use a pointer then the class will still be in memory which will mount up if I have 200 loot items in storage (part of my game design involves a pack mule).
What I’m looking to do is delete them on pick up and just store a (reference?) to the class so I can spawn it latter if dropped or used.
You are trying to assign a pointer to a class reference so you must change the pointer. You have to get the class of theClosestActor. Inventory.Add(ClosestActor->GetClass()); will fix it.
If you plan to spawn and destroy a lot of things then the garbage collector will have a lot of work to do. I would rather keep the memory footprint low by not duplicating the loot items but just have one object with a “uses” counter.
If you really want to free up some memory then you should look at TSoftObjectPtr so the class can be loaded and unloaded whenever you like.