From de55d1bc52db0a44a53759652a8f328857863498 Mon Sep 17 00:00:00 2001 From: Fildrance Date: Mon, 27 Jan 2025 16:25:52 +0300 Subject: [PATCH] fix: EntityPrototypeView now only creates entities when the UI is open Co-authored-by: pa.pecherskij Co-authored-by: metalgearsloth --- RELEASE-NOTES.md | 2 +- .../Controls/EntityPrototypeView.cs | 34 ++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 9b21f246115..5b65967d34f 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -43,7 +43,7 @@ END TEMPLATE--> ### Bugfixes -*None yet* +* EntityPrototypeView control now avoids creating entities if the prototype is set while the control is not on the UI tree. ### Other diff --git a/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs b/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs index 5aa23f3dc33..5173a3da4fa 100644 --- a/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs +++ b/Robust.Client/UserInterface/Controls/EntityPrototypeView.cs @@ -22,8 +22,6 @@ public EntityPrototypeView(EntProtoId? entProto, IEntityManager entMan) : base(e public void SetPrototype(EntProtoId? entProto) { - SpriteSystem ??= EntMan.System(); - if (entProto == _currentPrototype && EntMan.TryGetComponent(Entity?.Owner, out MetaDataComponent? meta) && meta.EntityPrototype?.ID == _currentPrototype) @@ -32,14 +30,10 @@ public void SetPrototype(EntProtoId? entProto) } _currentPrototype = entProto; - SetEntity(null); - EntMan.DeleteEntity(_ourEntity); - if (_currentPrototype != null) + if (_ourEntity != null) { - _ourEntity = EntMan.Spawn(_currentPrototype); - SpriteSystem.ForceUpdate(_ourEntity.Value); - SetEntity(_ourEntity); + UpdateEntity(); } } @@ -48,12 +42,34 @@ protected override void EnteredTree() base.EnteredTree(); if (_currentPrototype != null) - SetPrototype(_currentPrototype); + { + UpdateEntity(); + } } protected override void ExitedTree() { base.ExitedTree(); EntMan.TryQueueDeleteEntity(_ourEntity); + _ourEntity = null; + } + + private void UpdateEntity() + { + SetEntity(null); + EntMan.DeleteEntity(_ourEntity); + + if (_currentPrototype != null) + { + SpriteSystem ??= EntMan.System(); + + _ourEntity = EntMan.Spawn(_currentPrototype); + SpriteSystem.ForceUpdate(_ourEntity.Value); + SetEntity(_ourEntity); + } + else + { + _ourEntity = null; + } } }