UGameplayStatics::GetAllActorsOfClass

I’m trying to understand what we did with UGameplayStatics::GetAllActorsOfClass.

For example here’s what the documentation says:

static void GetAllActorsOfClass
(
    const UObject * WorldContextObject,
    TSubclassOf< AActor > ActorClass,
    TArray< AActor * > & OutActors
) 

The part in question is the second input TSubclassOf< AActor > ActorClass. Stephen mentions that we learned that when a function requires a TSubclassOf, we can pass it a UClass instead. I must have missed learning that, so my question is why can we pass a UClass instead of a TSubclassOf? What is the difference between the two?

I guess I assumed that under the hood the TSubclassOf ultimately ends up being a UClass, and if that’s the case, why wouldn’t the GetAllActorsOfClass documentation just say it’s needs a UClass instead of a TSubclassOf?

Thanks!

Hey!

Just going through the course at the same time as you and saw your question. It helped cement my understanding of this, so thanks for asking it!

The main reason that we are happy that it uses TSubClassOf<> is because the towers in our game aren’t actually of Class Type ATower, but a blueprint that was created from ATower. TSubClassOf<> can take a UClass* and then does a runtime check for any objects that are of class ATower or any that are derived from that class. Since BP_PawnTower has a Parent Class of ATower, it gets included.

To go beyond that, something that seems interesting in the future. If we have a TSubClassOf<> variable that can be edited in the editor, the editor will only allow you to assign a value that meets that requirement.

Hope this helps! And if I skewed anything, I hope the instructors can correct it! :stuck_out_tongue:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms