Ss14 ui bui

From space-sunrise/sunrise-station · View interactive version

Ss14 ui bui is an AI agent skill from the GitHub repository space-sunrise/sunrise-station. A practical guide to Bound User Interface (BUI) in SS14: architecture, network messages, input validation, prediction through component state, lifecycle windows and server-client working patterns. Use it when developing and refactoring entity-bound interfaces.

How to use this skill

Skill file

Bound User Interface (BUI) in SS14

This skill only covers BUI (entity-bound UI) :)

Manage XAML layout, EUI and style system in separate skills.

When to choose BUI

Choose BUI when the interface:

Don't choose BUI when you need a global admin/session interface without being tied to an entity (this is an EUI).

BUI mental model

BUI consists of 4 layers:

  1. UiKey + typed messages (BoundUserInterfaceMessage) + optional BoundUserInterfaceState in shared.
  2. Prototypical interface registration (UserInterface.interfaces) and ActivatableUI.key.
  3. Server-side processing of events/messages via SharedUserInterfaceSystem and the feature system.
  4. Client BoundUserInterface, which creates a window, sends messages and updates the UI.

Basic contract (shared)

// Specific BUI key.
[NetSerializable, Serializable]
public enum BatteryUiKey : byte
{
    Key,
}

// Client message to server (button/switch).
[Serializable, NetSerializable]
public sealed class BatterySetInputBreakerMessage(bool on) : BoundUserInterfaceMessage
{
    public bool On = on;
}

// Full UI-state (if you really need a separate BUI-state).
[Serializable, NetSerializable]
public sealed class BatteryBuiState : BoundUserInterfaceState
{
    public bool CanCharge;
    public float Charge;
    public float Capacity;
}

Pattern:

Anti-pattern:

Binding in prototype

# Keychain and client-side BUI class
- type: UserInterface
  interfaces:
    enum.GasVolumePumpUiKey.Key:
      type: GasVolumePumpBoundUserInterface

# How does the player open this key?
- type: ActivatableUI
  key: enum.GasVolumePumpUiKey.Key

Pattern:

Anti-pattern:

Server-side processing: correct scheme

Use Subs.BuiEvents<TComp>(uiKey, ...) to subscribe to BUI events and messages.

public override void Initialize()
{
    base.Initialize();

    Subs.BuiEvents<BatteryInterfaceComponent>(BatteryUiKey.Key, subs =>
    {
        // User messages.
        subs.Event<BatterySetInputBreakerMessage>(HandleSetInputBreaker);
        subs.Event<BatterySetOutputBreakerMessage>(HandleSetOutputBreaker);
    });
}

private void HandleSetInputBreaker(Entity<BatteryInterfaceComponent> ent, ref BatterySetInputBreakerMessage args)
{
    var netBattery = Comp<PowerNetworkBatteryComponent>(ent);
    netBattery.CanCharge = args.On; // Changing the domain model
}

Recommendation:

Client-side BUI: lifecycle

public sealed class GasVolumePumpBoundUserInterface : BoundUserInterface
{
    private GasVolumePumpWindow? _window;

    protected override void Open()
    {
        base.Open();

        // Helper creates a window, opens it, binds Close -> bui.Close(), registers the position.
        _window = this.CreateWindow<GasVolumePumpWindow>();

        _window.ToggleStatusButtonPressed += OnToggle;
        _window.PumpTransferRateChanged += OnRateChanged;
        Update();
    }

    private void OnToggle()
    {
        if (_window == null)
            return;

        SendPredictedMessage(new GasVolumePumpToggleStatusMessage(_window.PumpStatus));
    }
}

Pattern:

Anti-pattern:

Prediction: modern way (preferred)

Priority approach: the UI already reads the network fields of the component, and does not duplicate everything in BoundUserInterfaceState.

1) The component raises `AfterAutoHandleStateEvent`

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class GasVolumePumpComponent : Component
{
    [DataField, AutoNetworkedField]
    public bool Enabled = true;

    [DataField, AutoNetworkedField]
    public float TransferRate = Atmospherics.MaxTransferRate;
}

2) The client system updates the open BUI from the component state

public override void Initialize()
{
    base.Initialize();
    SubscribeLocalEvent<GasVolumePumpComponent, AfterAutoHandleStateEvent>(OnPumpState);
}

private void OnPumpState(Entity<GasVolumePumpComponent> ent, ref AfterAutoHandleStateEvent args)
{
    if (_ui.TryGetOpenUi(ent.Owner, GasVolumePumpUiKey.Key, out var bui))
        bui.Update(); // UI reads the actual component
}

3) BUI sends input via `SendPredictedMessage`

private void OnPumpTransferRatePressed(string value)
{
    var rate = UserInputParser.TryFloat(value, out var parsed) ? parsed : 0f;
    SendPredictedMessage(new GasVolumePumpChangeTransferRateMessage(rate));
}

When you need `BoundUserInterfaceState`

Use SetUiState(...) if:

If everything you need is already AutoNetworkedField, prefer the component path.

High frequency input (sliders, etc.)

For “noisy” input, use the following link:

void IBuiPreTickUpdate.PreTickUpdate()
{
    if (_chargeRateCoalescer.CheckIsModified(out var chargeRateValue))
        _pred!.SendMessage(new BatterySetChargeRateMessage(chargeRateValue));
}

Pattern:

Anti-pattern:

Message security and validation

The default is InterfaceData.RequireInputValidation = true, and that's correct ✅

For each incoming BUI message, the engine raises BoundUserInterfaceMessageAttempt, where the following is checked:

Disable RequireInputValidation only in strictly justified cases.

Distance and auto-close

SharedUserInterfaceSystem does a range-check of open interfaces and closes the BUI when leaving the radius.

Manage this through InterfaceData.InteractionRange and, if necessary, through BoundUserInterfaceCheckRangeEvent.

Pattern:

Anti-pattern:

Critical patterns

Anti-patterns

Checklist before PR

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.