【UE数字孪生学习笔记】 Gameplay框架之TSubclassOf

声明:部分内容来自于b站,知乎,慕课,公开课等的课件,仅供学习使用。如有问题,请联系删除。

部分内容来自UE官方文档,博客等

TSubclassOf


  TSubclassOf 是一个模板类,用于存储对某个特定类的引用,通常用于指定类的子类。TSubclassOf能够约束下拉框中只会出现继承于T的类或者T本身,并且C++层面也能实现类型安全,如果给TSubclassOf对象赋值一个类型不兼容的UClass,则会得到编译错误。

    //类引用存储: 你可以将 TSubclassOf 用于存储对某个类(或其子类)的引用。
	TSubclassOf<AActor> MyActorClass;
	//类的实例化: 通过 TSubclassOf,你可以在运行时动态实例化特定类或其子类的对象。
	AActor* MyNewActor = GetWorld()->SpawnActor<AActor>(
        MyActorClass, 
        SpawnLocation, 
        SpawnRotation
    );

TSubclassOf 的应用场景和优势

  TSubclassOf 提供了在运行时动态引用并实例化类的便捷方法。它在UE中的使用场景非常广泛,尤其在处理各种动态生成和配置类对象的情况下非常有用。通过 TSubclassOf,开发者可以更灵活地设计和组织他们的代码,同时减少硬编码,提高代码的可维护性和可扩展性。

TSubclassOf 生成物体和定时生成

  基于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 "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<AActor>(ProjectileActor, SpawnActorLocation, SpwanActorRotation, ActorParmas);
    }
    }
}

参考资料
https://docs.unrealengine.com/latest/INT/
https://www.bilibili.com/video/BV1bC411j7Du
https://blog.csdn.net/ttod/article/details/136112588
https://zhuanlan.zhihu.com/p/670572209
https://www.cnblogs.com/lyj00/p/15889733.html

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值