Editor拡張をしていてデストラクタの実装に困りまっています。
調べていると、ガベージコレクションによって実際に破棄されるタイミングとDestroy関数のタイミングが異なるため、EndPlay関数をoverrideして使うものと理解しました。
しかし、Editor上でスポーンさせたAActorはDestroyしたときにEndPlayは呼ばれませんでした。
これはPlayをしていないからと予測しました。
Play in Editorをしていない状態で、Destroyをして呼ばれる関数はありますか?
また、BeginDestroyはガベージコレクションのタイミングで呼ばれたため、使用方法とマッチしません。
解決策はありますか?
Hi [佳依 [Content removed]
You are correct:
- BeginPlay() and EndPlay() are only called during play, so they are not suited for Editor extensions.
- BeginDestroy(), IsReadyForFinishDestroy(), and FinishDestroy() are only called when the actor (or any UObject) is going to be garbage collected, so this can happen at a later time after the actor was removed from the level. BeginDestroy() is meant to start any asynchronous cleanup that may be required, IsReadyForFinishDestroy() should return whether there are no more async cleanup tasks pending, and FinishDestroy() is meant to do final GameThread cleanup tasks.
If you need an actor to react immediately when deleted from a level in the Editor, you can try to override its “Destroyed()” method. Alternatively, external systems can react by registering a callback with either the current world or the global EditorEngine:
`// World delegates:
UUnrealEditorSubsystem* UnrealEditorSubsystem = GEditor->GetEditorSubsystem();
UWorld* World = UnrealEditorSubsystem->GetEditorWorld();
World->AddOnActorSpawnedHandler(…)
World->AddOnActorDestroyedHandler(…)
World->AddOnActorRemovedFromWorldHandler(…)
// Editor delegates:
GEditor->OnLevelActorAdded().Add*(…)
GEditor->OnLevelActorDeleted().Add*(…)`The GEditor callbacks above can be registered from a C++ Editor Module’s StartupModule() function, but you may have to adjust its “LoadingPhase” to “PostEngineInit” on the uproject or uplugin file.
Please let me know if one of these solutions work for you, and feel free to ask if you need any further assistance.
Best regards,
Vitor
PS: You can find more information about actor lifecycle and related callbacks here: https://dev.epicgames.com/documentation/en\-us/unreal\-engine/unreal\-engine\-actor\-lifecycle?application\_version\=5\.3
ご回答いただき、ありがとうございます!
こちらでも期待値通りの挙動することを確認できました。
そのためこちらに関してはクローズにして頂けると幸いです