![](https://img.haomeiwen.com/i12461829/95e8d915340e06d6.png)
我们在这里设置要重载的子类: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时候, 会自动触发函数重载.
![](https://img.haomeiwen.com/i12461829/3d74d42781ed539b.png)
从ComponentOverrides取出UBaseCharacterMovementComp, 然后创建:
![](https://img.haomeiwen.com/i12461829/4f12b209345ddd3b.png)
调用堆栈:
![](https://img.haomeiwen.com/i12461829/34b893b2d6ce9568.png)
这里还要补充一句:
FUObjectThreadContext维护了一个全局堆栈, 用于保存当前的FObjectInitializer
![](https://img.haomeiwen.com/i12461829/a98a57d3d3cff80d.png)
使用的时候直接取栈顶的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);
}
网友评论