08 - Setting Timers for Spawning

創(chuàng)建定時(shí)器來定時(shí)生成電池到場(chǎng)景中
最終 SpawnVolume.h
// Fill out your copyright notice in the Description page of Project Settings.

pragma once

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();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

FORCEINLINE UBoxComponent* GetWhereToSpawn()const { return WhereToSpawn; }
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector GetRandomPointInVolume();

protected:
UPROPERTY(EditAnyWhere, Category = "Spawning")
TSubclassOf<class APickUp> WhatToSpawn;
FTimerHandle SpawnTimer; //定時(shí)器
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;

};
最終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;
WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn"));
RootComponent = WhereToSpawn;
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, SpawnDelay, false); // 添加定時(shí)器 回調(diào)函數(shù)為SpawnPickup 時(shí)間間隔為SpawnDelay 是否循環(huán)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 (WhatToSpawn != NULL)
{
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;

        FVector SpawnLocation = GetRandomPointInVolume();
        FRotator SpawnRotation;
        SpawnRotation.Yaw = FMath::FRand()*360.0f;
        SpawnRotation.Pitch = FMath::FRand()*360.0f;
        SpawnRotation.Roll = FMath::FRand()*360.0f;

        APickUp* const SpawnedPickup = World->SpawnActor<APickUp>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
        GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
    }
}

}

在UE4 Editor中 間SpawnVolume拖拽到場(chǎng)景中 調(diào)整大小位置及參數(shù) 運(yùn)行 電池會(huì)動(dòng)態(tài)生成

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容