Skip to content

Events

An element runs an expression when it receives an event. Use it to turn clicks, keys and Back into calls to your game. on:<event>="..." takes any RmlUi event name (list; click, mouseup, keydown, back, ...), and its expression is a list of calls separated by ;.

  • call(...) calls a function of a view model; callIf(...) calls it only while a condition is true.
  • An event bubbles: it runs the handlers of the element and then of its ancestors. stopPropagation() ends it at the element.
  • back and uiaction (UI action) are events too.
  • data-action="close" and data-action="open:<package path>" are handled by the plugin instead of as view model calls.

The Events sample shows each form; its view model has a LastEvent text that the calls write.

Calls

call('<path>', '<Function>', args...) calls a function of a view model. The path starts with the name of a model the document or its frame declares, as in 'events', from any :model nesting; a longer path such as 'events.Child' or 'events.Children[1]' names a view model held by that model (Nested view models). The function must be:

  • a BlueprintCallable UFUNCTION that is not const, not pure, not a network function and not a BlueprintNativeEvent or BlueprintImplementableEvent. Otherwise nothing is called, the log warns that the function "is not a callable event function", and the validator reports unknown-function.
  • called with exactly as many arguments as it has parameters. Otherwise nothing is called, the log warns that it "received N arguments but takes M", and the validator reports call-arity.

Its return value and out parameters are dropped. A call does not change a field itself; the function does, and the document updates when it broadcasts the field.

Events.rml
<button id="ev-red" class="ev-button" on:click="call('events', 'Pick', 'Red', 1)">One red</button>
<button id="ev-blue" class="ev-button" on:click="call('events', 'Pick', 'Blue', 3)">Three blue</button>
<button id="ev-caller" class="ev-button" on:click="call('events', 'ShowCaller')">Who calls?</button>
Events/ArcRmlSamplesEvents.h
UFUNCTION(BlueprintCallable, Category = "Samples")
void Pick(const FString& Colour, int32 Amount);
Events/ArcRmlSamplesEvents.cpp
void UArcRmlSamplesEventsViewModel::Pick(const FString& Colour, int32 Amount)
{
    UE_MVVM_SET_PROPERTY_VALUE(LastEvent, FText::Format(INVTEXT("Picked {0} x {1}"), Amount, FText::FromString(Colour)));
}

Numeric parameters take numbers and numeric text, and bool, text, name and FText parameters convert. An object parameter takes 'vm:<model>.<path>', the path of a view model reachable from a declared model, as in call('events', 'Select', 'vm:events.Children[1]'). 'target:data-x' passes the data-x attribute of the element that has the handler, as in call('events', 'Choose', 'target:data-id').

Arguments from the element

An argument is an expression, so a call can pass what the element knows: the row and index of a :for (call('lists', 'Remove', index) in Lists), a component prop (Components) and the values of the event, ev.<name>, such as ev.button of a mouse event and ev.key of a key event (callIf, Event reference).

callIf

callIf(condition, '<path>', '<Function>', args...) calls only while the condition is true. A mouse event has ev.button, 0 for the left and 1 for the right button, and a key event has ev.key, the key's name as a browser reports it (ArrowUp, Enter, Escape, a letter in lower case):

Events.rml
<div id="ev-mouse" class="ev-box" on:mouseup="callIf(ev.button == 0, 'events', 'Pointer', 'left'); callIf(ev.button == 1, 'events', 'Pointer', 'right')">Click here with the left or the right mouse button.</div>
Events.rml
<div id="ev-keys" class="ev-box ev-focusable" autofocus="" on:keydown="callIf(ev.key == 'ArrowUp', 'events', 'Step', 1); callIf(ev.key == 'ArrowDown', 'events', 'Step', -1); stopPropagation(ev.key == 'ArrowUp' || ev.key == 'ArrowDown')">Value {{ events.Value }}: focus this box and press Up or Down.</div>

The second box takes keys only while it is focused. Its autofocus attribute focuses it when the document opens. A <button> is a navigation stop by default, but a <div> or <span> is not, so the sample's style sheet makes the box one:

Events.rcss
.ev-focusable {
    tab-index: auto;
    nav: auto;
}

stopPropagation

