Help me understand casting

Alright so i try to understand casting but its really hard. First of all thought casting ment like Cast a ball, like throw it. so i thought it worked like a message. but then i realized that it actuly means casting like an actor in a movie.

So i kinda understand how it works but i still have a hard time to understand exacly what to input to this pin

Example i wanna cast from my HUD to my character.

So i add a Cast to FPSCharacter in my HUDblueprint, then i try to plugg “Get Player Character” into that pin but it says “Is already casting to it” or something, so what should i plug in there? what object is it searching for?

HI yghtim,

Casting is an interesting topic. Effectively what you are doing is checking an object to see if it is of the type the node is casting to. So in your case, Cast To “LargeCube_Blueprint” is taking whatever value that is plugged into the object pin and checking to see if it is, in fact, a LargeCube_Blueprint or a child blueprint based off of LargeCube_Blueprint. If it is, you can run one set of instructions, if not, you can run another. The output “As Large Cube Blueprint” pin allows you to get information from the LargeCube_Blueprint. If, for instance, you needed to get a variable from it, you can drag off of “As LargeCube_Blueprint” and type "Get ", this will get a copy of the variable from the specific copy of LargeCube_Blueprint that you are currently working with.

If a variable has already been populated by something like “Get all Actors of Class”, you will not need to cast because the variable is already of type .

Think of casting like television actors. We can use Firefly for an example: Nathan Fillion (An Actor) is a parent class over the acting role (Pawn) of Malcolm Reynolds (A Character) and can therefore Cast To Malcolm Reynolds. While Nathan Fillion does not have a Gun, Malcolm Reynolds does and he can then easily cast to Malcolm Reynolds to get it.

Kaylee is not directly connected to Malcolm Reynolds, so she would have to find a way to reference Malcolm Reynolds. Then she could use that reference to Cast To Malcolm Reynolds and tell him to use the Gun. Now, let’s say we have multiple Malcolm Reynolds in the same environment, but Kaylee wanted a specific one to shoot the gun. That’s where a ForEachLoop comes in. It checks the array, casting each to Malcolm Reynolds, which gives Kaylee the access she needs to get a specific (for the example lets say Malcolm number 2 in the array) Mal to fire. Even further, the Cast To on an array of all actors in the show would allow her to specifically target Mal instead of River or Sheperd Book.

The TL: DR or less nerdy version: Any actor can cast to any other actor as long as they have the proper connections to do so. You use the Cast To node in a ForEachLoop to check for things such as whether or not the actor is cast to a specific class and/or to get specific instances of the class within an array, that way you can effect either all of them or just one.

hmm okay, but im still a bit confused to what exacly to plug in to the Object pin. You said it needs to se if LargeCube_Blueprint is in fact LargeCube_Blueprint? So i try to make a reference of LargeCube_Blueprint and plug it in there, but it just gives me a yellow warning sign, telling me im looping it or something. Not at home right now so i dont know what it says. So i still dont understand what i should plug in there.

A reference to another object.

Imagine it like a phone. You need a number to call someone (the object reference). Once you have that number you can start a call. Let’s say you call a random family. One father, one mother, one and one daughter. Someone picks up without saying anything. Casting is getting to know who is on the other side of the phone. All of those 4 people are part of the family and all could pick up the phone. But you only want to talk to the father because you’re a buddy from work.

Now let’s move this metaphor over to the real world.

You need an object reference. Let’s say your character just collided with another character. We can use “Event Hit” to do something as result of that. And as part of the “Event Hit” we get a “Hit Result”. Inside of that we get a reference to “Other Actor”. The Actor we just collided with! But what is it? Is it a bullet? Another player? Just a cube from the level? A teleporter?

Maybe we want to do something if it’s a bullet / projectile! So we Cast it from the “Other Actor” which is just a generic reference to some Actor. It can be any child of Actor so basically anything that could be placed in a level. We take that reference and Cast it to our Bullet Class / Blueprint. Let’s say it’s called BP_Bullet. So let’s take “Other Actor” and “Cast To BP_Bullet”. If it was successful we can now use the Other Actor like it is of the type “BP_Bullet” rather than just the generic actor. We have access to the variables in it as well as the events and functions. If it isn’t we will get the signal from “Cast Failed” and know: Hey! It’s not a bullet!.. do I want to do anything else if it’s something else? No. Cool. That’s it. Wrap it up everyone! We’re done here!

Maybe this helped.

In short: The reference you need there needs to come from somewhere. Casts don’t create anything. You need to get a reference to your object in another way.

Disclaimer: Pretty bad way of doing this but serves to illustrate a possible use case:

  1. Your game features a forest full of animals.
  2. At midnight you want all the wolves in the forest to howl at the moon.
  3. To do this, when a timer reaches midnight, you fetch all actors in your level
  4. Iterate through the list 1 actor at a time (assume you’re using a foreach loop)
  5. On each actor you “CastTo” Wolf and then call your “Howl to the Moon” function that you created in your Wolf class.

You have to use the CastTo because your list was made up of “Actors”. The generic Actor class doesn’t implement any method called “Howl to the Moon”. The input Object is the reference to an Actor from the list. Every actor in the list would be fed into that object reference one Actor at a time.

What the CastTo is doing is kinda saying: “Treat this Actor like a Wolf”. A cast will fail if the actor is not actually a wolf.

Japanese Translation