Skip to content
This repository was archived by the owner on Jun 28, 2026. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions Robust.Shared/GameObjects/Systems/SharedUserInterfaceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ public abstract partial class SharedUserInterfaceSystem : EntitySystem

private ActorRangeCheckJob _rangeJob;

/// Update type to apply
private enum QueuedUpdate
{
/// Upen a UI
Open,
/// Apply new state to the UI
ApplyState,
/// Close the UI
Close
};

/// <summary>
/// Defer BUIs during state handling so client doesn't spam a BUI constantly during prediction.
/// </summary>
private readonly List<(BoundUserInterface Bui, bool value)> _queuedBuis = new();
private readonly List<(BoundUserInterface bui, QueuedUpdate updateType)> _queuedBuis = new();

public override void Initialize()
{
Expand Down Expand Up @@ -82,9 +93,9 @@ public override void Initialize()
SubscribeLocalEvent<UserInterfaceUserComponent, ComponentShutdown>(OnActorShutdown);
}

private void AddQueued(BoundUserInterface bui, bool value)
private void AddQueued(BoundUserInterface bui, QueuedUpdate type)
{
_queuedBuis.Add((bui, value));
_queuedBuis.Add((bui, type));
}

/// <summary>
Expand Down Expand Up @@ -238,7 +249,7 @@ private void CloseUiInternal(Entity<UserInterfaceComponent?> ent, Enum key, Enti

if (ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui))
{
AddQueued(cBui, false);
AddQueued(cBui, QueuedUpdate.Close);
}

if (ent.Comp.Actors.Count == 0)
Expand Down Expand Up @@ -280,7 +291,7 @@ private void OnUserInterfaceStartup(Entity<UserInterfaceComponent> ent, ref Comp
// PlayerAttachedEvent will catch some of these.
foreach (var (key, bui) in ent.Comp.ClientOpenInterfaces)
{
AddQueued(bui, true);
AddQueued(bui, QueuedUpdate.Open);
}
}

Expand All @@ -300,7 +311,7 @@ protected void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref C
DebugTools.Assert(!ent.Comp.Actors.ContainsKey(key));
}

DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, false))));
DebugTools.Assert(ent.Comp.ClientOpenInterfaces.Values.All(x => _queuedBuis.Contains((x, QueuedUpdate.Close))));
}

private void OnUserInterfaceGetState(Entity<UserInterfaceComponent> ent, ref ComponentGetState args)
Expand Down Expand Up @@ -462,7 +473,7 @@ private void OnUserInterfaceHandleState(Entity<UserInterfaceComponent> ent, ref
}

var bui = ent.Comp.ClientOpenInterfaces[key];
AddQueued(bui, false);
AddQueued(bui, QueuedUpdate.Close);
}
}

Expand All @@ -489,9 +500,7 @@ private void OnUserInterfaceHandleState(Entity<UserInterfaceComponent> ent, ref
if (!ent.Comp.ClientOpenInterfaces.TryGetValue(key, out var cBui) || !cBui.IsOpened)
continue;

cBui.State = buiState;
cBui.UpdateState(buiState);
cBui.Update();
AddQueued(cBui, QueuedUpdate.ApplyState);
}
}

Expand Down Expand Up @@ -519,7 +528,7 @@ private void EnsureClientBui(Entity<UserInterfaceComponent> entity, Enum key, In
// Existing BUI just keep it.
if (entity.Comp.ClientOpenInterfaces.TryGetValue(key, out var existing))
{
_queuedBuis.Remove((existing, false));
_queuedBuis.Remove((existing, QueuedUpdate.Close));
return;
}

Expand All @@ -544,7 +553,7 @@ private void EnsureClientBui(Entity<UserInterfaceComponent> entity, Enum key, In
if (!open)
return;

AddQueued(boundUserInterface, true);
AddQueued(boundUserInterface, QueuedUpdate.Open);
}

/// <summary>
Expand Down Expand Up @@ -1101,15 +1110,18 @@ public override void Update(float frameTime)
{
if (_timing.IsFirstTimePredicted)
{
foreach (var (bui, open) in _queuedBuis)
foreach (var (bui, updateType) in _queuedBuis)
{
if (open)
if (updateType == QueuedUpdate.Open || updateType == QueuedUpdate.ApplyState)
{
#if EXCEPTION_TOLERANCE
try
{
#endif
bui.Open();
if (updateType == QueuedUpdate.Open)
{
bui.Open();
}

if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
Expand Down
Loading