How do I "Cast to" these Blueprints?

In general with huds you would want to modify the data inside the hud event graph itself. so inside the hud bp you would get a reference to the character or whatever graph has the variable you are trying to reference. I believe if you make the variables discoverable you can reference them at will on any blueprint without casting to that object character w/e.

The cast to is the object reference and the wildcard is the specific item if that makes sense. Example Cast to third person character =>= wildcard player character. in this example tp character is just a class (because there can be more than 1 tpcharcater in the game if that is a class of ai or whatever) and the specific character is the player character.

in your example before the cast to widget you need to add the widget component before you can cast to it iirc. try adding the widget then cast to it or go into the widget bp and cast to whatever bp you have that has the variable you are trying to modify. Hope this helps

This is just a general casting question so I can better understand things…

So I can “Cast To” things like my player, my game mode, my game instance easily through setups like the ones seen on the left hand side in the pic below. How, though, can I easily cast to OTHER blueprint actors I wish to cast to (like a widget blueprint that I created)? For example, let’s say I wanted to access “cast to” a widget blueprint I created so I could modify some variables within it…what can I input as the “Object” of the “Cast To” node?

As another “general understanding” type of question pertaining to casting, when I AM inputting nodes such as “Get Player Character,” “Get Game Mode,” and “Get Game Instance” into the “Object” inputs of the various “Cast To” nodes seen below…exactly WHAT am I inputting there? Just a general reference to that particular parent class? Why are those needed?

Thanks in advance!

“Casting” is a programming term used in object oriented programming. A more non-programmer name for “Cast to” could be “Treat as”. Before you can understand Casting you need to know what an Object is.

What is an Object?

Every Blueprint/Class needs to be Spawned/Created before you can use it. This is called Instantiation. When a Blueprint is spawned using the “SpawnActorOfClass” node you will notice it has a return value on the right. This is the constructed object reference normally simply referred to as the object.

Why is “Casting” even necessary?

Lets use an existing Class hierarchy in Unreal

  1. Class Actor
  2. Class Pawn(derived from Actor)
  3. Class Character(derived from Pawn)

The Actor Class has a function called ApplyDamage
The Pawn Class has a function called PawnMakeNoise
The Character Class has a function called Jump.

Now lets say you have spawned 3 different objects. One Actor, one Pawn, and one Character.

You can call ApplyDamage on all of them because each one “IS A Actor”
You can call PawnMakeNoise on the Pawn and the Character because each one “IS A Pawn”.
You can call Jump on only the Character because it is the only one that “IS A Character”

Now what if the Actor object is actually a RedExplodingBarrel BP derived from Actor and you want to call the Custom Event “Explode” you made which has a GetOverlappingActors with a filter of Pawn because we want to be able to detect anything that derives from Pawn including Pawns. This function only returns Actor references.

Why is it returning Actor references and not Pawn references when I set the filter to Pawns?

That is because the GetOverlappingActors function is made in the Actor class and the Actor class has no idea what a Pawn is because a Actor “IS NOT A Pawn”. It can only return its own type or a type it is derived from.

But I want both the Character and the Pawn to call the function “PawnMakeNoise” and the Character should Jump into the air

This is where Casting comes to the rescue. lets say GetOverlappingActors detected both the Pawn and the Character and returned two Actor references. Now you know that they are both Pawns but GetOverlappingActors doesn’t know so you CastToPawn and call the PawnMakeNoise function that you wanted. If one of the returned Actors was not a Pawn the cast would fail and nothing would happen. Now you also wanted the Character to jump so you now need to CastToCharacter and if it doesn’t fail you can call Jump on the Character.
Another scenario would be if you just want to ApplyDamage to the Pawn and the Character then you would not need to Cast at all since ApplyDamage is a function on the Actor class so all you need is a Actor references which GetOverlappingActors already provide. Hope this helps.

1 Like

Thanks for this reply.

I usually don’t have trouble with all of this…the ONE trouble spot I typically find is when I want to “talk to” a Widget Blueprint that is being created elsewhere. Here’s an example of a problem I sometimes run into…

Let’s say I make a Widget Blueprint called “HUD” and that this Widget BP is created and added to the viewport from within my Game Mode Blueprint. Within this “HUD” blueprint, I’ve created an ammo pickup interact message that I ONLY want to make visible when my player character is overlapping an ammo pickup blueprint I’ve created. What I WANT to be able to script from WITHIN my ammo pickup blueprint is an overlap event which prompts my “HUD” widget blueprint to now make visible the ammo pickup message contained within my HUD widget blueprint.

There doesn’t seem to be a way I can easily toggle the “showing / hiding” of the ammo pickup message from WITHIN the ammo pickup blueprint! And a big part of this is because I can’t do an “Event Begin Overlap —> Cast to HUD” (to “talk” to my HUD widget blueprint) because I have no clue what I can plug into the “object” input from the cast node to fulfill that requirement.

I know there are workarounds to this communication (I use them) but it’s frustrating to me that I can’t seem to be able to use an event overlap from within one blueprint (in this case, an ammo pickup blueprint) and DIRECTLY be able to set the visibility of an “ammo pickup” message from within my HUD widget blueprint.

Any suggested remedies to that sort of situation?

Thanks for this reply.

I usually don’t have trouble with all of this…the ONE trouble spot I typically find is when I want to “talk to” a Widget Blueprint that is being created elsewhere. Here’s an example of a problem I sometimes run into…

Let’s say I make a Widget Blueprint called “HUD” and that this Widget BP is created and added to the viewport from within my Game Mode Blueprint. Within this “HUD” blueprint, I’ve created an ammo pickup interact message that I ONLY want to make visible when my player character is overlapping an ammo pickup blueprint I’ve created. What I WANT to be able to script from WITHIN my ammo pickup blueprint is an overlap event which prompts my “HUD” widget blueprint to now make visible the ammo pickup message contained within my HUD widget blueprint.

There doesn’t seem to be a way I can easily toggle the “showing / hiding” of the ammo pickup message from WITHIN the ammo pickup blueprint! And a big part of this is because I can’t do an “Event Begin Overlap —> Cast to HUD” (to “talk” to my HUD widget blueprint) because I have no clue what I can plug into the “object” input from the cast node to fulfill that requirement.

I know there are workarounds to this communication (I use them) but it’s frustrating to me that I can’t seem to be able to use an event overlap from within one blueprint (in this case, an ammo pickup blueprint) and DIRECTLY be able to set the visibility of an “ammo pickup” message from within my HUD widget blueprint.

Any suggested remedies to that sort of situation?

You are having an issue creating a reference to the Widget BP so that it can be accessed from within the ammo pick up. These videos may help you figure out how to create the necessary references, and/or explain casting and references in a way that you will be able to figure out how to create your widget so it is easily referenced.

Particularly videos #1 and #25

And this shows how to reference a viewport widget from another widget

You could use the GetAllWidgetsOfClass in the pickup Blueprint and get the reference that way.