Cast<T>(Actor) Vs Implements Interface

Hi,
This is more of a discussion, I am working on a Practice project and I am kind of Trying to squeeze every bit of performance from it.

I always preferred using Cast To over the does Implement Interface, As most of the tutorials used it, some people warned about using Cast In Methods like Tick and Timers, From there I Started using Interfaces and the way I checked if the Actor is of certain Type and Contains the Needed Information was a simple Check (Does Implement Interface).

Now I don’t have access to Source code but is Cast really a bad choice for performance? In my earlier days it just felt like using If else, which is not that performance costly but all over the internet people will recommend moving Cast to a Macro and Call it once instead of every time.

The reason behind asking this question:
I am working on an Inventory and I have Class structure like this : BaseItem->UseableItem
Inventory atm is just an array of BaseItems.
when I click on an Item, I would need to check if the item is just the baseitem or the useable one since the UseableItem has a Function OnUseItem(); Of course the sensible way is to use Cast on the clicked Slot to check if it is of UseableItem or not.
this will result in Casting everytime when the user clicks on an item.

Edit: I realized writing this that I maybe overthinking stuff and kind of feels stupid to try to get performance out of something as small as an inventory component with just 75Kg limit.

1 Like

i don’t really see this as an issue. Like you said you are only casting upon an event happening, not every frame. This is something i do all the time without any performance spikes etc.

There is another thread about this is you fancy additional insights…

https://forums.unrealengine.com/development-discussion/blueprint-visual-scripting/20490-is-casting-expensive

Best,
Matt.

Maybe I’m misunderstanding… but every time you call the macro that contains the cast, the cast will be executed too… Or maybe you intend not to call the macro every time?

My bad, The way I was told to do was, Cast Once and Store the result in a variable then, use that variable instead of casting again.

Yes, that’s what I usually do.