I completed all of the code from the tutorial up to the end of video 8. After that, I decided to compile and test it out myself. I set the spawn volumes to the correct classes but nothing ends up spawning. This is the code from my spawn volume class,
SpawnVolume.h
#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"
UCLASS()
class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnVolume();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
//returns the wheretospawn subobject
FORCEINLINE class UBoxComponent* GetWhereToSpawn() const { return WhereToSpawn; }
//Find random point in spawn volume
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector GetRandomPointInVolume();
protected:
//pickup to spawn
UPROPERTY(EditAnywhere, Category = "Spawning")
TSubclassOf<class APickup> WhatToSpawn;
FTimerHandle SpawnTimer;
//min spawn delay
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeLow;
//max spawn delay
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeHigh;
private:
//Box Component to specify where pickups should be spawned
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
class UBoxComponent* WhereToSpawn;
//handle spawning a new pickup
void SpawnPickup();
//current spawn delay
float SpawnDelay;
};
SpawnVolume.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BatteryCollector.h"
#include "SpawnVolume.h"
#include "Kismet/KismetMathLibrary.h"
#include "Pickup.h"
// Sets default values
ASpawnVolume::ASpawnVolume()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
//Create Box Component to represent spawn Volume
WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn"));
RootComponent = WhereToSpawn;
//set spawn delay range
SpawnDelayRangeLow = 1.0f;
SpawnDelayRangeHigh = 4.5f;
}
// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, false);
}
// Called every frame
void ASpawnVolume::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
FVector ASpawnVolume::GetRandomPointInVolume() {
FVector SpawnOrigin = WhereToSpawn->Bounds.Origin;
FVector SpawnExtent = WhereToSpawn->Bounds.BoxExtent;
return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);
}
void ASpawnVolume::SpawnPickup() {
//if we have something to spawn
if (WhatToSpawn != NULL) {
//check for valid world
UWorld* const World = GetWorld();
if (World) {
//set spawn parameters
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
//get random location to spawn at
FVector SpawnLocation = GetRandomPointInVolume();
//get a random rotation for spawned item
FRotator SpawnRotation;
SpawnRotation.Yaw = FMath::FRand() *360.0f;
SpawnRotation.Pitch = FMath::FRand() *360.0f;
SpawnRotation.Roll = FMath::FRand() *360.0f;
//spawn pickup
APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, false);
}
}
}