Battery Collector #13 – Powering Down the Character

I am continuing the Battery Collector tutorial. This is the 13th video. Click here for the first part and here for the previous one.

Powering Down the Character

In the BattercyCollectorGameMode.h file, we are going to create a decay rate.

protected:
	/** The rate at which the character loses power */
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
	float DecayRate;

In the BatteryCollectorGameMode.cpp, we are going to define a default value in the constructor:

// base decay rate
DecayRate = 0.01f;

We will use this property by overriding the Tick function. In the public section of BatteryCollectorGameMode.h we declare the function:

virtual void Tick(float DeltaTime) override;

And in the BatteryCollectorGameMode.cpp, we define the function:

void ABatteryCollectorGameMode::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	// Check that we are using the battery collector character
	ABatteryCollectorCharacter* MyCharacter = Cast(UGameplayStatics::GetPlayerPawn(this, 0));
	if (MyCharacter)
	{
		// if the character's power is positive
		if (MyCharacter->GetCurrentPower() > 0)
		{
			// decrease the chracter's power using the decay rate
			MyCharacter->UpdatePower(-DeltaTime*DecayRate*(MyCharacter->GetInitialPower());
		}
	}
}

We need to include UGameplayStatics from kismet:

#include "Kismet/GameplayStatics.h"

In the Unreal Engine Editor, I am compiling the project. When playing and showing the power of the character, we can see that it is decreasing overtime.
PlayDecreasePower

Next Step

Next, we will be changing the character’s speed and material

About

Gamer

Tagged with:

Leave a Reply