EUI + UserInterfaceSystem + UserInterfaceManager
This skill only covers UI lifecycle architecture and networking for interfaces :)
Details of XAML layout and styling are provided in separate skills.
Source of truth and selection of examples
- Priority: current engine code and current game code.
- Use the documentation from
docsas a secondary source. - Do not use a code older than 2 years.
- Do not use examples with TODO/FIXME/problematic comments on the UI grid and lifecycle.
When to choose what
- Local window/widget without server state?
UIController+UserInterfaceManager.
- Is the interface tied to the entity and its components?
BoundUserInterface+UserInterfaceSystem(BUI).
- Server-side dialog/control panel that doesn't have to live on the entity?
EUI(server/clientBaseEui+ messages/status).
Mental model
UserInterfaceManagerowns UI roots (StateRoot,WindowRoot,PopupRoot) and update queues.UIControllerlives as a singleton, connects input/state/system events and windows.- BUI works with
BoundUserInterfaceMessagemessages and often opensBaseWindowthrough helpers. - EUI works with
EuiMessageBasemessages andEuiStateBasestate snapshots with an explicitStateDirty.
Patterns
- Do
EnsureWindow()and reuse the window instead of the uncontrollednew. - In BUI, link
OnClosewindows withbui.Close()so that the client and server close synchronously. - Transmit compact DTO state/message via EUI, without “raw” UI noise.
- Send updates via
StateDirty()/queue rather than spamming them every tick. - Subscribe/unsubscribe to state/system events at the appropriate lifecycle points.
- Share responsibility: the controller controls the flow, the window displays, the system stores the domain state.
Anti-patterns
- Use EUI for high-frequency time-lapse telemetry.
- Create a new window for each click instead of reusing.
- Mix EUI and BUI for the same use-case for no reason.
- Forget to send
CloseEuiMessagewhen closing the client-side window. - Transmit “universal” weakly typed payloads into messages ⚠️
Code examples
Example 1: secure window lifecycle in `UIController`
public sealed class OptionsUIController : UIController
{
private OptionsMenu _optionsWindow = default!;
private void EnsureWindow()
{
if (_optionsWindow is { Disposed: false })
return;
// Creation via UIManager, not direct new.
_optionsWindow = UIManager.CreateWindow<OptionsMenu>();
}
public void ToggleWindow()
{
EnsureWindow();
if (_optionsWindow.IsOpen)
_optionsWindow.Close();
else
_optionsWindow.OpenCentered();
}
}
Example 2: entity-bound UI (BUI) with windowed view
protected override void Open()
{
base.Open();
// We create a window as part of the lifecycle BUI.
_menu = this.CreateWindow<JukeboxMenu>();
_menu.OnPlayPressed += shouldPlay =>
{
// The client sends a typed message to the BUI server part.
SendMessage(shouldPlay ? new JukeboxPlayingMessage() : new JukeboxPauseMessage());
};
_menu.OnStopPressed += () => SendMessage(new JukeboxStopMessage());
}
Example 3: Restoring the window position for the BUI
public static T CreateWindow<T>(this BoundUserInterface bui) where T : BaseWindow, new()
{
var window = GetWindow<T>(bui);
if (bui.EntMan.System<UserInterfaceSystem>().TryGetPosition(bui.Owner, bui.UiKey, out var position))
window.Open(position);
else
window.OpenCentered();
return window;
}
Example 4: server-side EUI manager and deferred state sending
public void QueueStateUpdate(BaseEui eui)
{
_stateUpdateQueue.Enqueue((eui.Player, eui.Id));
}
public void SendUpdates()
{
while (_stateUpdateQueue.TryDequeue(out var tuple))
{
var (player, id) = tuple;
if (!_playerData.TryGetValue(player, out var plyDat) || !plyDat.OpenUIs.TryGetValue(id, out var ui))
continue;
// Real state sending in one place lifecycle.
ui.DoStateUpdate();
}
}
Example 5: type-safe EUI state + client-side application
[Serializable, NetSerializable]
public sealed class PlayerPanelEuiState(
NetUserId guid,
string username,
TimeSpan playtime)
: EuiStateBase
{
public readonly NetUserId Guid = guid;
public readonly string Username = username;
public readonly TimeSpan Playtime = playtime;
}
public override void HandleState(EuiStateBase state)
{
if (state is not PlayerPanelEuiState s)
return;
// The UI is updated from the state snapshot.
PlayerPanel.SetUsername(s.Username);
PlayerPanel.SetPlaytime(s.Playtime);
}
Mini checklist
- Correct layer selected:
UIController/ BUI / EUI. - The closing lifecycle is symmetrical to the opening lifecycle.
- Network messages are minimal and type safe.
- There are no duplicate windows and “dangling” subscriptions.
- The examples are based on fresh, non-problematic code ✅