我們?cè)谶@里設(shè)置要重載的子類:UBaseCharacterMovementComp
其實(shí)這個(gè)函數(shù)(SetDefaultSubobjectClass)就是把UBaseCharacterMovementComp對(duì)應(yīng)的UClass放入ComponentOverrides中
template<class T>
FObjectInitializer const& SetDefaultSubobjectClass(FName SubobjectName) const
{
AssertIfSubobjectSetupIsNotAllowed(*SubobjectName.GetPlainNameString());
ComponentOverrides.Add(SubobjectName, T::StaticClass(), *this);
return *this;
}
在構(gòu)造函數(shù)中調(diào)用CreateDefaultSubobject時(shí)候, 會(huì)自動(dòng)觸發(fā)函數(shù)重載.
從ComponentOverrides取出UBaseCharacterMovementComp, 然后創(chuàng)建:
調(diào)用堆棧:
這里還要補(bǔ)充一句:
FUObjectThreadContext維護(hù)了一個(gè)全局堆棧, 用于保存當(dāng)前的FObjectInitializer
使用的時(shí)候直接取棧頂?shù)腇ObjectInitializer
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);
}