About Blueprint Casting

This is as simple as polymorphism goes goes… so not that much simple. It allows us to represent object as something else. Here is some example:

You want to have object that is a “light emitting object”. And it can be tons of stuff. You want to make sure you turn on the lights on those object, but from some LightController.

You create interface LightEmittingObject that has functions TurnLightsOn and TurnLightsOff
Then you create LightController object. It has a list of LightEmittingObject. When it decides that light need sto be on, it iterates trough list of those object and either turns it on, or turns it off. But it does not have to care what that object is in general. It can be car light, street light, house, lamp… etc.

Then you create car, implement LightEmittingObject, add logic for two functions to turn on and off the lights.
Then you create Lamp, House, Street Light… etc. And do the same. In their BeginPlay, you add them to the LightController list of LightEmittingObjects, and he controls when without even knowing if it’s car, house… or whatever. All it cares is that they are LightEmittingObject. And that is it.

Just make sure you don’t assume something can be cast to something. Because that can lead to nullptr.

Hope this helps a bit.

1 Like