本文主要是介绍【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 "floatActor.h"
#include "SpwanActor.generated.h"UCLASS()
class NEWFPS_API ASpwanActor : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesASpwanActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual 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;//在世界里面生成,则需要获得levelUWorld* 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);}}
}
参考资料
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
这篇关于【UE数字孪生学习笔记】 Gameplay框架之TSubclassOf的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!