Blueprint Casting Example

Choose your OS:

This page covers how to use Blueprint Casting to communicate with different Blueprints and share information. In this example we allow the player to shoot an object, making it smaller each time it is hit. Each time the object is hit, it also notifies the Character Blueprint and increases the player's movement speed.

For this guide we are using the Blueprint First Person template with Starter Content enabled.

  1. Inside the Viewport select one of the EditorCube meshes, then in the Details panel click the Blueprint/Add Script button.

    Casting1.png

  2. At the confirmation window that appears, change the name to LargeCube_Blueprint and click Ok to rename and create the Blueprint.

  3. In the Content Browser under Content/FirstPersonBP/Blueprints, open the FirstPersonProjectile Blueprint.

    Casting2.png

    This is the Blueprint for the projectile that is launched when the player fires the weapon during gameplay.

  4. Locate the Event Hit node and drag off the Other pin then search for and add the Cast To LargeCube_Blueprint node.

    Casting3.png

    On the Event Hit node, the Other pin refers to the other Actor that was hit. By using the Cast To node, we are checking to see if it is indeed the LargeCube_Blueprint that was the other Actor that was hit. If it is, we can access it and execute any Events or Functions within the Blueprint, change Variable values inside of it, or change the properties of the Components that are inside the Blueprint.

  5. Off the As Large Cube Blueprint pin, use Get Static Mesh Component and then Set World Scale 3D and connect as shown.

    Casting4.png

    After we Add Impulse at Location we are then casting to the Large Cube Blueprint and getting the Static Mesh Component inside it (the actual Cube) and setting a new World Scale 3D value for it. Currently the New Scale is set to 0 however we will reduce the size of the Large Cube Blueprint each time it is hit.

  6. Off Static Mesh Component use Get World Scale and connect to a Vector * Float pin (set to 0.5) then plug into New Scale.

    Casting5.png

    This will get the current World Scale of the cube and multiply it by .5 (or half its current size) each time it is hit, reducing its size.

  7. Right-click in the graph and add a Get Player Character node, then off Return Value, Cast To FirstPersonCharacter.

    Casting6.png

    Here we are checking if the player character is using the FirstPersonCharacter Blueprint, which by default is the case with this template. By doing so we can now access the Events, Functions, Variables or Properties of the player character and modify them.

  8. Connect the Set World 3D node to the Cast To FirstPersonCharacter node.

  9. Off the As First Person Character pin, add the Get Character Movement node.

    Casting7.png

    We are going to adjust the character's speed whenever they hit the Large Cube, making them run faster.

  10. Off the Character Movement node, add a Set Max Walk Speed and Get Max Walk Speed node.

  11. Off the Get Max Walk Speed, use a Float + Float node (set to 200) then connect that to the Set Max Walk Speed pin.

    Casting8.png

    Once you have connected the nodes as shown above, this will allow the player character to move faster each time the cube is hit.

  12. For both of the Cast To nodes, connect the Cast Failed pins to the Destroy Actor node.

    Casting9.png

    In the event that the player hits something that is not the Large Cube, we still want to call the Destroy Actor node to destroy the projectile. When hitting the Large Cube, in the event that it is not the First Person Character that hit the cube, we also still want to destroy the projectile.

  13. The ending sequence for the Projectile Blueprint should look similar to below.

    Click image for full view.

    We used Reroute Nodes to adjust the flow of our wires for clarity, but your connections should be the same.

  14. Click Compile and Save, then the Play button to play in the editor.

    Each time you shoot the Large Cube Blueprint, it will reduce in size and your character will move faster.

    For more information on Casting and usage cases, refer to the Related Topics section below.