美文网首页
08 - Setting Timers for Spawning

08 - Setting Timers for Spawning

作者: 镜月s | 来源:发表于2017-09-18 11:53 被阅读8次

    创建定时器来定时生成电池到场景中
    最终 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; //定时器
    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); // 添加定时器 回调函数为SpawnPickup 时间间隔为SpawnDelay 是否循环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拖拽到场景中 调整大小位置及参数 运行 电池会动态生成

    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:08 - Setting Timers for Spawning

          本文链接:https://www.haomeiwen.com/subject/wlovsxtx.html