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

MenuValueComponent`1

Class
Namespace: SwiftlyS2.Shared.Menu.Components · Assembly: SwiftlyS2.CS2.dll
public abstract class MenuValueComponent<TValue> : TextComponent, IMenuComponent

The shared behaviour of a component that holds a value.

The value lives in IMenuSession state, so one instance of the component serves every player without their values touching, and the value is gone once the menu closes. Use OnChanged to persist it and DefaultValueProvider to seed it back.

View source

Constructors

MenuValueComponent(string, string?)

Creates a value component.

protected MenuValueComponent(string text = "", string? id = null)
Parameters
  • textstringoptionalThe label to display.
  • idstring?optionalA stable id, or null to generate one.

Properties

Comparer

How values are compared to decide whether a change happened.

protected virtual IEqualityComparer<TValue> Comparer { get; }
Property Value

DefaultValue

The value a player starts with when DefaultValueProvider is null.

public TValue DefaultValue { get; set; }
Property Value
TValue

DefaultValueProvider

Produces the value a player starts with, or null to use DefaultValue.

public Func<IMenuSession, TValue>? DefaultValueProvider { get; set; }
Property Value
Func<IMenuSession, TValue>?

Called once per session, on first access. This is where a stored value is read back.

IsFocusable

Whether the selection can land on this component.

public override bool IsFocusable { get; }
Property Value

A label can hold the selection like any other body entry, so a menu scrolls past it rather than skipping over it, but it never overrides MenuActionContext) so activating it is always a no-op.

OnChanged

Runs after a player changes the value.

public Action<MenuValueChangedContext<TValue>>? OnChanged { get; set; }
Property Value
Action<MenuValueChangedContext<TValue>>?

Separator

The separator drawn between the label and the value.

public string Separator { get; set; }
Property Value

ValueColor

The colour of the value, or null to keep Style.

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

Methods

Coerce(IMenuSession, TValue)

Adjusts a value before it is stored.

protected virtual TValue Coerce(IMenuSession session, TValue value)
Parameters
  • sessionIMenuSessionThe session the value belongs to.
  • valueTValueThe value being stored.
Returns
TValue
The value to store.

Used for clamping into a range or into the bounds of a list.

GetValue(IMenuSession)

Reads this player's value, seeding it from the default on first use.

public TValue GetValue(IMenuSession session)
Parameters
Returns
TValue
The current value.

RenderLabel(IMenuComponentRenderContext)

Builds the label part of this component's line.

protected MenuNode? RenderLabel(IMenuComponentRenderContext context)
Parameters
Returns
The label node, or null when there is no label.

RenderValue(IMenuComponentRenderContext, string, string?)

Builds a node for a value, greyed out when the component is disabled.

protected MenuNode RenderValue(IMenuComponentRenderContext context, string text, string? color = null)
Parameters
Returns
The value node.

SetValue(IMenuSession, TValue)

Writes this player's value.

public bool SetValue(IMenuSession session, TValue value)
Parameters
  • sessionIMenuSessionThe session to write for.
  • valueTValueThe value to store. Components may adjust it before it is stored.
Returns
true when the stored value actually changed.