|
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.
Inside the Viewport select one of the LargeCube meshes, then in the Details panel click the Blueprint/Add Script button.
At the confirmation window that appears, click Ok to use the default name and create the Blueprint.
In the Content Browser under Content/FirstPersonBP/Blueprints, open the FirstPeronProjectile Blueprint.
This is the Blueprint for the projectile that is launched when the player fires the weapon during gameplay.
Locate the Event Hit node and drag off the Other pin then search for and add the Cast To LargeCube_Blueprint node.
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 LaregCube_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.
Off the As Large Cube Blueprint pin, use Get Static Mesh Component and then Set World Scale 3D and connect as shown.
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.
Off Static Mesh Component use Get World Scale and connect to a Vector * Float pin (set to 0.5) then plug into New Scale.
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.
Right-click in the graph and add a Get Player Character node, then off Return Value, Cast To FirstPersonCharacter.
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.
Connect the Set World 3D node to the Cast To FirstPersonCharacter node.
Off the As First Person Character pin, add the Get Character Movement node.
We are going to adjust the character's speed whenever they hit the Large Cube, making them run faster.
Off the Character Movement node, add a Set Max Walk Speed and Get Max Walk Speed node.
Off the Get Max Walk Speed, use a Float + Float node (set to 200) then connect that to the Set Max Walk Speed pin.
Once you have connected the nodes as shown above, this will allow the player character to move faster each time the cube is hit.
For both of the Cast To nodes, connect the Cast Failed pins to the Destroy Actor node.
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.
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.
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.
相关页面
Casting
蓝图交互的用法
蓝图用户指南
|
|