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"

Help Understanding TSubclassOf<> When Spawning

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:

 TSubclassOf<APickup> HealthPotion;

and then in the .cpp it might have some function like SpawnHealthPotion():

 UWorld* const World = GetWorld();
 if (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.

Product Version: UE 4.10
Tags:
more ▼

asked May 19 '16 at 03:09 PM in C++ Programming

avatar image

Scorch-D62
10 3 4 6

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

2 answers: sort voted first

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: https://docs.unrealengine.com/latest/INT/API/Runtime/CoreUObject/UObject/UClass/index.html

more ▼

answered May 19 '16 at 03:27 PM

avatar image

Shadowriver
35.9k 928 169 1105

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

Why not try setting the TSubClassof directly to child classes instead of Pickup class?

 TSubclassOf<AHealthPotion> HealthPotion;
 TSubclassOf<AManaPotion> ManaPotion;
more ▼

answered May 19 '16 at 03:18 PM

avatar image

ryanjon2040
1.6k 55 43 92

avatar image Scorch-D62 May 19 '16 at 03:37 PM

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?

avatar image Shadowriver May 19 '16 at 03:42 PM

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 :p

(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