Skip to content

Ink System

Ink is the game's UI framework. Everything the game draws on screen, from the HUD to menus, is an ink widget tree. The game script API exposes the ink widget classes under the ink namespace, so a bundle can create widgets and change existing ones. ink.InkSystem gives access to the layers these trees live in.

A text widget created by a bundle, shown on the HUD

Layers

The game UI is split into layers like HUD, Menu, Loading or PhotoMode, each listed in ink.ELayerType. Every layer has a virtual window, the root widget all of its other widgets are children of.

if (ink.InkSystem.IsLayerVisible(ink.ELayerType.Menu))
{
    return; // a menu is open
}

var hud = ink.InkSystem.GetLayer(ink.ELayerType.HUD)?.GetVirtualWindow();
  • IsLayerVisible(type) returns whether the layer is shown right now
  • GetLayer(type) returns the layer, or null when the game doesn't create it. Editor and Debug return null in a normal game session
  • GetVirtualWindow() returns the layer's root window, or null while it has none

Adding a widget

The root window is a regular compound widget, so new widgets can be added to it like in any other widget tree:

cl/HudLabel.cs
static ink.TextWidget? Label;

static void ShowLabel(string message)
{
    var hud = ink.InkSystem.GetLayer(ink.ELayerType.HUD)?.GetVirtualWindow();
    if (hud is null)
    {
        return;
    }

    Label = new ink.TextWidget();
    Label.SetFontFamily("base\\gameplay\\gui\\fonts\\raj\\raj.inkfontfamily", new CName("Medium"));
    Label.SetFontSize(64);
    Label.SetLetterCase(text.LetterCase.UpperCase);
    Label.SetTintColor(new HDRColor(0.37f, 0.96f, 1.0f, 1.0f));
    Label.SetText(message);
    Label.SetAnchor(ink.EAnchor.TopCenter);
    Label.SetAnchorPoint(0.5f, 0.0f);
    Label.SetMargin(0.0f, 120.0f, 0.0f, 0.0f);

    hud.AddChildWidget(((ink.Widget)Label).Downgrade());
}
  • SetFontFamily picks the font, raj is the font of the game's UI
  • AddChildWidget and RemoveChild take a weak handle to an ink.Widget. Cast to ink.Widget before Downgrade() to get one
  • RemoveChild takes the widget out again

Remove your widgets when the bundle stops

Widgets you add to a game layer belong to the game. They stay on screen after your bundle unloads, and nothing can reach them anymore. Remove them when your bundle stops:

re.Events.On(
    "UgcBundleStopping",
    (re.UgcBundleStopping x) =>
    {
        if (x.Name != re.Meta.GetBundleName() || Label is null)
        {
            return;
        }

        ink.InkSystem.GetLayer(ink.ELayerType.HUD)
            ?.GetVirtualWindow()
            ?.RemoveChild(((ink.Widget)Label).Downgrade());
    }
);

Animations

Widgets animate through animation definitions. An ink.anim.Definition contains one or more interpolators, and each interpolator changes one property of the widget over time. PlayAnimation starts a definition on a widget and returns a proxy for the running animation.

static ink.anim.Proxy? FadeIn(ink.Widget widget)
{
    var alpha = new ink.anim.TransparencyInterpolator();
    alpha.SetStartTransparency(0.0f);
    alpha.SetEndTransparency(1.0f);
    alpha.SetDuration(0.6f);
    alpha.SetType(ink.anim.InterpolationType.Sinusoidal);
    alpha.SetMode(ink.anim.InterpolationMode.EasyOut);

    var fade = new ink.anim.Definition();
    fade.AddInterpolator(alpha);

    return widget.PlayAnimation(fade);
}
  • There is an interpolator for most widget properties, for example TransparencyInterpolator, ScaleInterpolator, TranslationInterpolator, RotationInterpolator, SizeInterpolator, MarginInterpolator and ColorInterpolator
  • Durations and delays are in seconds. SetStartDelay holds an interpolator back, so the interpolators of one definition can run one after another
  • SetType and SetMode pick the easing curve. Without them the value changes linearly
  • The proxy can Pause(), Resume() and Stop() the animation

Looping

PlayAnimationWithOptions accepts ink.anim.PlaybackOptions, which make an animation repeat:

static ink.anim.Definition PulseDefinition()
{
    var scale = new ink.anim.ScaleInterpolator();
    scale.SetStartScale(new Vector2(1.0f, 1.0f));
    scale.SetEndScale(new Vector2(1.15f, 1.15f));
    scale.SetDuration(0.3f);

    var pulse = new ink.anim.Definition();
    pulse.AddInterpolator(scale);
    return pulse;
}

var proxy = widget.PlayAnimationWithOptions(
    PulseDefinition(),
    new ink.anim.PlaybackOptions { loopType = ink.anim.LoopType.PingPong, loopCounter = 3 }
);
  • PingPong plays the animation forward and then backward, Cycle starts it over from the beginning
  • loopInfinite = true keeps it going until you call Stop() on the proxy

Reacting to animation events

The proxy reports when its animation starts, finishes, pauses or loops (the values of ink.anim.EventType). It reports them to an object and a function name, like many game functions do, so you hand it a script callback. This fades a widget in and starts a pulse once the fade finished:

static void Show(ink.Widget widget)
{
    var proxy = FadeIn(widget);
    if (proxy is null)
    {
        return;
    }

    var faded = re.ScriptCallbacks.Create(
        (ink.anim.Proxy _) => Pulse(widget),
        owner: widget,
        once: true
    );
    proxy.RegisterToCallback(ink.anim.EventType.OnFinish, faded.Instance, faded.Function);
}
  • The proxy passes itself along, so the handler signature needs an ink.anim.Proxy. A handler with other parameter types is never called
  • once: true ends the callback after the animation finished. owner: widget ends it together with the widget, in case the animation never gets to finish
  • A handler can start the next animation and register the next callback, which is how you chain animations

For an event that fires more than once, keep the callback and dispose it when you no longer need it. This counts the loops of the pulse and reports them when it finished:

static void Pulse(ink.Widget widget)
{
    var proxy = widget.PlayAnimationWithOptions(
        PulseDefinition(),
        new ink.anim.PlaybackOptions { loopType = ink.anim.LoopType.PingPong, loopCounter = 3 }
    );
    if (proxy is null)
    {
        return;
    }

    var loops = 0;
    var looped = re.ScriptCallbacks.Create((ink.anim.Proxy _) => ++loops, owner: widget);
    proxy.RegisterToCallback(ink.anim.EventType.OnEndLoop, looped.Instance, looped.Function);

    var finished = re.ScriptCallbacks.Create(
        (ink.anim.Proxy _) =>
        {
            looped.Dispose();
            Console.WriteLine($"Pulsed {loops} times");
        },
        owner: widget,
        once: true
    );
    proxy.RegisterToCallback(ink.anim.EventType.OnFinish, finished.Instance, finished.Function);
}

For interfaces of your own, a Web UI is usually the better fit. Ink is useful to add small elements that blend in with the game's HUD, or to change the game's own widgets.