Fixed issue where scripts of Entities destroyed during scene unloading are not properly disposed

This commit is contained in:
Kah Wei 2022-11-17 15:43:35 +08:00
parent 17662abef1
commit 6efbea54e1
2 changed files with 4 additions and 4 deletions

View File

@ -307,7 +307,7 @@ namespace SHADE
SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr) SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr)
{ {
auto eventData = reinterpret_cast<const SHEventSpec<SHEntityDestroyedEvent>*>(eventPtr.get()); auto eventData = reinterpret_cast<const SHEventSpec<SHEntityDestroyedEvent>*>(eventPtr.get());
csScriptsRemoveAll(eventData->data->eid); csScriptsRemoveAllImmediately(eventData->data->eid, true);
return eventData->handle; return eventData->handle;
} }

View File

@ -402,8 +402,8 @@ namespace SHADE
System::Collections::Generic::List<Script^>^ scriptList = scripts[entity]; System::Collections::Generic::List<Script^>^ scriptList = scripts[entity];
for each (Script ^ script in scriptList) for each (Script ^ script in scriptList)
{ {
// Call OnDestroy only if indicated and also in play mode // Call OnDestroy only if indicated and also if the game has run
if (callOnDestroy) if (callOnDestroy && Application::IsPlaying || Application::IsPaused)
{ {
script->OnDestroy(); script->OnDestroy();
} }
@ -469,7 +469,7 @@ namespace SHADE
script->OnDestroy(); script->OnDestroy();
} }
auto entity = script->Owner.GetEntity(); auto entity = script->Owner.GetEntity();
auto scriptList = scripts[script->Owner.GetEntity()]; auto scriptList = scripts[script->Owner.GetEntity()]; // Unable to find here
scriptList->Remove(script); scriptList->Remove(script);
if (scriptList->Count <= 0) if (scriptList->Count <= 0)
{ {