Thanks but still getting this error:
error C2664: 'T *UWorld::SpawnActor<APickUp>(UClass *,const FVector &,const FRotator &,const FActorSpawnParameters &)' : cannot convert argument 1 from 'APickUp *' to 'UClass *'
my Header:
#pragma once
#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"
UCLASS()
class TUTORIALCODE_API ASpawnVolume : public AActor
{
//defaults to private
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnVolume(const FObjectInitializer& ObjectInitializer);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
void EnableSpawning();
void DisableSpawning();
UPROPERTY(VisibleInstanceOnly, Category = Spawning)
class UBoxComponent* WhereToSpawn;
/** The pickup to spawn. */
UPROPERTY(EditAnywhere, Category = Spawning)
class APickUp * WhatToSpawn;
//Min Spawn Delay
UPROPERTY(EditAnywhere, BluePrintReadWrite, Category = Spawning)
float SpawnDelayRangeLow;
//Max Spawn Delay
UPROPERTY(EditAnywhere, BluePrintReadWrite, Category = Spawning)
float SpawnDelayRangeHigh;
/** Finds a random point within the BoxComponent */
UFUNCTION(BlueprintPure, Category = Spawning)
FVector GetRandomPointInVolume();
private:
/** Whether or not spawning is enabled. */
bool bSpawningEnabled;
/** Calculates a random spawn delay */
float GetRandomSpawnDelay();
/** The current spawn delay */
float SpawnDelay;
//Handles the spawning of a new pickup
void SpawnPickup();
/** The timer for when to spawn the pickup */
float SpawnTime;
};
My CPP:
#include "TutorialCode.h"
#include "SpawnVolume.h"
#include "PickUp.h"
// Sets default values
ASpawnVolume::ASpawnVolume(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// 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;
//create the simple staticmeshcomponent to represent the pickup in the level
WhereToSpawn = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("WhereToSpawn"));
//Set the staticMeshComponent as the root component
RootComponent = WhereToSpawn;
//set the spawn delayt range and get the first spawnDelay
SpawnDelayRangeLow = 1.0f;
SpawnDelayRangeLow = 4.5f;
SpawnDelay = GetRandomSpawnDelay();
}
// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASpawnVolume::Tick( float DeltaTime )
{
if (!bSpawningEnabled)
return;
Super::Tick( DeltaTime );
//Always add deltatime to out sawn time
SpawnTime += DeltaTime;
bool bShouldSpawn = (SpawnTime > SpawnDelay);
if (bShouldSpawn)
SpawnPickup();
//Deduct spawn delay from accumulated time value
SpawnTime -= SpawnDelay;
SpawnDelay = GetRandomSpawnDelay();
}
void ASpawnVolume::SpawnPickup()
{
// If we have set something to spawn:
if (WhatToSpawn != NULL)
{
// Check for a valid World:
UWorld* const World = GetWorld();
if (World)
{
// Set the spawn parameters
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// Get a random location to spawn at
FVector SpawnLocation = GetRandomPointInVolume();
// Get a random rotation for the spawned item
FRotator SpawnRotation;
SpawnRotation.Yaw = FMath::FRand() * 360.f;
SpawnRotation.Pitch = FMath::FRand() * 360.f;
SpawnRotation.Roll = FMath::FRand() * 360.f;
// spawn the pickup
APickUp* const SpawnedPickup = World->SpawnActor<APickUp>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
}
}
}
FVector ASpawnVolume::GetRandomPointInVolume()
{
FVector RandomLocation;
float MinX, MinY, MinZ;
float MaxX, MaxY, MaxZ;
FVector Origin;
FVector BoxExtent;
// Get the SpawnVolume's origin and box extent
Origin = WhereToSpawn->Bounds.Origin;
BoxExtent = WhereToSpawn->Bounds.BoxExtent;
// Calculate the minimum X, Y, and Z
MinX = Origin.X - BoxExtent.X / 2.f;
MinY = Origin.Y - BoxExtent.Y / 2.f;
MinZ = Origin.Z - BoxExtent.Z / 2.f;
// Calculate the maximum X, Y, and Z
MaxX = Origin.X + BoxExtent.X / 2.f;
MaxY = Origin.Y + BoxExtent.Y / 2.f;
MaxZ = Origin.Z + BoxExtent.Z / 2.f;
// The random spawn location will fall between the min and max X, Y, and Z
RandomLocation.X = FMath::FRandRange(MinX, MaxX);
RandomLocation.Y = FMath::FRandRange(MinY, MaxY);
RandomLocation.Z = FMath::FRandRange(MinZ, MaxZ);
// Return the random spawn location
return RandomLocation;
}
float ASpawnVolume::GetRandomSpawnDelay()
{
// Get a random float that falls within the spawn delay range
return FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
}
void ASpawnVolume::EnableSpawning()
{
bSpawningEnabled = true;
}
void ASpawnVolume::DisableSpawning()
{
bSpawningEnabled = false;
}
is it the way I’ve set up the class declaration like this??
UPROPERTY(EditAnywhere, Category = Spawning)
class APickUp * WhatToSpawn;
thanks