UE4 TSubclassOfO生成物体和定时生成

基于FloatingActor

(类模板)

TSubclassOf<AClass>name;

创建一个 UClass 类型的 UPROPERTY,让设计者指定派生自 UDamageType 的类;而使用 TSubclassOf 模板强制要求此选择。

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

 

创建蓝图并分配一个类

 

posted @ 2022-02-13 16:56  ChioHiro  阅读(713)  评论(0)    收藏  举报