
我们在这里设置要重载的子类:UBaseCharacterMovementComp
其实这个函数(SetDefaultSubobjectClass)就是把UBaseCharacterMovementComp对应的UClass放入ComponentOverrides中
template<class T>
FObjectInitializer const& SetDefaultSubobjectClass(FName SubobjectName) const
{
AssertIfSubobjectSetupIsNotAllowed(*SubobjectName.GetPlainNameString());
ComponentOverrides.Add(SubobjectName, T::StaticClass(), *this);
return *this;
}
在构造函数中调用CreateDefaultSubobject时候, 会自动触发函数重载.

从ComponentOverrides取出UBaseCharacterMovementComp, 然后创建:

调用堆栈:

这里还要补充一句:
FUObjectThreadContext维护了一个全局堆栈, 用于保存当前的FObjectInitializer

使用的时候直接取栈顶的FObjectInitializer
UObject* UObject::CreateDefaultSubobject(FName SubobjectFName, UClass* ReturnType, UClass* ClassToCreateByDefault, bool bIsRequired, bool bAbstract, bool bIsTransient)
{
FObjectInitializer* CurrentInitializer = FUObjectThreadContext::Get().TopInitializer();
UE_CLOG(!CurrentInitializer, LogObj, Fatal, TEXT("No object initializer found during construction."));
UE_CLOG(CurrentInitializer->Obj != this, LogObj, Fatal, TEXT("Using incorrect object initializer."));
return CurrentInitializer->CreateDefaultSubobject(this, SubobjectFName, ReturnType, ClassToCreateByDefault, bIsRequired, bAbstract, bIsTransient);
}
网友评论