Component Life-cycle
There are several ways to update your UI when a value changes.
Refresh()
Use Refresh() when a plain field changes and you need a rerender:
Example:
private int _score;
public void SetScore(int score)
{
_score = score;
Refresh();
}State Management
Reactive Signals
LuiSignal<T> is the preferred state primitive.
private readonly LuiSignal<int> _gold = new(100);
public override LuiNode Render()
{
return Lui.Text($"Gold: {_gold.Value}", "text-yellow-300");
}Reading .Value inside Render() subscribes the component. Writing .Value triggers a
rerender.
_gold.Value += 10;Peek
Use .Peek to read without subscribing:
var current = _items.Peek.Count;Lists and Collections
Prefer replacing collections rather than mutating them in place:
_items.Value = _items.Peek.Append(newItem).ToList();If you must mutate in place, call Touch():
_items.Peek.Add(newItem);
_items.Touch();Advanced
Rarely used.
Mount and Unmount Hooks
Override these for subscriptions and cleanup:
protected internal override void OnLuiMount()
{
}
protected internal override void OnLuiUnmount()
{
}Use these for LuiRef.GeometryChanged subscriptions and other external event hooks.
SetState
Use SetState to assign and rerender only when the value actually changes:
private int _health;
public void SetHealth(int health)
{
SetState(ref _health, health);
}Use these for LuiRef.GeometryChanged subscriptions and other external event hooks.
Last updated on