From 551e6010ddf1f04b3d3d289ef73bacbe4477fa2a Mon Sep 17 00:00:00 2001 From: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> Date: Tue, 16 Nov 2021 19:23:53 -0600 Subject: [PATCH] Add support for getting the level entity when prefabs are enabled. (#5680) * Add support for getting the level entity when prefabs are enabled. Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> * Made a little prettier. Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> * Addressed PR feedback plus fixed the crash when the level component failed to add. Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com> --- .../hydra_editor_utils.py | 4 +++ .../Application/ToolsApplication.cpp | 27 +++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py index 985e32ede5..19aa6e9d3c 100644 --- a/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py +++ b/AutomatedTesting/Gem/PythonTests/EditorPythonTestTools/editor_python_test_tools/hydra_editor_utils.py @@ -57,6 +57,10 @@ def add_level_component(component_name): level_component_list, entity.EntityType().Level) level_component_outcome = editor.EditorLevelComponentAPIBus(bus.Broadcast, 'AddComponentsOfType', [level_component_type_ids_list[0]]) + if not level_component_outcome.IsSuccess(): + print('Failed to add {} level component'.format(component_name)) + return None + level_component = level_component_outcome.GetValue()[0] return level_component diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Application/ToolsApplication.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Application/ToolsApplication.cpp index 056872edab..d3b3b09583 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Application/ToolsApplication.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Application/ToolsApplication.cpp @@ -1196,14 +1196,25 @@ namespace AzToolsFramework AZ::EntityId ToolsApplication::GetCurrentLevelEntityId() { - AzFramework::EntityContextId editorEntityContextId = AzFramework::EntityContextId::CreateNull(); - AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(editorEntityContextId, &AzToolsFramework::EditorEntityContextRequestBus::Events::GetEditorEntityContextId); - AZ::SliceComponent* rootSliceComponent = nullptr; - AzFramework::SliceEntityOwnershipServiceRequestBus::EventResult(rootSliceComponent, editorEntityContextId, - &AzFramework::SliceEntityOwnershipServiceRequestBus::Events::GetRootSlice); - if (rootSliceComponent && rootSliceComponent->GetMetadataEntity()) - { - return rootSliceComponent->GetMetadataEntity()->GetId(); + if (IsPrefabSystemEnabled()) + { + if (auto prefabPublicInterface = AZ::Interface::Get()) + { + return prefabPublicInterface->GetLevelInstanceContainerEntityId(); + } + } + else + { + AzFramework::EntityContextId editorEntityContextId = AzFramework::EntityContextId::CreateNull(); + AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult( + editorEntityContextId, &AzToolsFramework::EditorEntityContextRequestBus::Events::GetEditorEntityContextId); + AZ::SliceComponent* rootSliceComponent = nullptr; + AzFramework::SliceEntityOwnershipServiceRequestBus::EventResult( + rootSliceComponent, editorEntityContextId, &AzFramework::SliceEntityOwnershipServiceRequestBus::Events::GetRootSlice); + if (rootSliceComponent && rootSliceComponent->GetMetadataEntity()) + { + return rootSliceComponent->GetMetadataEntity()->GetId(); + } } return AZ::EntityId();