Battery Collector #19 – Toggling the Spawn Volumes

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

Toggling the Spawn Volumes

In the SpawnVolume.h file, I am declaring a new SetSpawningActive function:

	/** This function toggles wether or not the spawn volume spanws pickups */
	UFUNCTION(BlueprintCallable, Category = "Spawning")
	void SetSpawningActive(bool bShouldSpawn);

In the SpawnVolume.cpp file, I am defining the function.

void ASpawnVolume::SetSpawningActive(bool bShouldSpawn)
{
	if (bShouldSpawn)
	{
		// Set the timer on Spawn Pickup
		SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
		GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
	}
	else
	{
		// Clear the timer on Spawn Pickup
		GetWorldTimerManager().ClearTimer(SpawnTimer);
	}
}

I am removing the SetTimer in the BeginPlay function.
In the BatteryCollectorGameMode.h file, I am declaring a private SpawnVolumeActors variable:

	TArray SpawnVolumeActors;

In BatteryCollectorGameMode.cpp, I am including SpawnVolume.h:

#include "SpawnVolume.h"

At the beginning of BeginPlay, I am adding code to find all spawn volume actors:

	// find all spawn volume Actors
	TArray FoundActors;
	UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnVolume::StaticClass(), FoundActors);

	for (auto Actor : FoundActors)
	{
		ASpawnVolume* SpawnVolumeActor = Cast(Actor);
		if (SpawnVolumeActor)
		{
			SpawnVolumeActors.AddUnique(SpawnVolumeActor);
		}
	}

Note that it must be done before we set the current state.

Next Step

Next, I will handle new play states.

About

Gamer

Tagged with:

Leave a Reply