Draining stamina while sprinting

To add a constraint to the sprinting ability of the player, we'll need to return to the Blueprint where we originally defined the ability. Open the FirstPersonCharacter Blueprint located in the Blueprints folder of Content Browser.

First, we need to create a couple of variables that will keep track of whether or not the player is sprinting and how much stamina sprinting should cost. Find the Variables category of the My Blueprint panel and click the + button twice to add two new variables. Rename the first variable to Sprint Cost and assign it the Float type. Make sure to click the checkbox next to Instance Editable as True and set the variable's default value to 0.1. Rename the second variable IsSprinting. Set the variable type to Boolean and make the variable editable. After compiling the Blueprint, find some empty graph space near the block of Blueprint nodes you created for the sprint Function, which should have a comment block labeled Sprint around it.

We are going to create a custom Event to drain the player's stamina at a consistent rate while they are sprinting. A custom Event allows us to trigger the Blueprint nodes that are attached to the Event whenever another Blueprint node calls that Event. In this way, groups of Blueprint nodes within the same Blueprint can communicate with one another, even when they are not connected directly:

Search for a custom event node and add it, putting it a moderate distance away from your other sprint nodes. When you click on the Add Custom Event... node, look at the Details panel and find the Name field. This allows you to create the name of your custom Function. The name you give to the custom Event is important as you are establishing the name by which the Function will be referenced by the Function calls that trigger it. In this case, let's call the Function Sprint Drain. Type Sprint Drain into the Name field and press the Enter key to establish the Event. The Blueprint structure we'll follow for this sequence looks like this:

First, drag a wire from the Sprint Drain Event onto empty grid space and add a SET Player Stamina node. Next, attach the Player Stamina input pin to a MAX (Float) node. This node will output the highest of the float numbers given as inputs. We want to ensure that Player Stamina never dips below zero, so leave the bottom pin at 0.0 and drag a wire from the top input pin of this node and attach it to a Float – Float node. Attach a GET Player Stamina node to the top input pin of the Float – Float node. In the bottom pin, we'll establish the amount of stamina that is drained while sprinting.

We could enter a number into the field next to the bottom input pin of this node. However, if we ever wished to change the amount of stamina drained by sprinting, then we would need to open this Blueprint, find this node, and adjust the value within this small textbox each time. A better habit to get into is to use a custom, public variable that we attach to this pin, which will allow us to tweak the amount of stamina drain incurred by sprinting continually, without even entering the Blueprint Editor interface. Because we already created variables for both sprint cost and checking whether the player is sprinting, we'll use the Sprint Cost variable here. Either drag the Sprint Cost variable onto the bottom input pin of Float - Float, or drag a wire from the bottom pin out and search for GET Sprint Cost.

Next, we want to stop both the sprint and the stamina drain effect when the player runs out of stamina. Drag a wire from the output execution pin of the SET Player Stamina node and attach it to a Branch node. Now, drag a wire from the Condition input pin of this node and attach it to a Float >= Float node. Drag a second wire from the GET Player Stamina node onto the top input pin of the Float >= Float node and drag a second wire from the GET Sprint Cost node to the bottom input pin. This will determine whether or not the player has enough stamina to continue sprinting.

When the player does not have enough stamina to take another tick of stamina-draining sprinting, we need to force the player back to walking speed and clear the timer that will be calling this custom Function. Do so by dragging the Character Movement Component down from the Components panel and drop it near the Branch node. Drag a wire from this node and attach it to a SET Max Walk Speed node. Set the Max Walk Speed field to 600 to match the default walk speed we established. Now, connect the input execution pin of this node to the False output execution pin of the Branch node.

Next, drag a wire from the SET Max Walk Speed node's output execution pin and attach it to a Clear Timer by Function Name node. Type in Sprint Drain precisely into the Function Name input field to link it to the custom Event. The Object input indicates the object that implements the timer. You do not need to change it because its default value is self, which means it is the current Blueprint. Finally, attach a SET Is Sprinting node to the output execution pin of Clear Timer by Function Name, ensuring that the checkbox is left unchecked.

Now, select all five nodes and create a comment around them explaining their utility for draining the player's stamina. I chose Sprinting Drains Stamina by Sprint Cost. The next step will be to call our new custom Event from inside the Blueprint nodes that manage our sprint. Remember to compile and save the Blueprint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset