API reference

Component script

What a component's <script lang="cs"> may declare, and the Component class every component derives from. The attributes need no using.

Attributes

DeclarationMeaning
[State] int count;Reactive state. The field becomes a property backed by a signal: assigning it updates what reads it. Make it public when game code sets it
[Derived] string label => ...;A value computed from state. Cached; recomputed only when what it read changed; read-only
[Prop] public string Title = "x";An input the parent sets: <Card Title="Sword" />. The initializer is the default
[Prop(Required = true)] public Item Item;A prop the parent must give
[Prop, Bindable] public float Value;A prop the child may assign. The parent writes bind:Value={x} and its x follows
[Prop] public Action OnBuy;A callback prop: OnBuy={() => Buy(item)}
[Effect] void Sync() { ... }Runs after the page updated, and again whenever state it read changes
[Effect(EffectKind.Pre)] void Before() { ... }The same, before the page updates
[Tick] float timeLeft => Round.TimeLeft;Read every frame. The page updates only when the value differs from last frame: for values game systems own

Avoid LINQ in [Derived] and [Tick]: it allocates on every run. Loops and interpolation do not.

Lifecycle

protected override void OnMount()   { /* the component's elements are in the page */ }
protected override void OnDestroy() { /* release what OnMount took */ }

Context

Context passes a value to every descendant without threading it through props.

using DevCore.UiEngine.Components;

public static readonly ContextKey<Theme> ThemeKey = new ContextKey<Theme>("theme");

SetContext(ThemeKey, theme);            // in an ancestor
var theme = GetContext(ThemeKey);       // in any descendant; default(T) when no ancestor set it

Texts

MemberMeaning
T(string key)The text of key in the current locale. Reactive
T(key, int | long | double count)The plural form for the number, with the number filled in
T(key, params object[] args)The text with {0}, {1}… filled in, formatted for the locale
Plural(key, long count)The plural form's text, not filled in. Allocates nothing

The Component class

DevCore.UiEngine.Components.Component. Generated component classes are sealed partial, so you can add members in another partial file.

MemberMeaning
void Mount(Node target), Mount(Node target, Node anchor)Builds the component's elements inside target (before anchor)
void Destroy()Removes its elements, effects and children
void ReplaceWith(Component replacement)Puts another component in its place
Component Parent, IReadOnlyList<Component> ChildrenThe component tree
Document DocumentThe document it is mounted in
Element RootElementIts first element
bool IsMounted, bool IsDestroyedIts state
bool Contains(Node node)Whether a node belongs to it
Q, Q<T>, Query<T>Find elements and components inside it by name. See Queries by name
static Component OwnerOf(Node node)The component a node belongs to
static IReadOnlyList<Component> RootsOf(Document document)The components mounted directly in a document
bool SetPropFromString(string name, string value)Sets a prop from text, as a page's HTML attributes do

Mounting from code

var view = GetComponent<UIView>();
var counter = view.Mount(new MyGame.UI.Counter());         // into the page's body
var toast = view.Mount(new MyGame.UI.Toast(), container);   // into an element

ComponentRegistry

Every compiled component registers itself by tag name and by full class name, so a page's HTML can use it as a tag.

MemberMeaning
static void Register(string name, string fullName, Func<Component> factory)Registers a component
static bool TryCreate(string name, out Component component)Creates one by tag name or full name
static bool IsRegistered(string name)Whether a name is known
static IEnumerable<string> Names, FullNamesWhat is registered