1. 程式人生 > 其它 >UE4 TSubclassOfO生成物體和定時生成

UE4 TSubclassOfO生成物體和定時生成

基於FloatingActor

(類模板)

TSubclassOf<AClass>name;

SpwanActor.h中

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "floatActor.h"
#include "SpwanActor.generated.h"

UCLASS()
class NEWFPS_API ASpwanActor : public
AActor { GENERATED_BODY() public: // Sets default values for this actor's properties ASpwanActor(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override
; //(類模板)指定生成一個類:生成一個物體  UPROPERTY(EditDefaultsOnly,Category="Projectile")
TSubclassOf<AfloatActor>ProjectileActor;
  float BaseTime;
};

SpwanActor.cpp中

// Fill out your copyright notice in the Description page of Project Settings.


#include "SpwanActor.h"

// Sets default values
ASpwanActor::ASpwanActor()
{
     
// 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; } // Called when the game starts or when spawned void ASpwanActor::BeginPlay() { Super::BeginPlay(); } // Called every frame void ASpwanActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); float IntervalTime = 6.0f;//間隔時間 //DeltaTime每一幀消耗 1/幀數 的時間 BaseTime -= DeltaTime; //basetime<=0 先生成一個物體,basetime變成6.0f 隨著時間的變化 basetime又<=0依次重複 if (BaseTime <= 0.0f) { BaseTime += IntervalTime; //在世界裡面生成,則需要獲得level UWorld* const World = GetWorld(); if (World) { FVector SpawnActorLocation = GetActorLocation(); FRotator SpwanActorRotation = GetActorRotation(); //宣告一個變數 FActorSpawnParameters ActorParmas; //指定生成的方式 /* /Fall back to default settings. Undefined UMETA(DisplayName = "Default"), /Actor will spawn in desired location, regardless of collisions. AlwaysSpawn UMETA(DisplayName = "Always Spawn, Ignore Collisions"), / Actor will try to find a nearby non-colliding location (based on shape components), but will always spawn even if one cannot be found. 忽略碰撞一直生成 AdjustIfPossibleButAlwaysSpawn UMETA(DisplayName = "Try To Adjust Location, But Always Spawn"), / Actor will try to find a nearby non-colliding location (based on shape components), but will NOT spawn unless one is found. AdjustIfPossibleButDontSpawnIfColliding UMETA(DisplayName = "Try To Adjust Location, Don't Spawn If Still Colliding"), / Actor will fail to spawn. 如果產生碰撞則不產生 DontSpawnIfColliding UMETA(DisplayName = "Do Not Spawn"), */ ActorParmas.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::DontSpawnIfColliding; //在世界中生成物體 //名稱,位置,選擇,指定引數 World->SpawnActor<AfloatActor>(ProjectileActor, SpawnActorLocation, SpwanActorRotation, ActorParmas); } } }

建立藍圖並分配一個類