TSubclassOf的一些说明

注:补充下SpawnActor的用法
TSubclassOf<AActor> TS = LoadClass<AActor>(NULL, TEXT("Blueprint'/Game/bp/Map/TMapActorBP.TMapActorBP_C'"));
GetWorld()->SpawnActor<AActor>(TS);
TS就是个类类型
SpawnActor<A>(B);
B位置可以写子类,然后位置写父类,创建完毕会创建一个B类型,然后强制转成父类型A
template< class T >
T* SpawnActor( UClass* Class, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
{
	return CastChecked<T>(SpawnActor(Class, NULL, NULL, SpawnParameters),ECastCheckedType::NullAllowed);
}







  1. https://answers.unrealengine.com/questions/462096/why-use-tsubclassof-and-not-just-the-class-itself.html
  2. UDamageType* dmgType;
  3. vs
  4.  
  5. UClass* DamageType;
  6. vs
  7. TSubclassOf<UDamageType> DamageType;
  8.  
  9.  

the first one is a blue pin object reference, which is not a class reference at all, its a reference to an object that is already created, or it is null. the second one is a purple pin Class reference, used to create new objects from, but in the details panel it would create a drop down menu that allows you to select any class.

the third one is TSubclassOf, which is like the second one, where it is a purple pin Class variable used to create new objects from, but the editor's details panel will list only classes derived from UDamageType as choices for the property, instead of listing every class in the engine as an available replacement for that pointer.

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/TSubclassOf/index.html

so the first one is an object pointer, and the last 2 are class pointers, and the final one is a specific type of class pointer than narrows down the list of available class choices.

class pointers are different from object pointers, because UClass pointers reference class assets available in the editor, while object pointers can only be valid at runtime, because they can only reference objects that are already created or spawned into ram during gameplay.


so if you were making a weapon pickup item, where you wanted the designer to be able to choose the damage type for that weapon from the details panel, you would use

  1. TSubclassOf<UDamageType> DamageTypeClass;
  2.  

then on begin play, you can use

  1. UDamageType* dmgType = NewObject<UDamageType>(DamageTypeClass);
  2.  

that will give you a pointer to a created object of a type decided in the editor by a designer.

转载于:https://www.cnblogs.com/nafio/p/9137058.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值