Hi everyone
I am trying to write a class that will spawn a pawn and i failed.
I have a pawn blueprint, i want to choose that from editor without adding to level and make spawn volume spawn that pawn.
Well, this class spawns nothing. There is a problem with it.
Here is the header file of SpawnVolume
#pragma once
#include "GameFramework/Actor.h"
#include "PursuerPawn.h"
#include "SpawnVolume.generated.h"
UCLASS()
class SHOOTBOMBERS_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;
// Box component that defines spawn area
UPROPERTY(EditAnywhere, Category = Spawn)
UBoxComponent* SpawnVolume;
// Pawn to be spawned
UPROPERTY(EditAnywhere, Category = Spawn)
// Maybe i shouldnt choose UClass, but AActor doesnt give me what i want
class UClass* SpawnActor;
// Minimum time required to spawn, Needed for random spawn time
UPROPERTY(EditAnywhere, Category = Spawn)
float SpawnTimeRangeMin;
// Maximum time required to spawn, Needed for random spawn time
UPROPERTY(EditAnywhere, Category = Spawn)
float SpawnTimeRangeMax;
private:
// Gives random spawn location
FVector GetSpawnLocation() const;
// Gives random spawn rotation
FRotator GetSpawnRotation() const;
// To keep lines shorter, returns origin of box component
const FVector& GetBoxOrigin() const;
// To keep lines shorter, returns extent of box component
const FVector& GetBoxExtent() const;
// Returns random spawn time between min and max
float RandSpawnTime() const;
// Time passed since last spawn
float SpawnTimePassed;
// Random spawn time decided
float SpawnTimeRandom;
};
and source file
#include "ShootBombers.h"
#include "SpawnVolume.h"
#include "Kismet/GameplayStatics.h"
#include "PursuerPawn.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 = true;
// A dummy root component
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
// Box component that defines spawn area
SpawnVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnVolume"));
// Dont ask, name says it
SpawnVolume->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();
// Set default values
SpawnTimeRangeMin = 0.5f;
SpawnTimeRangeMax = 1.5f;
SpawnTimePassed = 0.f;
SpawnTimeRandom = RandSpawnTime();
}
// Called every frame
void ASpawnVolume::Tick( float DeltaTime )
{
// If you dont know what this line does, well...
Super::Tick( DeltaTime );
// If we have a SpawnActor and World
if (SpawnActor || GetWorld())
{
// Add delta time to passed time
SpawnTimePassed += DeltaTime;
// If enough time passed, spawn object
if (SpawnTimePassed >= SpawnTimeRandom)
{
// Substract random spawn time from passed time
SpawnTimePassed -= SpawnTimeRandom;
SpawnTimeRandom = RandSpawnTime();
FActorSpawnParameters SpawnParameter;
SpawnParameter.Owner = this;
SpawnParameter.Instigator = Instigator;
// Spawn the actor with random location and rotation
AActor* const SpawnedActor = GetWorld()->SpawnActor<AActor>(SpawnActor->StaticClass(), GetSpawnLocation(), GetSpawnRotation(), SpawnParameter);
}
}
}
FVector ASpawnVolume::GetSpawnLocation() const
{
// Gets a random location inside Box component
FVector RandomPoint;
RandomPoint.X = FMath::FRandRange(GetBoxOrigin().X - GetBoxExtent().X / 2.f, GetBoxOrigin().X + GetBoxExtent().X / 2.f);
RandomPoint.Y = FMath::FRandRange(GetBoxOrigin().Y - GetBoxExtent().Y / 2.f, GetBoxOrigin().Y + GetBoxExtent().Y / 2.f);
RandomPoint.Z = FMath::FRandRange(GetBoxOrigin().Z - GetBoxExtent().Z / 2.f, GetBoxOrigin().Z + GetBoxExtent().Z / 2.f);
return RandomPoint;
}
FRotator ASpawnVolume::GetSpawnRotation() const
{
// Gets a random rotation
FRotator RandomRotation;
RandomRotation.Pitch = FMath::FRand();
RandomRotation.Yaw = FMath::FRand();
RandomRotation.Roll = 0.f;
return RandomRotation;
}
const FVector& ASpawnVolume::GetBoxOrigin() const
{
// To keep lines short, returns origin of box component
return SpawnVolume->Bounds.Origin;
}
const FVector& ASpawnVolume::GetBoxExtent() const
{
// To keep lines short, returns extent of box component
return SpawnVolume->Bounds.BoxExtent;
}
float ASpawnVolume::RandSpawnTime() const
{
// Returns a random spawn time between Min and Max
return FMath::FRandRange(SpawnTimeRangeMin, SpawnTimeRangeMax);
}