Get actor of class vs cast to class?

As @eldany.uy said, they serve different purposes and have different uses.

GetActorOfClass() searches for every actor that can be casted as a AWhateverActor on the world and returns the first found actor as an AActor* (Pointer to that actor).


This is what the Engine says about it.

The Cast takes an AActor* (or whatever class AWhateverActor inherits from) and returns an AWhateverActor* which can be used for whatever methods or attributes AWhateverActor has.

image

As you can see, to use a AWhateverActor* that is currently on the world, and you don’t have it tagged or the reference to it, you can use GetActorOfClass which returns an AActor*, that can then be cast into a AWhateverActor using the Cast

Another thing, the GetActorOfClass only works with actors, meanwhile, the Cast works with any UObject, which AActor inherits from, that means that you can cast objects that are not spawned on the world, actors, components, anything really (except for interfaces) that are inherited from another class on your code/blueprints

5 Likes