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