Skip to content

Commit

Permalink
fix: EntityPrototypeView now only creates entities when the UI is open
Browse files Browse the repository at this point in the history
Co-authored-by: pa.pecherskij <[email protected]>
Co-authored-by: metalgearsloth <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2025
1 parent d5e6e91 commit de55d1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 25 additions & 9 deletions Robust.Client/UserInterface/Controls/EntityPrototypeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public EntityPrototypeView(EntProtoId? entProto, IEntityManager entMan) : base(e

public void SetPrototype(EntProtoId? entProto)
{
SpriteSystem ??= EntMan.System<SpriteSystem>();

if (entProto == _currentPrototype
&& EntMan.TryGetComponent(Entity?.Owner, out MetaDataComponent? meta)
&& meta.EntityPrototype?.ID == _currentPrototype)
Expand All @@ -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();
}
}

Expand All @@ -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<SpriteSystem>();

_ourEntity = EntMan.Spawn(_currentPrototype);
SpriteSystem.ForceUpdate(_ourEntity.Value);
SetEntity(_ourEntity);
}
else
{
_ourEntity = null;
}
}
}

0 comments on commit de55d1b

Please sign in to comment.