Battery Collector #12 – Powering Up the Character

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

Powering Up the Character

In the BatteryCollector.cpp file, I am including the BatteryPickup.h file. In the CollectPickups() function, I am adding a CollectedPower float variable and I am modifying the loop, to test if the pickup is also a battery.

void ABatteryCollectorCharacter::CollectPickups()
{
	// Get all overlapping Actors and store then in an array
	TArray<AActor*> CollectedActors;
	CollectionSphere->GetOverlappingActors(CollectedActors);

	// keep track of the collected battery power
	float CollectedPower = 0;

	// For each Actor we collected
	for (int32 iCollected = 0; iCollected < CollectedActors.Num(); ++iCollected)
	{
		// Cast the Actor to APickup
		APickup* const TestPickup = Cast(CollectedActors[iCollected]);
		
		// If the cast is successful and the pickup is valid and active
		if (TestPickup && !TestPickup->IsPendingKill() && TestPickup->IsActive())
		{
			// call the pickup's WasCollected function
			TestPickup->WasCollected();
			// Check to see if the pickup is also a battery
			ABatteryPickup* const TestBattery = Cast(TestPickup);
			if (TestBattery)
			{
				// increase the collected power
				CollectedPower += TestBattery->GetPower();
			}
			// Deactivate the pickup
			TestPickup->SetActive(false);
		}
	}
	if (CollectedPower > 0)
	{
		UpdatePower(CollectedPower);
	}
}

Back to the Unreal Engine Editor, I am compiling the project. Then when I hit play and select the 3rdPersonCharacter, I can see the power increasing in the detail window:

PlayPower

Next Step

In the next step, we will power down the character.

About

Gamer

Tagged with:

Leave a Reply