About Blueprint Casting

When I was studying Casting, I saw the description on Unreal Documentation: “you are attempting to check if the object you are casting from is the specific object you casted to.”
My understanding of this is that compared with the ordinary method of directly creating an object reference variable, Casting only has one more judgment and gives us an option after failure. That is to say, we need to use Cast only when we are not sure whether the thing we encounter (the thing we want to call) is really the thing we want.
In addition, if we are sure that the actor we want to call is the one, we can directly get the object reference variable of the actor. In this case, pure cast and object reference variable are the same.
I’m not sure whether it is really as I understand it. Please give me some suggestions.

As shown in the figure below, I can create an Actor variable and cast it to My Light, or I can directly refer the Actor variable to the object reference of My Light without using cast. These two methods can both enable me to access functions in My Light.

Thanks

Please do not fall back on casts for this.

This is a task for an interface. Read up on interfaces and decoupling. It will change the way you program for the better.

5 Likes

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

Thank you so much for your detailed explanation. I know there are better methods such as interface and dispatcher. I just want to make sure that my understanding of Direct communication is correct. Cuz I feel many explanations in this regard are vague. In many tutorials, it seems that you can directly refer the Actor to the instance in the viewport to access the functions without casting.

Yeah, the interface is a better solution than Casting. But just there are still some uncertainties in my understanding of casting. I want to wrap my mind around it.

Its not complicated. You just do a simple test

Can this unkown class A become the casted class B if so you get a new return of class B.

Unknown A => cast ok to B => Cast returns A a copy of class type B.
You can now use all of B’s functions and variables in the newly returned class.

If it fails then you do down the failed cast pin.

1 Like

Take one of the most common examples for casting: The player character.

In a lot of cases you, as the programmer, know what the player character is, but the game doesn’t.

So you might do “get player pawn” then to access your custom events and functions, you cast to the character you know it should be.

Is the character ThirdPersonCharacter? Is it a custom character you created? Using a cast you’ll confirm it and then can go from there.

Another use is for overlaps and hit events, they return an actor, you can use cast as a test that the overlapped actor is the player. If cast fails, do nothing, on success, do whatever it should for the character

1 Like

Yeah,I can understand your example of overlapped. But for cases like getting player, we are actually getting a child from the parent class. Then why can’t we directly create a variable and set it as an object reference? This way of direct communication seems to have the same result as casting.

Yes you’ll want to create a variable to avoid casting over and over, a very common method is cast once on begin play and set your variable, then use the variable going forward.

Even if you create a variable of a specific actor type, you still need to set it with a reference to an object

1 Like

Sorry for my rephrasing. This question may not be of much value, but I just want to make it clear.
In addition to detecting the other object in the case of overlapped, I can’t seem to think of the difference between casting and direct communication. As in the following example, Casting seems unnecessary

What you did was create a variable of a ThirdPersonCharacter type, but it has no idea what object in your scene it’s referencing.

Delete the “As BP Third Person Character” and connect the “BP Third Person Character” to the cast instead. You do not need 2 variables of the same type - unless you have 2 “Third Person Characters” in your scene you want to keep reference to, of course.

You are getting the reference for this variable by using “Get Player Character”, then casting to the type of character you want it to be, to confirm it is what you expect, and on success you are then setting the variable with your reference.