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

MenuChoiceComponentBase`1

Class
Namespace: SwiftlyS2.Shared.Menu.Components · Assembly: SwiftlyS2.CS2.dll
public abstract class MenuChoiceComponentBase<TItem> : MenuValueComponent<int>, IMenuComponent
Inherits object, MenuComponentBase, TextComponent, MenuValueComponent<int>
Implements IMenuComponent

The shared behaviour of a component that picks one item out of a list.

The stored value is the index, which keeps duplicate items in the list unambiguous. Use IMenuSession) and OnItemChanged to work in terms of items instead.

View source

Constructors

MenuChoiceComponentBase(string, IEnumerable<TItem>?, int, string?)

Creates a choice component.

protected MenuChoiceComponentBase(string text = "", IEnumerable<TItem>? choices = null, int defaultIndex = 0, string? id = null)
Parameters
  • textstringoptionalThe label to display.
  • choicesIEnumerable<TItem>?optionalThe items to pick from.
  • defaultIndexintoptionalThe index a player starts on.
  • idstring?optionalA stable id, or null to generate one.

Properties

Choices

The items to pick from.

public IReadOnlyList<TItem> Choices { get; set; }
Property Value

EmptyText

The text shown when Choices is empty.

public string EmptyText { get; set; }
Property Value

Formatter

Turns an item into the text shown for it.

public Func<TItem, string> Formatter { get; set; }
Property Value
Func<TItem, string>

OnItemChanged

Runs after a player moves to a different item.

public Action<MenuValueChangedContext<TItem>>? OnItemChanged { get; set; }
Property Value
Action<MenuValueChangedContext<TItem>>?

WrapAround

Whether stepping past either end continues from the other.

public bool WrapAround { get; set; }
Property Value

Methods

Coerce(IMenuSession, int)

Adjusts a value before it is stored.

protected override int Coerce(IMenuSession session, int value)
Parameters
  • sessionIMenuSessionThe session the value belongs to.
  • valueintThe value being stored.
Returns
The value to store.

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

Format(int)

Formats the item at an index, tolerating an empty list.

protected string Format(int index)
Parameters
  • indexintThe index to format.
Returns
The formatted item, or EmptyText.

GetItem(IMenuSession)

Reads the item this player is on.

public TItem? GetItem(IMenuSession session)
Parameters
Returns
TItem?
The selected item, or the type default when the list is empty.

Step(IMenuSession, int)

Moves this player to a different item.

public bool Step(IMenuSession session, int delta)
Parameters
  • sessionIMenuSessionThe session to move.
  • deltaintHow far to move. Negative moves towards the start.
Returns
true when the selection changed.