Which is faster: Casting or Finding Tag

Which is faster:

  • Using ‘Actor has Tag’ and looking for the player tag
  • Using Cast to Player Controller

My AI looks for items in the scene and I want to know if it is faster (performance wise) to check each actor he sees by tag first of to just skip to trying to cast for each item.

Thanks

Id have to go with casting though I havent timed it, function calls do cost abit so thats something to take into consideration that nothing really comes for free.

The most expensive thing though is the loop so if you can cut down on iterations that would be beneficial. There is an iterator for finding all Actors of a specific Tag and this might be faster than looping (in BP) and casting your array (assuming you have quite a long list of Actors in your array because I cant see it in the image).

Internally that iterator could be doing similar to what you have here though so you might have to bechmark it yourself, you can save the time in seconds before the loop starts and then check it again once the looping is all over to see how fast its being performed.

It would be better to use a GetAllActorsOfClass node to get filtered actors. As result you wouldn’t need to cast anything.

126852-getallactorsbyclass.png

Please dont. Go with interfaces mate, don’t listen to this GetAllActorsOfCrazySuggestion.
You drop an interface on your pawn and you can discover if that specific actor implement said interface, you can call for functions into your Pawn without ever casting to your pawn. Try avoid casting or GetAllActorsOfClass in these situations.

2 Likes