From 22a52bc7b2a46a37670ed6d947c42009c80e676d Mon Sep 17 00:00:00 2001 From: sconel <32552662+sconel@users.noreply.github.com> Date: Fri, 9 Jul 2021 13:39:17 -0700 Subject: [PATCH] Fix issue where game and editor entities could exist simultaneously while exiting game mode (#2015) Signed-off-by: sconel --- .../PrefabEditorEntityOwnershipService.cpp | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp index 8c541a2392..9dce8ff946 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Entity/PrefabEditorEntityOwnershipService.cpp @@ -597,22 +597,23 @@ namespace AzToolsFramework { if (m_rootInstance && m_playInEditorData.m_isEnabled) { - auto end = m_playInEditorData.m_deactivatedEntities.rend(); - for (auto it = m_playInEditorData.m_deactivatedEntities.rbegin(); it != end; ++it) - { - AZ_Assert(*it, "Invalid entity added to list for re-activation after play-in-editor stopped."); - (*it)->Activate(); - } - m_playInEditorData.m_deactivatedEntities.clear(); - AZ_Assert(m_playInEditorData.m_entities.IsSet(), "Invalid Game Mode Entities Container encountered after play-in-editor stopped. " "Confirm that the container was initialized correctly"); m_playInEditorData.m_entities.DespawnAllEntities(); m_playInEditorData.m_entities.Alert( - [assets = AZStd::move(m_playInEditorData.m_assets)]([[maybe_unused]]uint32_t generation) mutable + [assets = AZStd::move(m_playInEditorData.m_assets), + deactivatedEntities = AZStd::move(m_playInEditorData.m_deactivatedEntities)] + ([[maybe_unused]]uint32_t generation) mutable { + auto end = deactivatedEntities.rend(); + for (auto it = deactivatedEntities.rbegin(); it != end; ++it) + { + AZ_Assert(*it, "Invalid entity added to list for re-activation after play-in-editor stopped."); + (*it)->Activate(); + } + for (auto& asset : assets) { if (asset) @@ -624,13 +625,21 @@ namespace AzToolsFramework } } AZ::ScriptSystemRequestBus::Broadcast(&AZ::ScriptSystemRequests::GarbageCollect); + + // This is a workaround until the replacement for GameEntityContext is done + AzFramework::GameEntityContextEventBus::Broadcast(&AzFramework::GameEntityContextEventBus::Events::OnGameEntitiesReset); }); m_playInEditorData.m_entities.Clear(); - - // This is a workaround until the replacement for GameEntityContext is done - AzFramework::GameEntityContextEventBus::Broadcast(&AzFramework::GameEntityContextEventBus::Events::OnGameEntitiesReset); } + // Game entity cleanup is queued onto the next tick via the DespawnEntities call. + // To avoid both game entities and Editor entities active at the same time + // we flush the tick queue to ensure the game entities are cleared first. + // The Alert callback that follows the DespawnEntities call will then reactivate the editor entities + // This should be considered temporary as a move to a less rigid event sequence that supports async entity clean up + // is the desired direction forward. + AZ::TickBus::ExecuteQueuedEvents(); + m_playInEditorData.m_isEnabled = false; }