UE4C++(5):⽣命周期
10/14/2020
⽂章⽬录
UGameEngine
在Project Setting设置中绑定GameInstance
UGameEngine()=default;//构造函数为空,默认没有
void Init()
{
FSoftClassPath GameInstanceClassName = GetDefault<UGameMapsSettings>()->GameInstanceClass;
UClass* GameInstanceClass =(GameInstanceClassName.IsValid()? LoadObject<UClass>(NULL,*GameInstanceClassName.ToString()): UGameInstanc e::StaticClass())
GameInstance = NewObject<UGameInstance>(this, GameInstanceClass);
//Create GameInstance
GameInstance->InitializeStandalone();//
//Create GameViewport --- 平台相关的渲染,输⼊和声⾳
}
void Start()
{
GameInstance->StartGameInstance();
}
void Tick()
{
//GameViewport相关
}
1. 构造函数
2. Init():对GameInstance先LoadObject再NewObject
3. Start():StartGameInstance()
UGameInstance
UGameInstance()=default;
void InitializeStandalone()
{
//Create World Context and World!
WorldContext =&GetEngine()->CreateNewWorldContext(EWorldType::Game);
//..
UWorld* DummyWorld = UWorld::CreateWorld(EWorldType::Game,false, InPackageName, InWorldPackage);
//..
Init();
}
void Init(){}
void StartGameInstance(){}
void Tick()
{
//UPlayer JoinHostSession
}
1. 构造函数:初始化TimerManager和LatentActionManager
2. InitializeStandalone():创建WorldContext和World
3. Init(): 创建UOnlineSession
雷宇扬4. StartGameInstance():GameMap
WorldContext
CreateNewWorldContext()
{
FWorldContext* NewWorldContext =new FWorldContext;
}
好听的歌曲大全调⽤WorldContext构造函数
王琦个人资料
World
void CreateWorld(....)
{
UWorld* NewWorld = NewObject<UWorld>(WorldPackage,*WorldNameString);//构造函数}
void UWorld::InitializeNewWorld(const InitializationValues IVS)
{
PersistentLevel = NewObject<ULevel>(this,TEXT("PersistentLevel"));
PersistentLevel->Initialize(FURL(nullptr));
孟茜个人资料
PersistentLevel->Model = NewObject<UModel>(PersistentLevel);
PersistentLevel->Model->Initialize(nullptr,1);
PersistentLevel->OwningWorld =this;
/
/重写WorldSetting
// Initialize the world
InitWorld(IVS);
// Update components.
UpdateWorldComponents(true,false);
}
World构造函数
InitializeNewWorld():⽣成PersistentLevel
WorldSetting
InitWorld()
最后的人歌词UpdateWorldComponents()
Level
ULevel(){}
void ULevel::Initialize(const FURL& InURL)
{
URL = InURL;
}
构造函数
Initialize()
Actor的⽣命周期
从下往上的输出,最下⾯最先输出
/
**
* Actor is the base class for an Object that can be placed or spawned in a level.
* Actors may contain a collection of ActorComponents, which can be used to control how actors move, how they are rendered, etc.
* The other main function of an Actor is the replication of properties and function calls across the network during play.
*
* Actor initialization has multiple steps, here's the order of important virtual functions that get called:
* - UObject::PostLoad: For actors statically placed in a level, the normal UObject PostLoad gets called both in the editor and during gameplay.
*                      This is not called for newly spawned actors.
* - UActorComponent::OnComponentCreated: When an actor is spawned in the editor or during gam
eplay, this gets called for any native components. *                                        For blueprint-created components, this gets called during construction for that component.
*                                        This is not called for components loaded from a level.
* - AActor::PreRegisterAllComponents: For statically placed actors and spawned actors that have native root components, this gets called now.
*                                    For blueprint actors without a native root component, these registration functions get called later during construction.
* - UActorComponent::RegisterComponent: All components are registered in editor and at runtime, this creates their physical/visual representation.
*                                      These calls may be distributed over multiple frames, but are always after PreRegisterAllComponents.
*                                      This may also get called later on after an UnregisterComponent call removes it from the world.
* - AActor::PostRegisterAllComponents: Called for all actors both in the editor and in gameplay, this is the last function that is called in all cases.
* - AActor::PostActorCreated: When an actor is created in the editor or during gameplay, this gets called right before construction.
*                            This is not called for components loaded from a level.
* - AActor::UserConstructionScript: Called for blueprints that implement a construction script.
* - AActor::OnConstruction: Called at the end of ExecuteConstruction, which calls the blueprint construction script.
*                          This is called after all blueprint-created components are fully created and registered.
大漠敦煌背景音乐
*                          This is only called during gameplay for spawned actors, and may get rerun in the editor when changing blueprints.
* - AActor::PreInitializeComponents: Called before InitializeComponent is called on the actor's components.
*                                    This is only called during gameplay and in certain editor preview windows.
* - UActorComponent::Activate: This will be called only if the component has bAutoActivate set.
*                              It will also got called later on if a component is manually activated.
* - UActorComponent::InitializeComponent: This will be called only if the component has bWantsInitializeComponentSet.
*                                        This only happens once per gameplay session.
* - AActor::PostInitializeComponents: Called after the actor's components have been initialized, only during gameplay and some editor previews.
* - AActor::BeginPlay: Called when the level starts ticking, only during actual gameplay.
*                      This normally happens right after PostInitializeComponents but can be delayed for networked or child actors.
*
* @see docs.unrealengine/latest/INT/Programming/UnrealArchitecture/Actors/
* @see docs.unrealengine/en-us/Programming/UnrealArchitecture/Actors/ActorLifecycle
* @see UActorComponent
*/
1. UObject::PostLoad
2. UActorCompont::OnComponentCreate
3. AActor::PreRegisterAllComponents
4. UActorComponnt::RegisterComponent
5. AActor::PostRegisterAllComponents
6. AActor::PostActorCreated
7. AActor::UserConstructionScript
8. AActor::OnConstruction
9. AActor::PreInitializeComponents
10. UActorComponent::Activate
11. UActorComponent::InitializeComponent
12. AActor::PostInitializeComponents
13. AActor::BeginPlay
总结
对于AActor,先运⾏所有的PostInitializeComponent,再运⾏所有构造函数,之后运⾏所有的BeginPlay,最后运⾏Tick函数游戏从引擎开始,GameEngine->GameInstance->GameMode,最后构造GameMode下的所有绑定的类
总结
通常从GameEngine⾛下来,不断的创建GameInstance->World和WorldContext->Level->AActor,每当我们创建⼀个新的AActor加⼊到关卡中,每个AActor都会按照顺序执⾏它们的⽣命周期!
参考
UE4 AActor的源码