Using looping timers to repeat Actions

Now, we want to customize our sprint Blueprint nodes to fire the custom Event that we just created so that the player's stamina drains as they sprint, as seen in the following screenshot:

From the output execution pin of the node setting walk speed to 2200.0, drag a wire onto empty space and add a SET Is Sprinting node. Check the checkbox inside this node to set the Boolean value to True when the player is sprinting. Next, we want to make sure that stamina is continually drained as long as the left Shift key is held down. To do so, we can utilize a timer, as shown in the following screenshot:

Timers allow us to perform an Action after a designated amount of time has expired or even fire an Action repeatedly on a set-time interval. This second functionality is going to serve the purpose of looping our Sprint Drain Function repeatedly.

Drag a wire from the execution output pin of the SET Is Sprinting node onto empty graph space and add a SET Timer by Function Name node. Inside this node, click on the field underneath the Function Name label, and type in Sprint Drain to connect the timer to our custom Event.

The second input we want to adjust in the timer node is labeled Time, which will determine the interval in seconds at which our sprint draining Event is triggered. Put 0.5 into the Time field to give the draining effect a notable and steady default rate. If you expect that you might want to tweak this value repeatedly, then you could also choose to create a custom float variable and attach that to this input instead, in a way similar to how we handled Sprint Cost. Finally, drag a wire from the red output pin of the SET Is Sprinting node and attach it to the Looping input pin to ensure that Sprint Drain is called repeatedly at each time interval as long as the Is Sprinting Boolean is set to True.

With this completed, this Function will now ensure that the value we designated for the Sprint Cost variable will be drained from the player stamina meter every 0.5 seconds, starting when the left Shift key is pressed as long as the player is sprinting. However, we want the drain effect to stop when the player stops sprinting by letting go of the key. To accomplish this, we need to stop the timer using Clear Timer by Function Name, just as we did inside the Sprint Drain Function:

Attach a Clear Timer by Function Name node to the output execution node of the SET Max Walk Speed node, which is attached to the Left Shift node's Released output pin. This node will abort out of the timer attached to the Function name given. Type Sprint Drain into the Function Name field of this node to link the node to the timer node we just made. End this sequence by attaching a SET Is Sprinting node to the output execution pin of Clear Timer by Function Name and leave the checkbox unchecked.

As an alternative to Clear Timer by Function Name, you could use the Pause Timer by Function Name node. Pausing the timer works almost identically, except that the remaining time on the timer countdown before it was paused would persist when the timer is activated again. So, if you paused a 10-second timer that had 5 seconds left until the next activation, then the attached Function would trigger after 5 seconds instead of 10 at the next activation.

Compile, save, and test the game. As you sprint around the Level, you should see your stamina meter deplete in regular increments while the left Shift key is held down. The next step to constrain sprinting is to ensure that the player cannot initiate a sprint if they are drained of stamina.

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

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