Interfaces VS Casting - have a bit of confusion

I hear that the use of interfaces is often recommended VS casting, but I’m somewhat confused on one point: how do you get the reference to your target blueprint without casting? Conceptually I’m having a hard time grasping this.

What do you mean exactly? interfaces would require a reference as well.

Interfaces are not a replacement to casting, so it depends.

Think of an interface as a topping on your class. If your class implememts an interface (UObject has a Implements<>() function) you can call your interface function with IMyInterface::Execute_SomeFunction(MyObject);

You can also cast MyObject to IMyInterface with Cast<> and call SomeFunction() directly, but this can cause issues if MyObject is a Blueprint class so it’s much preferable to use the Execute_ versions.

Interfaces are sort of like a “blind cast”.

If you know a target object implements an interface, you can call an interface message on it, without knowing what class it is.

Example: Suppose I have an object which can only overlap pawns, and when it does I want it to tell that pawn to play a reaction animation. The ability to play that animation isn’t native to the pawn class, but in order to cast it to a specific type of character I have to know the character TYPE, and I don’t know if it’s a playercharacter, an enemycharacter, an emenycharacter_heavy, etc. so I can’t cast to it.

But, as long as I know (due to collision settings or whatever) that the overlapping actor is of a kind which implements an interface, I can just take that Object reference and call the desired interface message on it. Then, the interface can be implemented on each kind of actor to get the desired reaction.

I use an Interface message (and corresponding BP-able component which actually implements the interface itself as well) for all actors in my game that take damage. The player doesn’t know if what he is attacking is a certain kind of enemy, or a certain kind of attackable set piece, or what… But it can use interfaces to report things like damage, desired reaction, desired knockback, etc and rely on the implementing actor to interpret that.

Ah… yes, this helps immensely. So essentially you need a reference to the object you are calling the interface on, but you don’t actually need to know what type of object it is.