Get actor of class vs cast to class?

get actor of class vs cast to class?
which one is more expensive for performance?
do you have any other solutions?
what is advantages and disadvantages of them?

image

Hi @ganbaa_elmer !

Get Actor of Class will give you the first found actor of the defined class.

for the cast you give the actor you want evaluate to the function so cast is more efficient because you have the actor already.

get actor of class has to:

get all actors, filter the class, take the first of the array , cast and send.
also if you don’t have any actor of that class, the function will return a null/invalid
output.

2 Likes

@eldany.uy so which one is better?

so you mean “cast to” is better?

different purposes.

for example you have a unique camera actor in the scene and you need to get it, then get actor of class camera actor would work. if you have an overlap output actor and want to access its properties then you use cast.

I never use Get Actor of Class…I’m more a tag guy.

In the camera example I’d get actors with a tag and then cast . I think tagging I can selectively filter the actors and feel more in control.

4 Likes

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

If you are trying to get your player controller you will want to use a cast. “Cast to player controller” if u made a custom one make sure you choose the one u want. “Cast to MyPlayerController” and then use for the object input “get player controller” and that would return the players currently used controller. Player Controller is also always active anyways so no problem casting to it. You only want to avoid casting to blueprints that you don’t want to be constantly referenced. The engine has a warning about this on the cast.

Only use “get actor of class” or the plural if you don’t need it to be specific or say wanted to make an array of all and find the closest one and/or no other way to cast to it. As you require an object input on a cast which is a reference saying “this one specifically”.

Hope that helps

1 Like

It is possible to use Blueprint Interface to run the interface function of the output actor from Overlap without casting?. I try but not working. I use tags too. I try to avoid casting if possible

of course is possible…should be like this:

you need the interface:

image

inside the interface define some empty function:

image

in your blueprint ask if the overlaped actor implements your interface:

Obviously you must add the interface in the overlaped actor blueprint and implement its interface function with something like this:

image

image

Hope it helps.
Good luck!

Dany

2 Likes

Yeah I do exacly same and not working previously so I change use GetActorOfClass and works. Then I try again directly from Othe Actor output and magically working with exacly same setting lol. thank you

1 Like