中EN

Actor Lifecycle

  • Load from Disk (LoadMap / level streaming AddToWorld): load → PostLoad (custom versioning/fixup; mutually exclusive with PostActorCreated) → UAISystemBase::InitializeActorsForPlay → ULevel::RouteActorInitialize: PreInitializeComponents → UActorComponent::InitializeComponent → PostInitializeComponents → BeginPlay when level started.
  • Play in Editor: actors duplicated from editor into new World → UObject::PostDuplicate → same initialize path.
  • Spawning: UWorld::SpawnActor → PostSpawnInitialize → PostActorCreated ("any constructor implementation behavior should go here") → ExecuteConstruction → OnConstruction (Blueprint components created, BP variables initialized) → PostActorConstruction (PreInitializeComponents/InitializeComponent/PostInitializeComponents) → UWorld::OnActorSpawned broadcast → BeginPlay.
  • Deferred Spawn ("Expose on Spawn"): UWorld::SpawnActorDeferred for procedural actors, allows setup before construction script; after PostActorCreated do setup with valid but incomplete actor; FinishSpawning resumes at ExecuteConstruction.
  • End: Destroy marks pending kill and removes from Level's actor array. EndPlay called from: explicit Destroy, PIE end, level transitions (seamless travel/load map), streaming level unload, lifetime expired, application shutdown. Actor marked RF_PendingKill → deallocated next GC; prefer FWeakObjectPtr over manual pending-kill checks.
  • EndPlay doesn't necessarily mean destroyed: if s.ForceGCAfterLevelStreamedOut is false and sublevel quickly reloaded, actor may be "resurrected" with local variables not re-initialized.
  • OnDestroyed is legacy; move logic to EndPlay.
  • GC destruction: BeginDestroy (free memory, multithreaded resources e.g. render-thread proxies; gameplay cleanup should be in EndPlay) → IsReadyForFinishDestroy (return false to defer to next GC pass) → FinishDestroy (last call before memory freed).
  • GC clusters: object + subobjects in a single cluster freed together; reduces GC time and memory churn. Project Settings "Create Garbage Collector UObject Clusters" can be disabled, usually less efficient — only if profiling shows benefit.

引用这一章的笔记

输入关键词开始搜索

滚轮或双指缩放 · 拖动平移 · 双击放大 / 还原