Legacy/Blueprint Power Up Tutorial
- 1 Overview
- 2 Components and Setup
- 3 Contruction Script
- 4 Event Graph
A power up is a fundamental gameplay mechanic, and placing the art and functionality of a power up area inside a Class Blueprint makes it easy to replicate multiple instances of the area within a level, as well as throughout a game as a whole. Using a Blueprint to create a new class makes it easy to tweak the behavior and appearance of the class and have the altered properties propogate to all instances. Although this example simply changes the scale of a pawn to represent a "power up", this example can serve as a starting point for adding any kind of buff or debuff functionality to your game.
The gameplay behavior of the PowerUp Blueprint illustrated here is that if a player enters the glowing dome, the dome flashes to signal that it has been activated, while the character grows to twice its original size. After a delay, the character will be scaled back to its original size. This example will show the logic for triggering a Timeline to play after some event fires, and then reverse that Timeline after a delay.
Components and Setup
The PowerUp Blueprint contains two components: a Scene Component and a Static Mesh. The Static Mesh is displayed in the level when the Blueprint is added to the level. The Scene Component is used as a parent for the Static Mesh, so that the Static Mesh can have relative transformations. Without the Scene Component, each time the Blueprint is added to the level, the scale and position would have to be adjusted so that only the top half of the sphere shows above the ground.
Execution in the Blueprint's EventGraph needs to be triggered when the pawn overlaps the Static Mesh, so the Collision Profile of the Static Mesh has been set to OverlapOnlyPawn.
No Material has been applied to the Static Mesh in the Details tab. However, a Material is set in the ConstructionScript for the Blueprint. The Preview Viewport shows how the Blueprint will look after the ConstructionScript has executed.
The ConstructionScript executes when an instance of the Blueprint is placed in the level and:
- Creates a Material Instance Dynamic, or MID and saves it to a Material variable
- Applies this Material to the PowerVolume Static Mesh component.
The Material Instance that is set for the Parent pin of the Create MID node has a scalar parameter called BeamBrightness that will be manipulated in the EventGraph of this Blueprint.
There are six areas of the EventGraph that have been commented to give a general overview of how execution and data flows through the graph.
- Execution begins when a player overlaps the PowerVolume Static Mesh, in the Player Enters the PowerUp block
- The pawn's size is checked in the Is the Actor Already Powered Up? block to prevent errors from a player running through multiple power up instances.
- If the player is not currently powered up, execution proceeds to the Timeline block.
- As the Timeline plays and updates:
- The Interpolate and Set the Brightness and Size block executes.
- After that, the Update the Pawn Size (Always) and the PowerUp Brightness (if Powering Up) block executes.
- Once the Timeline finishes playing:
- Execution moves to the Toggle the Value of PoweredUp block.
- This may send execution to the Power Down After a Delay block depending on flow control nodes.
The networks within each block are explained in detail below.
Player Enters the PowerUp
The collision channel for the PowerVolume Static Mesh is set to OverlapOnlyPawn, so an OnComponentBeginOverlap event node can be used to start execution of the Blueprint's graph network. This node is a delegate or bound event, which means that it waits for the Component it is bound to to report that an Actor or Component is overlapping it, and then fires.
- Execution begins after the pawn overlaps PowerVolume, and the Actor that overlaps the Static Mesh is saved to a variable.
- Execution moves to the Is the Actor Already Powered Up? block to set the value of PoweredUp.
- The Branch node checks the value of the PoweredUp Boolean variable, which should be true if the pawn is scaled up, and false otherwise.
- If true, no other nodes are executed. This prevents the player from triggering the power up many times in rapid succession by running into and out of the PowerVolume; a new power up cannot be triggered until the old one has worn off.
- If false, execution proceeds to the Play input execution pin of the Timeline node.
Is the Actor Already Powered Up?
The Set PoweredUp node executes soon after a pawn overlaps the PowerVolume.
This network checks the scale of the OverlappingActor.
- If the scale is not equal to (1,1,1), the pawn's scale has already been increased, so PoweredUp should be set to true.
- Otherwise, PoweredUp should be set to false.
The Timeline node contains the tracks for changing the brightness of the PowerUpMaterial and for increasing the scale of the pawn after it overlaps the Static Mesh. Timelines are similar to Matinee in that they both allow you to change values over time. Timelines have the benefit of being accessible in Class Blueprints, and the time-dependent values generated by Timelines can be used in more applications than those from Matinee.
Both tracks in this Timeline are float tracks, and the data output pins from the Timeline node are connected to Lerp nodes elsewehere in the Blueprint.
- The shape of the BrightnessTrack curve was set to spike the brightness of the PowerUpMaterial three times in quick succession after the Timeline begins playing.
- The shape of the SizeTrack curve was set to slowly increase the scale of the pawn when the Timeline plays forward (and decrease the scale when reversed).
Each tick during gameplay causes execution to flow from the Update execution pin, and when the Timeline is finished playing, execution flows from the Finished execution pin.
Interpolate and Set the Brightness and Size
A Lerp node takes in data inputs for A, B, and Alpha. A is the starting value and B is the end value; the node linearly interpolates between A and B such that the value is 100% A when Alpha = 0 and 100% B when Alpha = 1.
- The Alpha data for the float Lerp node comes from the BrightnessTrack output data pin of the Timeline node, and the result of the linear interpolation is saved to the variable Brightness.
- The Alpha data for the Lerp (Vector) node comes from the SizeTrack output data pin of the Timeline node, and the result of the linear interpolation is saved to the variable Size.
Execution enters this block whenever the Timeline node updates, so the Brightness and Size variables are updated on every tick. After the Size variable is set, execution proceeds to the block which scales the pawn and causes the PowerVolume to flash.
Update the Pawn Size and PowerUp Brightness
After the execution flow from the Update output of the Timeline node proceeds through the Blueprint and sets the Brightness and Size variables, execution enters this region of the Blueprint graph. This region of the Blueprint graph will execute whether the Timeline is playing forward or backward, since both directions will cause the Update execution pin to fire.
- The Set Actor Relative Scale 3D changes the scale of the OverlappingActor to the current linearly interpolated value of Size.
- The Timeline playing forward will increase the character's size (to represent powering up).
- Playing backward will decrease the character's size back to 1:1 scale (to represent powering down).
- If the character is not powered up yet, the BeamBrightness of the MID created in the ConstructionScript will be changed using Set Scalar Parameter Value which takes the Brightness variable as input.
Toggle the Value of PoweredUp
This block of the Blueprint again uses a Branch Flow Control node which checks the value of the PoweredUp Boolean variable. This Branch node executes whenever the Timeline node is Finished, so it is executed twice for each power up event.
- The first time is after the Timeline plays forward, so PoweredUp is still false and needs to be set to true so the Blueprint knows the character is currently powered up.
- The second time is after the Timeline plays in reverse, so PoweredUp is true and needs to be reset to false since the effect has been removed.
Power Down After a Delay
This section of the Blueprint graph executes after the Timeline finishes playing forwards, so PoweredUp is false.
- The Delay node keeps the execution output from firing until the time set on the Delay pin has elapsed.
- After the delay has elapsed, the Reverse node plays PowerUpTimeline in reverse. This node is functionally the same as having an execution wire connected to the Reverse input execution pin of the PowerUpTimeline timeline node. That is, execution returns to the PowerUpTimeline timeline node, and execution proceeds from that node's Update and Finished pins.