Fix calling script OnDestroy when removing actors or scripts from the scene

This commit is contained in:
Wojtek Figat
2023-12-18 21:43:13 +01:00
parent 72f45afa45
commit bc2e130281
3 changed files with 41 additions and 12 deletions

View File

@@ -27,6 +27,7 @@ Script::Script(const SpawnParams& params)
, _tickUpdate(false)
, _tickLateUpdate(false)
, _tickLateFixedUpdate(false)
, _wasAwakeCalled(false)
, _wasStartCalled(false)
, _wasEnableCalled(false)
{
@@ -86,7 +87,7 @@ void Script::SetParent(Actor* value, bool canBreakPrefabLink)
// Unlink from the old one
if (_parent)
{
if (!value && _parent->IsDuringPlay() && _parent->IsActiveInHierarchy() && GetEnabled())
if (!value && _parent->IsDuringPlay() && _parent->IsActiveInHierarchy() && GetEnabled() && _wasEnableCalled)
{
// Call disable when script is removed from actor (new actor is null)
Disable();
@@ -241,19 +242,31 @@ String Script::ToString() const
void Script::OnDeleteObject()
{
// Ensure to unlink from the parent (it will call Disable event if required)
SetParent(nullptr);
// Check if remove object from game
if (IsDuringPlay())
// Call OnDisable
if (_wasEnableCalled)
{
Disable();
}
// Call OnDestroy
if (_wasAwakeCalled)
{
_wasAwakeCalled = false;
CHECK_EXECUTE_IN_EDITOR
{
OnDestroy();
}
}
// End play
if (IsDuringPlay())
{
EndPlay();
}
// Unlink from parent
SetParent(nullptr);
// Base
SceneObject::OnDeleteObject();
}
@@ -274,6 +287,9 @@ void Script::Initialize()
if (!IsRegistered())
RegisterObject();
// Call OnAwake
ASSERT(!_wasAwakeCalled);
_wasAwakeCalled = true;
CHECK_EXECUTE_IN_EDITOR
{
OnAwake();