From 84204aed4655232feb08f92f9374aad5f3c0b1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 17 Mar 2024 20:10:35 +0100 Subject: [PATCH] No longer crash when selected entity disappears --- .../src/EditWindows/ComponentEditWindow.bf | 6 ++- .../src/EditWindows/EditorViewportWindow.bf | 2 +- .../src/EditWindows/EntityHierarchyWindow.bf | 45 ++++++++++++++----- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index cd2d448..583a7da 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -49,8 +49,10 @@ namespace GlitchyEditor.EditWindows if (_entityHierarchyWindow.SelectionSize == 1) { - Entity entity = _entityHierarchyWindow.GetSelectedEntity(0); - ShowComponents(entity); + Result entityResult = _entityHierarchyWindow.GetSelectedEntity(0); + + if (entityResult case .Ok(let selectedEntity)) + ShowComponents(selectedEntity); } else { diff --git a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf index d316a34..fe5cbc0 100644 --- a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf +++ b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf @@ -326,7 +326,7 @@ namespace GlitchyEditor.EditWindows if(_editor.EntityHierarchyWindow.SelectionSize == 0) return false; - Entity entity = _editor.EntityHierarchyWindow.GetSelectedEntity(-1); + Entity entity = TrySilent!(_editor.EntityHierarchyWindow.GetSelectedEntity(-1)); var transformCmp = entity.GetComponent(); diff --git a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf index 80f9568..4914a1b 100644 --- a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf +++ b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf @@ -37,7 +37,7 @@ namespace GlitchyEditor.EditWindows public int SelectionSize => _selectedEntityIds.Count; /// Returns the Entity at the given index or null if it doesn't exist. - public Entity GetSelectedEntity(int index) + public Result GetSelectedEntity(int index) { var index; @@ -50,10 +50,14 @@ namespace GlitchyEditor.EditWindows if (selectedEntity case .Ok(let entity)) return entity; + else + { + // Entity doesn't exist. This can have many reasons, but it is safe to assume, that it existed at some point and probably got deleted. + // Thus we just remove it from the selection. + _selectedEntityIds.RemoveAt(index); - // TODO: The entity should always exist in the scene, I'm sure! - // If we can assume that, then we can remove the nullable and make everything even easier! - Runtime.FatalError(scope $"No entity exists with selected id \"{id}\"."); + return .Err; + } } public void HighlightEntity(Entity e) @@ -188,7 +192,12 @@ namespace GlitchyEditor.EditWindows for (int i < SelectionSize) { - Entity entity = GetSelectedEntity(i); + Result entityResult = GetSelectedEntity(i); + + if (entityResult case .Err) + continue; + + Entity entity = entityResult; var transformComponent = entity.GetComponent(); @@ -275,7 +284,7 @@ namespace GlitchyEditor.EditWindows return; } - let commonParent = GetSelectedEntity(0).GetComponent(); + let commonParent = TrySilent!(GetSelectedEntity(0)).GetComponent(); let newEntity = _scene.CreateEntity(); @@ -290,7 +299,12 @@ namespace GlitchyEditor.EditWindows // new entity is parent of all selected entities. for (int i < SelectionSize) { - let selectedTransform = GetSelectedEntity(i).GetComponent(); + Result selectedEntity = GetSelectedEntity(i); + + if (selectedEntity case .Err) + continue; + + let selectedTransform = selectedEntity.Value.GetComponent(); selectedTransform?.Parent = newEntity.Handle; } } @@ -307,8 +321,13 @@ namespace GlitchyEditor.EditWindows _scene.CreateEntity(); else { - Entity? parent = GetSelectedEntity(-1).Parent; - CreateChild(parent); + Result selectedEntityResult = GetSelectedEntity(-1); + + if (selectedEntityResult case .Ok(let selectedEntity)) + { + Entity? parent = selectedEntity.Parent; + CreateChild(parent); + } } } @@ -322,8 +341,12 @@ namespace GlitchyEditor.EditWindows { if(ImGui.MenuItem("Empty Child", null, false, SelectionSize != 0)) { - Entity selectedEntity = GetSelectedEntity(-1); - CreateChild(selectedEntity); + Result selectedEntityResult = GetSelectedEntity(-1); + + if (selectedEntityResult case .Ok(let selectedEntity)) + { + CreateChild(selectedEntity); + } } if(ImGui.IsItemHovered())