Casting in Blueprints

Choose your OS:

When using a Cast To node in Blueprints, put simply, you are attempting to check if the object you are casting from is the specific object you casted to. In other words, say you have created a special Character Blueprint (called MyCharacter for example) that has variables in it or other custom functionality contained within it and assigned it as the Default Pawn Class (or the Character Blueprint all Player Characters use by default).

If you want to access properties of the Player Character from another Blueprint, you can use the Get Player Character node and affect the Player Character in very general ways (setting/getting its location, rotation, etc.) but you cannot access any of the custom functionality you have added to the MyCharacter Blueprint you have set up as you are getting the Player Character but not a specific type of Character.

By using the Get Player Character node, then using a Cast To MyCharacter node (the special Character Blueprint), you can then say if the Player Character is MyCharacter, let me access the Variables, Functions, Events or any other special functionality contained within that Blueprint.

On this page, you will learn about some of the use cases for Casting, see an example for using a Cast To node, read about Target Blueprint Casting as well as other types of casting.

Example Use Cases

Below are a few examples of when Blueprint Casting could be used:

  • You want to access a specialized version of another Blueprint.

    • Character walks into fire causing a Health Value to deplete.

      • Cast to your special Character Blueprint to access and change the Health Value.

    • Your character dies and you need to respawn them.

      • Cast to your special Game Mode Blueprint to execute a respawn script.

  • You want to access multiple Blueprints of the same class and modify them all the same way.

    • You have several lights in your level and you want them all to turn on or off when an event occurs.

      • Cast to your Light Blueprint and execute a function that turns the light off.

  • You want to access a specific Child Blueprint.

    • You have several Blueprints based off an Animal Blueprint (Cat, Dog, Bird) and you want to access one of the animals.

      • Cast to Cat, Cast to Dog or Cast to Bird to access their respective Blueprints and unique functionality.

Setting Up a Cast To Example

The example below shows how you can access one Blueprint from inside another Blueprint by using a Cast To node.

For this example, we have a fire effect Blueprint in our level (which is an Actor) and we want it to communicate with the playable Character Blueprint the player is using. When the player enters the fire, we want to send a signal to the Character Blueprint that the player has entered the fire and that they should now take damage. By using the Return Value of an OverlapEvent, we can Cast To our Character Blueprint and access the Events, Functions, or Variables within it.

3_0a.png

  • The fire effect above is the Blueprint_Effect_Fire asset (included with starter content).

  • A sphere component named Trigger was added to the Blueprint and was set to OverlapOnlyPawn for its collision.

Using Blueprint Casting, we would do the following:

  1. The Character Blueprint assigned to the Default Pawn Class (the playable character) is our Target Blueprint we want to access.

    3_0b.png

    You can view the Default Pawn Class from the Edit menu under Project Settings in the Maps & Modes section.

  2. Now that we know our target is the MyCharacter Blueprint, inside it we create a Bool variable that states if the player Is on Fire.

    3_0c.png

    Above the Event Tick feeds a Branch where if True, we print Apply Damage to the screen (off True is where you would have your apply damage script).

  3. Inside the Blueprint_Effect_Fire Blueprint, we add two events for the Trigger: OnComponentBeginOverlap and OnComponentEndOverlap.

    3_1.png

  4. With the Events added, we drag off the Other Actor pin and enter Cast To My in the search field.

    3_2.png

    Here we check/assign the Actor (MyCharacter Blueprint) we want to trigger the event and Cast To it so that we may access it within the fire Blueprint.

  5. Select the Cast To MyCharacter option.

  6. With the node added, we can drag off the As My Character C pin and access the Events, Variables, Functions, etc. within it (in this case Set Is on Fire).

    3_3.png

  7. Both Events in the Blueprint_Effect_Fire Blueprint would then look like this.

    3_4.png

    When overlapping the fire, we are setting the IsOnFire variable in the MyCharacter Blueprint to True and setting it to False when not overlapping it. Inside the MyCharacter Blueprint, when IsOnFire is set to True via the fire Blueprint, we print to the screen Apply Damage (or if you have created a Health/Damage system, you could apply damage and reduce player's health here).

Target Blueprint Casting

There are also instances where you can use a variable to Cast To a specific type of Blueprint in order to access it.

CreateCasting.png

In the image above for number 1, a Save Game Object is created and assigned to a SaveGameObject variable. That variable is then used to Cast To a Save Game Blueprint called MySaveGame - which could be used to pass off or retrieve save game information such has a high score, best lap time, etc.

In the image above for number 2, a Widget Blueprint is created and assigned to a UserWidget variable. That variable is then used to Cast To a Widget Blueprint called MyWidgetBlueprint - which could be used to update or retrieve information from the Widget Blueprint (which could be a HUD or other UI element you want to access).

Other Types of Casting

There are some special functions that can be used to Cast To different classifications of Blueprints.

OtherCasting.png

In the example graph above, the following examples are given:

Blueprint

Description

Character (1)

Here the Get Player Character node is used and we are casting to a Character Blueprint called MyCharacter.

PlayerController (2)

Here the Get Player Controller node is used and we are casting to a Player Controller Blueprint called MyController.

Game Mode (3)

Here the Get Game Mode node is used and we are casting to a Game Mode Blueprint called MyGame.

Pawn (4)

Here the Get Controlled Pawn and Get Player Controller nodes are used to cast to a Pawn Blueprint called MyPawn.

HUD (5)

Here the Get HUD and Get Player Controller nodes are used to cast to a HUD Blueprint called MyHUD.

In each of the examples above, dragging off the As My X (where X is the type of Blueprint) node will allow you to access the Events, Variables, Functions, etc. from their respective Blueprints.

Also of note, the Player Index value in the Get Player Character and Get Player Controller nodes can be used to specify different players in a multiplayer scenario. For a single player scenario, leaving these as 0 is fine.