Casting has almost no performance impact, it is old myth that casting is slow.
It probably started by more experienced ppl telling less experienced ones “do not cast in loop, cast once and store variable”
What i am doing with most of things:
- i use middle man setup, for me my favorite middle man is BP_GameMode
- then i make blueprint function library, call all functions BP_Core_PlayerPawn, BP_Core_TheHUD, etc. so when i need reference i just write “CORE” and get them all
- now my core functions find correct actor/blueprint and cast to it then return correct class
- every actor that needs such reference executes CORE function and stores CASTED variable with reference, so no more casting during runtime (only at begin play)
- my core functions also have “IsValid” check and return boolean “success?”
- oh and those CORE functions are all blueprint pure.
So no more problems with casting.
About interfaces:
- they are useful only when you want single code to communicate with different blueprint (C++) classes. So you put interface in all of them like BP_rock BP_Tree BP_Bird , and same interface to all of them, so you cast to interface not all different BP classes
- for any other setup, its easier to just cast to (with CORE function) then create custom event or function in casted to blueprint, and call that event/function directly
- or create dispatcher in middle man like BP_GameMode, all victims get its reference from get_CORE_GameMode, store that as variable and assign to dispatcher in GameMode. But this is when middle man talks to many different actors
ps.
also (probably) another reason why cast to is considered “bad”.
when from for eg GameMode (or level bp) you get all actors of class: cast to them all in loop store all references in array. So as long as that array with references exists, all those actors are keept in memory, doing their tick and whatever. Then those actors may get all actors (of different class) and store all those references in array. This code would be allocating all the memory for not used actors. This is really bad. So use middle man blueprint that is always loaded, and store reference to it, instead other way around.