Designing a game's UI¶
The other pages explain one feature each. This page puts them together into the UI of a whole game: which layer each screen goes on, who opens it, where its data comes from and how the screens share styles and parts. It describes a pattern your game writes itself; the plugin gives you the parts.
Layer map¶
The default Layer Order has four layers (Documents). Each layer is a CommonUI widget stack: it shows only its top document, and closing that one shows the one below again.
| Layer | Put there | input-mode |
hide-lower |
Why |
|---|---|---|---|---|
UI.Layer.Game |
The HUD | game |
false |
The game keeps the keys and the mouse. Nothing is below it to hide. |
UI.Layer.GameMenu |
In-game overlays: a map, an inventory | menu |
true, or false to keep the HUD in view |
The overlay takes the pad and keys while it is open. |
UI.Layer.Menu |
The pause menu, settings, other full screens | menu |
true |
A full screen hides the HUD and the overlays behind it. |
UI.Layer.Modal |
Dialogs | menu |
false |
The screen that asked stays visible behind the dialog. |
- Keep one document per layer where you can. Two HUD documents on
UI.Layer.Gamedo not show together: only the top one shows. - A HUD that needs keys of its own, such as one that opens the pause menu, takes one of two input modes:
game: game code keeps the key.input-mappingis not allowed withgame.allwith aninput-mapping: the document handles the key. Analldocument closes on Back, so such a HUD stopsbackon its<body>(Events).- The plugin does not pause the game. Pause it from your own code when the pause menu opens and closes.
Who opens screens¶
Give the game one owner of its screens: a ULocalPlayerSubsystem of your own, or the PlayerController. It calls
UArcRmlViewportSubsystem::OpenDocument and CloseDocument (Documents) and listens to
OnDocumentClosed. A sketch:
#pragma once
#include "CoreMinimal.h"
#include "ArcRmlDocumentRef.h"
#include "Host/ArcRmlCloseReason.h"
#include "Subsystems/LocalPlayerSubsystem.h"
#include "MyUISubsystem.generated.h"
UCLASS()
class UMyUISubsystem : public ULocalPlayerSubsystem
{
GENERATED_BODY()
public:
virtual void Initialize(FSubsystemCollectionBase& Collection) override;
void OpenPauseMenu();
private:
UFUNCTION()
void HandleDocumentClosed(const FArcRmlDocumentRef& Document, EArcRmlCloseReason Reason);
void ResumeGame();
};
#include "MyUISubsystem.h"
#include "Engine/LocalPlayer.h"
#include "Host/ArcRmlViewportSubsystem.h"
#include "MyPauseViewModel.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(MyUISubsystem)
namespace MyUISubsystem
{
const FArcRmlDocumentRef PauseMenu{TEXT("/Game/UI/Rml/Screens/PauseMenu/PauseMenu.rml")};
}
void UMyUISubsystem::Initialize(FSubsystemCollectionBase& Collection)
{
Super::Initialize(Collection);
UArcRmlViewportSubsystem* Viewport = Collection.InitializeDependency<UArcRmlViewportSubsystem>();
Viewport->OnDocumentClosed.AddDynamic(this, &ThisClass::HandleDocumentClosed);
}
void UMyUISubsystem::OpenPauseMenu()
{
UArcRmlViewportSubsystem* Viewport = GetLocalPlayer()->GetSubsystem<UArcRmlViewportSubsystem>();
TWeakObjectPtr<UArcRmlViewportSubsystem> WeakViewport(Viewport);
Viewport->OpenDocument(MyUISubsystem::PauseMenu, [WeakViewport](const FArcRmlDocumentRef& Opened)
{
UArcRmlViewportSubsystem* Subsystem = WeakViewport.Get();
UMyPauseViewModel* Model = Subsystem ? Cast<UMyPauseViewModel>(Subsystem->GetViewModel(Opened, TEXT("pause"))) : nullptr;
if (Model)
{
Model->Refresh();
}
});
}
void UMyUISubsystem::HandleDocumentClosed(const FArcRmlDocumentRef& Document, EArcRmlCloseReason Reason)
{
if (Document == MyUISubsystem::PauseMenu && Reason != EArcRmlCloseReason::WorldCleanup)
{
ResumeGame();
}
}
void UMyUISubsystem::ResumeGame()
{
// Unpause the game here.
}
Besides Core, CoreUObject and Engine, the game module's Build.cs lists ArcRmlUI (the viewport subsystem and the
close reason), ArcRmlUIData (FArcRmlDocumentRef) and ModelViewViewModel (the base class of UMyPauseViewModel) in its
dependencies.
This is a sketch: it assumes your own UMyPauseViewModel with a Refresh() function.
A PlayerController Blueprint of your game builds the same pattern from these nodes. The sketch assumes your own
MyPauseViewModel with a BlueprintCallable Refresh function.
- Event BeginPlay, then Arc Rml Viewport Subsystem (in the palette: Get ArcRmlViewportSubsystem) with
PlayerControllerSelf, then Bind Event to On Document Closed on it, wired to a custom eventHandleDocumentClosedwith the inputsDocumentandReason. - A custom event
OpenPauseMenu: Make Arc Rml Document Ref withPath/Game/UI/Rml/Screens/PauseMenu/PauseMenu.rml, then Open Document for Player with Owning PlayerSelfand thatDocument. - From its Before Activate pin: Get View Model on the subsystem with the pin's
DocumentandModelNamepause, then Cast To MyPauseViewModel, then itsRefresh. -
In
HandleDocumentClosed: Break Arc Rml Document Ref onDocument; when itsPathequals the pause menu path andReasonis notWorldCleanup, a Branch resumes the game onTrue. -
FArcRmlDocumentRefhas no Blueprint equality node, so the graph compares thePathstrings.
- The
Initfunction, and theBeforeActivatepin ofOpenDocumentForPlayer, run on every open that activates the document, after its view models exist and before it shows, soGetViewModelreturns them there.AfterActivatefires once the open has succeeded. OnDocumentClosedalso fires when a keep-alive document is parked. The reason tells how it closed:Back,DocumentAction(data-action="close"),CloseKey,Apiand the others in Documents.
A screen's RML can open the next screen itself with data-action="open:<package path>", a package path. That is enough
when the next screen needs nothing from the screen that opens it: its view models come from the sources create,
context, resolver or path, and nothing waits for its answer. Pause menu to
settings is such a step. Open the screen from game code when:
- the open starts in gameplay: a key in
gameinput mode, an event, a trigger; - the screen needs setup per open, such as the item a vendor shows (
InitorSetViewModel); - something waits for the answer, as a confirm dialog's caller does;
- the open has a side effect in the game, such as pausing.
A view model function that a click calls can open a screen too: a parameter named WorldContextObject gets the player
controller (Events), from which it reaches your subsystem.
Choosing a view model source¶
View models has the rules of each source.
| I need | Use |
|---|---|
State of this screen only, made fresh on each open (kept while a keep-alive screen is parked) |
create |
| Data the whole game shares, owned by game code and alive across screens | context |
| An instance my C++ makes or finds for the player when the document opens | resolver |
| An object game code hands over per open, such as a dialog's message | manual with SetViewModel |
| A model my player controller or pawn already holds as a property | path |
| To swap the instance while the screen is open | settable on any source |
| To run code when the screen closes, such as answering a pending callback | on-close |
A typical flow¶
| Step | Layer | Opened by | Back |
|---|---|---|---|
| HUD | UI.Layer.Game |
Your subsystem, when the player is ready | Nothing: game mode takes no Back |
| Pause menu | UI.Layer.Menu |
Your subsystem, from the pause key | Closes the pause menu; the HUD shows again |
| Settings | UI.Layer.Menu |
data-action="open:..." in the pause menu |
Closes settings; the pause menu shows again |
| Confirm dialog | UI.Layer.Modal |
Game code, with a manual model |
Closes the dialog with reason Back: treat it as cancel |
- The pause menu hides the HUD with
hide-lower. Settings opens on the same layer, so the stack shows only settings, and Back returns to the pause menu. - The dialog keeps settings visible with
hide-lowerfalse, and Back reaches it first because its layer is on top. Its model'son-closecan answer the caller when the dialog closes without a choice. - The pause menu, settings and an inventory are the screens a player opens again and again. Make them
keep-alive, so a reopen keeps scroll and state, and preload them withPreloadDocumentso the first open shows at once (Keep-alive and preload). A screen opened once, such as credits, needs neither.
Tabs or separate documents¶
A frame with tabs (Frames and tabs) fits one screen with sections under a shared header: an
inventory, a character sheet and a quest log in one game menu. A visited tab keeps its state until the frame closes, and
the frame's head decides the layer, input mode and keep-alive for all of them.
Separate documents fit screens that need their own layer, input mode or keep-alive, and screens opened from several
places on their own.
Two things to watch with tabs:
- A tab is isolated from its frame: frame rules and custom properties do not reach into it, so every tab links the shared style sheets itself.
- A tab may not declare a view model name that a kept tab declares differently, and a frame cannot have a frame
(Frames and tabs). A
:ifthat rebuilds thearc-embedelement drops the kept tabs (Limits).
Shared parts¶
- Components. A button, a prompt or a list row written once and imported with
dc-importby package path (Components). A component file holds markup only. Its styles go in a style sheet of the importing document, so keep them in one shared sheet; an.rcssfile with the component's name next to it (Card.rcss) is linked into every document that imports it. - One tokens sheet. Colours, spacing and fonts as custom properties on
:root, in one sheet linked from every document and every tab (Styling). The fonts themselves come from the Font Sheets setting (Project setup). - Key hints.
<kbd action="confirm"/>,<kbd action="back"/>and<kbd action="/Game/Input/IA_Jump"/>show the key of the current input device (Input and gamepad), so one prompt serves keyboard and pad. - Input per document. Each screen's own verbs go in its own mapping context, named by
input-mapping; Confirm and Back stay in one project-wide context (Input and gamepad).
Folder layout¶
A game's UI lives under UI/Rml/ of a content root (Project setup). One layout:
Content/UI/Rml/
Fonts/ Fonts.rcss and the font files
Shared/ Tokens.rcss and the shared component styles
Components/ one .rml per component
Images/ images and icons the documents name
Screens/
Hud/ Hud.rml, Hud.rcss
PauseMenu/ PauseMenu.rml, PauseMenu.rcss
GameMenu/ the frame and its tabs
The samples keep one folder per screen, with a Shared and a Fonts folder beside them; the demo keeps its screens
in one folder with Components and the hub's tabs in subfolders. Screens do not go under UI/Rml/Test/; see Packaging.
More than one local player¶
UArcRmlViewportSubsystem is a local player subsystem, so each local player has its own documents, layers and
stack, and your subsystem should be per local player too. For split-screen, see Limits. The ArcRml.*
console commands act on the first local player. Details: Screen size, cursor and local players.
Before you ship¶
- Validate every document on the build machine with
-run=ArcRmlValidate -WarningsAsErrors(Validation). It finds missing view model fields, images, fonts and localization keys, and a menu document whose Back action has no key. - Walk every screen with the pad only: each one takes focus, every control is reachable by arrow (Navigation), and Back leaves it.
- Run Sync Strings on your documents, gather and compile the texts, and list the cultures to stage (Localization).
- Decide which screens are
keep-alive: a parked document stays until a discard, a reload or the end of the player's world. Discard a parked screen you will not open again withDiscardDocument(Keep-alive and preload). - With Package Everything off, list in Paths to Package the documents your C++ opens by path. The cook adds
frames, literal
open:targets and documents that Blueprints and data assets reference, and a path that only your C++ names is none of those (Project setup).