Maybe I’m just not understanding this right, but what is the point of using TSubclassOf ? It seems kind of general… What if I wanted to spawn a specific child class of pickup. I.e. if I have a parent class called pickup and I have derived classes that give health, one that gives mana, etc. How would I go about delineating which specific subclass I am referencing? For example, if I made a health potion spawning class had a property in the header that looked like:
and then in the .cpp it might have some function like SpawnHealthPotion():
UWorld* const World = ();
APickup* spawnHealthPotion = World->SpawnActor<APickup>(HealthPotion, FVector(0,0,0), FRotator::ZeroRotator);
It just seems sort of pointless because it seems like I have nowhere specified that I want a health potion to be spawned as opposed to, say, the mana potion, both of which are subclasses of APickup. Maybe I’m just not understanding this right, or there’s a better way to go about doing this when there are multiple child classes of a parent.
Why not try setting the TSubClassof directly to child classes instead of Pickup class?
I think this is where I got confused then. If that’s the case, then why won’t it accept if I simply declared “class AChildClass* ChildClass” and pass that straight into SpawnActor(), rather than bothering with the TSubclassOf<> declaration at all?
TSubclassOf in reality is UClass*, only diffrence is that TSubclassOf limit selection of classes in editor to specific class relation (and i think it also limits that in C++ too but i never deared to try it, i personally only use it limit selection in editor), other then that it is still fully functional UClass*. UClass object is class identifier in UE4 reflection system, each C++ and Blueprint class has one, by it you can check if class you talking to is a class you expect to be or you can reffer to specific class (which you do in SpawnActor), which normally impossible in C++, thats what reflection system is for and not only that.
Here is what you can do with UClass:
Because AChildClass is a actor object of class, UClass (which TSubclassOf effectivly is) is class identifier object, it contains information about class but it not class of your object it self or object of your class, only representive. You can get UClass from any UObject using GetClass() function and staticly from class decleration like this APickup::StaticClass(), See my anwser below