swiftlys2/docs/api/ai/
API Reference › Menu

MenuComponentBase

Class
Namespace: SwiftlyS2.Shared.Menu · Assembly: SwiftlyS2.CS2.dll
public abstract class MenuComponentBase : IMenuComponent
Inherits object
Implements IMenuComponent

The recommended starting point for a component.

Supplies identity, visibility and enabled rules, the activation pipeline shared by every interactive component, and a no-op action handler, leaving IMenuComponentRenderContext) as the only required member.

View source

Constructors

MenuComponentBase(string?)

Creates a component.

protected MenuComponentBase(string? id = null)
Parameters
  • idstring?optionalA stable id, or null to generate one.

Properties

Comment

The hint shown while this component holds the selection, or null for none.

public string? Comment { get; set; }
Property Value

Enabled

Whether this component accepts actions, for every player.

public bool Enabled { get; set; }
Property Value

EnabledWhen

Decides interactivity per player, or null to use Enabled.

public Func<IMenuSession, bool>? EnabledWhen { get; set; }
Property Value

Id

A stable id for this component, unique within its menu.

public string Id { get; }
Property Value

IsFocusable

Whether the selection can land on this component.

public virtual bool IsFocusable { get; }
Property Value

LineCount

How many lines this component draws.

public virtual int LineCount { get; }
Property Value

Used to decide how much of the body fits on one page, without rendering components that will not be shown. It must match what IMenuComponentRenderContext) produces: a MenuBlankNode costs its Lines, a MenuStackNode costs the sum of its children, and everything else costs one. A component that returns a stack has to override this, otherwise it is budgeted as a single line and the page overflows.

PlaySound

Whether activating this component plays the menu's selection sound.

public bool PlaySound { get; set; }
Property Value

Tag

An arbitrary value carried alongside this component.

public object? Tag { get; set; }
Property Value

Validating

Runs before an activation and can cancel it by returning false .

public Func<MenuActionContext, ValueTask<bool>>? Validating { get; set; }

Visible

Whether this component is shown, for every player.

public bool Visible { get; set; }
Property Value

VisibleWhen

Decides visibility per player, or null to use Visible.

public Func<IMenuSession, bool>? VisibleWhen { get; set; }
Property Value

Evaluated on every frame. This is how one component instance is hidden from some players and not others without holding any per-player state itself.

WaitingColor

The colour applied to WaitingText, or null to keep the normal style.

public string? WaitingColor { get; set; }
Property Value

WaitingText

The text shown in place of this component's own while an activation is still running.

public string WaitingText { get; set; }
Property Value

Methods

ActivateAsync(MenuActionContext, Func<MenuActionContext, ValueTask>)

Runs an activation through the shared pipeline.

protected ValueTask<bool> ActivateAsync(MenuActionContext context, Func<MenuActionContext, ValueTask> body)
Parameters
Returns
true once the action has been consumed.

Refuses when the component is disabled or a previous activation for the same player is still running, then consults Validating. While body runs the component draws WaitingText, so a slow callback cannot be triggered twice.

GetHint(IMenuSession)

Produces the hint shown while this component holds the selection.

public virtual string? GetHint(IMenuSession session)
Parameters
Returns
The hint text, or null to show nothing.

Use this for a per-entry explanation, or to advertise keys the component takes over through MenuActionContext).

HandleActionAsync(MenuActionContext)

Offers an action to this component before the menu handles it.

public virtual ValueTask<bool> HandleActionAsync(MenuActionContext context)
Parameters
Returns
true to consume the action and stop the menu's own handling from running.

Only the focused component is offered actions. Returning true is how a component takes over a key that would otherwise navigate or close the menu.

IsBusy(IMenuSession)

Whether an activation started by this player is still running.

protected bool IsBusy(IMenuSession session)
Parameters
Returns
true while the previous activation has not finished.

IsEnabled(IMenuSession)

Whether this component can be interacted with by a player.

public virtual bool IsEnabled(IMenuSession session)
Parameters
Returns
true when the component accepts actions.

IsVisible(IMenuSession)

Whether this component is shown to a player.

public virtual bool IsVisible(IMenuSession session)
Parameters
Returns
true when the component should be drawn.

Matches(MenuActionContext, MenuActionId)

Whether an action matches a built-in or declared action by name.

protected static bool Matches(MenuActionContext context, MenuActionId action)
Parameters
Returns
true when the names match.

NeedsRedraw(IMenuSession, DateTime)

Whether this component needs a new frame even though nothing invalidated the session.

public virtual bool NeedsRedraw(IMenuSession session, DateTime now)
Parameters
Returns
true to redraw on this tick.

Asked once per tick for every visible component of an otherwise clean session, so keep the check cheap. Animated text and live values are what this exists for.

Render(IMenuComponentRenderContext)

Produces this component's renderer-agnostic output.

public abstract MenuNode Render(IMenuComponentRenderContext context)
Parameters
Returns
The nodes describing this component.