An event bubbles from the element to its ancestors, and every ancestor with a handler for it runs. stopPropagation() ends that at the element; stopPropagation(condition) ends it only while the condition is true. In the keys box above, a stopped keydown also skips the document's own default for that key, so ArrowUp and ArrowDown do not move focus.

Some events cannot be stopped; the Stop column of the Event reference says which.

In the sample, a click on "Inner, stops" counts only the inner click, and a click on "Inner, bubbles" also reaches the outer element:

Events.rml
<div id="ev-outer" class="ev-box" on:click="call('events', 'Outer')">
    <p>Outer clicks: {{ events.OuterCount }}, inner clicks: {{ events.InnerCount }}</p>
    <div class="ev-row">
        <button id="ev-inner-stop" class="ev-button" on:click="call('events', 'Inner'); stopPropagation()">Inner, stops</button>
        <button id="ev-inner-bubble" class="ev-button" on:click="call('events', 'Inner')">Inner, bubbles</button>
    </div>
</div>

The world context object

A function parameter named WorldContextObject is not an argument: the call fills it with the controller of the local player, and the RML passes nothing for it. A view model function uses it to reach the player and the player's subsystems, such as UArcRmlViewportSubsystem.

The "Who calls?" button of the listing under Calls runs ShowCaller:

Events/ArcRmlSamplesEvents.h
UFUNCTION(BlueprintCallable, Category = "Samples", meta = (WorldContext = "WorldContextObject"))
void ShowCaller(UObject* WorldContextObject);
Events/ArcRmlSamplesEvents.cpp
void UArcRmlSamplesEventsViewModel::ShowCaller(UObject* WorldContextObject)
{
    const UWorld* World = WorldContextObject ? WorldContextObject->GetWorld() : nullptr;
    if (!World)
    {
        return;
    }
    UE_MVVM_SET_PROPERTY_VALUE(LastEvent, FText::Format(INVTEXT("Called by {0} at {1} s"), FText::FromString(WorldContextObject->GetName()), FMath::FloorToInt(World->GetTimeSeconds())));
}

Back

In a document whose input mode is menu or all, a Back press (the Back action of the project's CommonUI input, see Project setup) runs the back event on the focused element and its ancestors. If no handler stops it, the document closes. In a child focus area, focus first returns to the parent area before the event reaches the elements above the area (Back and the parent area). on:back on <body> makes a document handle Back itself. In the sample, the first Back runs ArmBack and stops the event, so the document stays open, and the second Back is not stopped and closes it:

Events.rml
<body :model="events" on:back="callIf(!events.bBackArmed, 'events', 'ArmBack'); stopPropagation(!events.bBackArmed)">
Events/ArcRmlSamplesEvents.cpp
void UArcRmlSamplesEventsViewModel::ArmBack()
{
    UE_MVVM_SET_PROPERTY_VALUE(bBackArmed, true);
    UE_MVVM_SET_PROPERTY_VALUE(LastEvent, INVTEXT("Back pressed: press it again to close"));
}

UI actions

An action of the document's Input Mapping Context (Input and gamepad) is sent to the focused element as a uiaction event, or to <body> when nothing in the document is focused. on:uiaction reads it as ev.action, the action's name even where the document names the action by package path. The action counts as handled only when the handler stops propagation; call and callIf alone do not. The demo's loading screen runs ToggleDevBar for one action and stops only that one:

LoadingScreen.rml
on:uiaction="callIf(ev.action == 'IA_ArcDemo_DevBar', 'ui', 'ToggleDevBar'); stopPropagation(ev.action == 'IA_ArcDemo_DevBar')"

Without the stopPropagation the function still runs, but the action is not consumed: it goes on to the document's data-drag-action, data-hotkey and data-hotkey-cycle elements, and is only logged ("has no handler") when none of them takes it. See Event reference.

<meta name="any-input" content="true"/> (Documents) sends an anyinput event on every key and pad press, with ev.key, ev.device and ev.action. The press keeps its normal route whatever the handler does.

Document actions

data-action="close" closes the document and data-action="open:<package path>" opens another one. They are handled by the plugin and are not view model calls (Documents).

Run it

Play in the map L_ArcRmlSamples and choose Events in the samples list (Samples); the console command is ArcRmlSamples.Open Events.

The Events sample: a last-event line, three call buttons, a click box, a key-step box, an outer box with stopping and bubbling inner buttons, and the Back note

Next