Attach GUI to Sprites Guide
This guide explains how to use theattachToSprite option to attach GUI components directly to sprites in the game world.
Use attached GUI when an interface should follow a sprite and still use GUI
lifecycle and interaction APIs. For passive sprite visuals, use
Sprite Components or
Authoritative Sprite Components.
Overview
By default, GUI components are displayed as fixed overlays on the screen. However, you can attach GUI components to sprites so they follow the spriteβs position in the game world. This is useful for:- Tooltips that appear when hovering over a sprite
- Name tags above characters
- Interactive UI elements attached to specific sprites
- Contextual menus that follow a sprite
- Status indicators that move with the sprite
Basic Usage
To attach a GUI component to sprites, setattachToSprite: true in your GUI configuration:
Creating an Attached GUI Component
Attached GUI components are Canvas Engine components (.ce files) that receive the sprite object as a prop. They are rendered within the spriteβs container, so they automatically follow the spriteβs position.
Example: Simple Tooltip Component
object as a prop automatically, allowing you to access sprite properties:
For more information about DOMContainer, see the Canvas Engine documentation
Controlling Display from Server Side
Attached GUIs are controlled from the server side using theshowAttachedGui() and hideAttachedGui() methods on the player object.
Show Attached GUI
Display the attached GUI for a player:Hide Attached GUI
Hide the attached GUI for a player:Example: Show Tooltip on Hover
Component Props
Attached GUI components receive the following props:object: The sprite object (RpgClientObject) - contains all sprite properties like position, health, name, etc.onFinish: Callback function when the GUI finishes its actiononInteraction: Callback function for GUI interactions
Example: Using Component Props
Example: Using Reactive Signals with Form Elements
DOMContainer supports reactive two-way data binding for form elements using signals:Example: Using Event Handlers
DOMContainer supports all standard DOM events. Use the event name directly (without βonβ prefix):Example: Using CSS Classes
You can apply CSS classes using different formats:Example: Using Inline Styles
You can apply styles using string or object format:Example: Using Forms with Automatic Data Collection
DOMContainer automatically collects form data when a form is submitted:Supported Events
DOMContainer supports all standard DOM events. Use the event name directly (without βonβ prefix):- Mouse events:
click,mouseover,mouseout,mouseenter,mouseleave,mousemove,mouseup,mousedown - Touch events:
touchstart,touchend,touchmove,touchcancel - Keyboard events:
keydown,keyup,keypress - Form events:
submit,reset,change,input,focus,blur - Drag events:
drag,dragend,dragenter,dragleave,dragover,drop,dragstart - Other events:
wheel,scroll,resize,contextmenu,select