Casting to Interface vs Actor

Does casting to interface better than casting to actor in performance as some of YouTube content creators say ?
Because what I know about interfaces that they r used when a class has a function that multiple object have their own implementation for this function for example.

If you want to cast to an actor, you have to #include its class into the one doing the casting. If you’re working with various different classes, you can include either all of those classes, or just a few light interfaces that only have several function declarations.

And the casting itself, I guess it has to allocate some memory to the pointer, and the larger the class, the more it needs?? I’m not sure about this one though, I don’t know how it really works =)

1 Like

i know ur first point, i am asking about the second point.
imagine i want to get a bool from my character object so in utube they create an interface that returns that bool and cast to that interface instead of casting to the who character object as they r saying that is faster and save memory. but in OOB interface is not used for this approach i think, so i want to know before i cast to my character in many class if casting to interface better.

You don’t use interfaces for performance reasons. Interface functions are virtual and cannot be inlined, so if anything, there is a performance cost. However, programming at a gameplay level means that the penalty is practically always of no concern.

Interfaces are used for decoupling. The caller does not need to know anything about the class implementing the interface. This can save memory (because the caller only needs to know about the interface and not every class it could possibly call) or at the very least let you avoid undesireable inheritance hierarchies.

1 Like

so to make sure that i understood u, u suggest to cast to interface to save memory.
for example if i want to get character’s current health, i should implement a function called gethealth() in an interface class (CharacterInterface) and then cast to this interface to get the health only and by this way i will safe memory because i did not get a reference for the whole character object ?
Note: if it saves memory then the game is more optimized then how it does not effect performance ?

Think of something more generic, like an interface for interactions. Not every interactable object is necessarily going to be on every map at once, so you don’t need to add them to the memory.

If the character is going to be on the map anyway and that is the only class that has the interface you aren’t saving any memory. A pointer/reference is a memory address.


so if i am casting to the character in another class every tick to get only the character speed, i do not need to create a getspeed funtion inside interaction interface class to avoid hard reference and save memory ?

The character is probably a bad example since if it is already loaded it will not need to be loaded again.

I think you are missing the big picture of all this. performance tricks like interfaces and whatnot are important for repetitive, time consuming tasks. then is my understanding that one needs to implement C++ code, optimized and the rest.

So if you are checking the health on your player once a frame is like a drop a in a huge bucket of available computer power. Checking the life on 10000 Ai actors may probably be an issue and you need some custom high level stuff. maybe. Or maybe the engine is optimized to do this anyway.

What I found useful for the interfaces is that I can reuse the code if for example I change the name of the player class blueprint. now someone told me on this forum that I can cast to the base class. But I’m not sure I can then use the custom functions from the child class.

This is a mental trap I got tangled all the time. Starting to think about performance like I’m creating some sort of high level real world Formula 1 car that needs everything ultra-optimized, when in reality I’m building a wooden cart.

Yes it is easy to fall in the trap of thinking about performance before readability but in reality readability IS performance, since code is written by people and people tend to make mistakes especially when the readability is poor and mistakes often cost a lot of performance.

Speaking of hard references, if you avoid hard references it also makes refactoring quicker and the editor load faster etc. so decoupling has several benefits.