Download

Battery Collector Tutorial: Stuck at GameMode Tick [Beginner]

Hello, I was trying out the Battery Collector Tutorial on the video tutorial page, and I couldn’t get it to work, specifically with the Character’s power decrease.
I’m guessing there is something wrong with the GameMode tick.

GameMode Header:


UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameModeBase
{
	GENERATED_BODY()

public:
	ABatteryCollectorGameMode();

	virtual void Tick(float DeltaTime) override;

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

GameMode Source:


ABatteryCollectorGameMode::ABatteryCollectorGameMode()
{
	// set default pawn class to our Blueprinted character
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
	if (PlayerPawnBPClass.Class != NULL)
	{
		DefaultPawnClass = PlayerPawnBPClass.Class;
	}

	UE_LOG(LogClass, Log, TEXT("GameMode Constructor"));

	/* The base decay rate */
	DecayRate = 0.01f;

	UE_LOG(LogClass, Log, TEXT("Decay Rate %f"), DecayRate);
}

void ABatteryCollectorGameMode::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	UE_LOG(LogClass, Log, TEXT("Tick"));

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

UE Output log, on play, prints out the expected output on the constructor, however, the tick doesn’t seem to do so.

UE Output log:
LogClass: GameMode Constructor
LogClass: Decay Rate 0.010000

Battery Collector Character Source - Update Power function:


void ABatteryCollectorCharacter::UpdatePower(float PowerChange)
{
	UE_LOG(LogClass, Log, TEXT("Character Update Power"));

	/* Change power */
	CharacterPower = CharacterPower + PowerChange;
}

UE Output Log:
LogClass: You have collected Battery_BP_C_2
LogClass: You have collected Battery_BP_C_0
LogClass: You have collected Battery_BP_C_1
LogClass: Character Update Power

Also, I left another UE_LOG output on the Character, so that whenever this function gets called it would print the specified message on the Output log as well.
It works when I collect the batteries, however, this function should be called on every Tick on the GameMode, but it doesn’t seem to work.

I just can’t seem to figure it out. Thanks for reading it up to here, and I want to thank in advance for any input and advice!

Hi,

the lock doesn’t show the “Tick” log message right?

I don’t know how the AGameMode (can’t take look into it right now) behave, but the documentation says this about tick

“Function called every frame on this Actor. Override this function to implement custom logic to be executed every frame. Note that Tick is disabled by default, and you will need to check PrimaryActorTick.bCanEverTick is set to true to enable it.”

Probably the “Tick” isn’t enabled on the GameMode?

Kind Regards

freakxnet

simply add the following line to game mode
right after the decayRate variable set

PrimaryActorTick.bCanEverTick = true;

recompile and it should work