Casting and Interface, memory usage clarification.

Hi all.

I wished to ask for some clarification and couldnt really find an answer on my own so apoligies if this has been asked before.

While I understand how both casts and interfaces work functionally wise and how to use them, I feel like I maybe have confused myself with the memory side of what they actually do. I previously saw interfaces as a way to avoid hard references, which for the most part has actually helped me avoid issues and improve performance on a project. (I try my best to use vague references such as AActor or UUserWidget for interface functionality and avoid direct references such as AMyActor or MyUserInterface)

However, I was recently told that interfaces are pointless to use on something that is already / constantly loaded into memory such as the game manager or player character and that it is fine to cast directly between these already loaded ones as the memory is already allocated for use. (as previously I was just spamming get player character or get gamemode to trigger interfaces easily)

Is this true for the most part, have I been using over using interfaces or using them in the wrong way? Is it fine to cast between already loaded systems without causing problems with hard references ?

I am still fairly new to memory management and I am trying to learn the best way to keep usage low as possible.

Hi,

yes that is a common misconception.

“Don’t cast” and “Only use Interfaces” are both not entirely correct…

Casting

You’re correct in saying that casting to an object will create a hard reference and force load the object into memory.
This is not only relevant during Gameplay but also decides how assets will be loaded together.

If (for instance) inside actor BP_Apples you cast to another actor BP_Banana and you spawn the BP_Apples actor into the level, it will also force load the actor BP_Banana.
And if BP_Banana also has a cast to BP_Cherry, it will force load the BP_Cherry too!
(this chain can go on and on)

Meaning that if you spawn BP_Apples, it also will load BP_Banana and BP_Cherry because they’re hard references← and you don’t even need to execute the actual Cast node, they will be loaded right away / on spawn !
And depending on the size of the hard references, this can cause a hitch in your game or even very poor performance.

Interfaces

Using Interfaces to avoid hard references is indeed a good approach.
Since interfaces are very tiny objects, casting to an interface (when you want to call a function on it), won’t impact your game much.

Besides that, using Interfaces to add a layer of abstraction to your code can help staying organized.

Interface or Cast

Yes that (technically) is correct.
Anything that will always be in memory is totally fine to cast to.
For instance the PlayerCharacter, GameMode, etc will always be created when the Level starts playing.

But there is a downside when it comes to Garbage Collection.
Most likely you will cast to BP_MyPlayerController and save that reference inside a variable for convenience.
Forgetting to clear that variable will prevent UE from garbage collecting it and freeing up the memory that was used.

At the end of the day, a mix of both Interfaces and Casting will be totally fine.
In some places, it might even be good to use interfaces on objects that you know will always be loaded either way.
E.g., an interaction system where you have interactable target interfaces (for interactables) and interactable instigator interfaces (the actor interacting)
This way you could share functionality for interacting on multiple actors.
Maybe the Player Controller will have the interactable instigator interface but also an AI Controller, if you like to have Bots that can Interact with things.

I hope this helps :slight_smile:

2 Likes

Thank you, its kind of you to make such a detailed response for me ! I really appreciate it.

I do believe this brings me confidence on the subject a bit better then before and clears up my confusion. I really appreciate the help!

1 Like

I’m glad to hear that!
Feel free to reach out if you have any further questions