Training Stream - Blueprint Communications with - February 23rd

No, that’s not what I’m saying. I’ts not a question of safety, it’s a question of accessibility. When you cast to a given class, you’re just verifying that the incoming reference is of a given type. If that cast is successful, then you will only have access to those properties that exist within the class you cast to, or anything it has inherited from its parent classes.

Let’s consider a ThirdPersonCharacter. It’s parent class is Character, so its class hierarchy would look something like this:

  • Object
  • —Actor
  • ------Pawn
  • ---------Character
  • ------------ThirdPersonCharacter

So let’s say you get a reference to this object. References are just like variables in that they all will have a type. Think of the term “type” in this case just as you would the “specific class” to which the object belonged, or the lowest member of the hierarchy. In fact, it’s a good idea to think of a class hierarchy in terms of how specific it is. More things are near the top, more specific things are near the bottom. Whenever you hear “” you should always think, “further up the hierarchy.”

Most references you’ll get in your Blueprints are going to be of a very type. The most common gameplay reference type is Actor (as a reminder, just about anything that actually lives in your game worlds is probably based on the Actor class). If you got a reference to this object it will more often than not be an Actor reference*. You could cast the reference to anything along the above hierarchy chain and the cast would succeed. However, you would not necessarily be able to access the data you need. As an example, let’s say you wanted to access the FieldOfView of the CameraComponent on the ThirdPersonCharacter. If you cast to a Pawn, that would succeed! Great! But a Pawn does not have a CameraComponent, so you can’t get to the FieldOfView data.

That is why we cast. We need to verify that in incoming reference is of the type you really want to communicate with, so that you can get access to the data you need.

*The exception is in the event you get a reference that is already the type you need. For example, you might have set up a way to get a reference of type ThirdPersonCharacter, then there would be no need to cast in order to access its CameraComponent and the field of view. In fact, if you do try to cast, Unreal will warn you saying that the cast will always test true and is therefore superfluous.

To your other point, how are you accessing variables without using a Getter or Setter? :slight_smile: