What I’m trying to do is spawn 4 different pickups/powerups at random but I don’t know how to get the system/functions working.
Here is the code:
SpawnPickup.h (1.4 KB)
SpawnPickup.cpp (2.3 KB)
Pickup.h (1.1 KB)
Pickup.cpp (2.4 KB)
Writing it here as well just in case:
SpawnPickup.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnPickup.generated.h"
UCLASS()
class SPAWNER_API ASpawnPickup : public AActor
{
GENERATED_BODY()
//ADD this
FORCEINLINE class UBoxComponent* GetWhereToSpawn() const { return WhereToSpawn; }
public:
// Sets default values for this actor's properties
ASpawnPickup();
//Add this
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector GetRandomPointInVolume();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//Reference of actor to spawn
UPROPERTY(EditAnywhere, Category = "Spawning")
TSubclassOf<class APickup> WhatToSpawn;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Add everything after this
FTimerHandle SpawnTimer;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeLow;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeHigh;
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
class UBoxComponent* WhereToSpawn;
void SpawnPickUp();
float SpawnDelay;
float RandomSpawnTimeOffset = 1.0f;
};
SpawnPickup.cpp
#include "SpawnPickup.h"
#include "Kismet/KismetMathLibrary.h"
#include "Pickup.h"
#include "TimerManager.h"
#include "Engine/World.h"
#include "Components/BoxComponent.h"
// Sets default values
ASpawnPickup::ASpawnPickup()
{
// 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;
WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn"));
RootComponent = WhereToSpawn;
SpawnDelayRangeHigh = 1.0f;
SpawnDelayRangeLow = 1.0f;
}
FVector ASpawnPickup::GetRandomPointInVolume()
{
//Add everything in this function
FVector SpawnOrigin = WhereToSpawn->Bounds.Origin;
FVector SpawnExtent = WhereToSpawn->Bounds.BoxExtent;
return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);
}
// Called when the game starts or when spawned
void ASpawnPickup::BeginPlay()
{
Super::BeginPlay();
//Add here
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnPickup::SpawnPickUp, SpawnDelay, false);
}
// Called every frame
void ASpawnPickup::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ASpawnPickup::SpawnPickUp()
{
if (WhatToSpawn != NULL)
{
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
//Gets a reference of the location inside the box extent to spawn
FVector SpawnLocation = GetRandomPointInVolume();
//Sets rotation --> Can be set to 0 if you dont want items to rotate
FRotator SpawnRotation;
SpawnRotation.Yaw = FMath::FRand() * 360;
SpawnRotation.Pitch = FMath::FRand() * 360;
SpawnRotation.Roll = FMath::FRand() * 360;
ASpawnPickup* const SpawnerPickUp = World->SpawnActor<ASpawnPickup>
(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
//Sets a new delay time for next spawn
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
//Loops the functions by calling itself
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnPickup::SpawnPickUp, SpawnDelay, false);
}
}
}