Skip to main content
Version: bleeding-edge 🩸

🖥️ WebUI

Class for spawning a dynamic Web Browser.

👪Inheritance
This class shares methods and events from Base Entity.

tip

Our WebUI implementation is built using the last versions of Chromium Embedded Framework.

tip

To access the in-browser remote debugging head over to http://localhost:9222 when you are connected to a server.

Proprietary Codecs

Proprietary Codecs like MPEG-4 (.mp4), H.264, H.265 and AAC are not supported on public CEF builds. We recommend converting your media files to WEBM or OGG.

🎒 Examples​

Client/Index.lua
-- Loading a local file
local my_ui = WebUI(
"Awesome UI", -- Name
"file://UI/index.html", -- Path relative to this package (Client/)
WidgetVisibility.Visible -- Is Visible on Screen
)

-- Loading a Web URL
local my_browser = WebUI(
"Awesome Site", -- Name
"https://nanos-world.com", -- Web's URL
WidgetVisibility.Visible -- Is Visible on Screen
)

-- Loading a local file from other package
local my_ui = WebUI(
"Awesome Other UI", -- Name
"file://other-package/Client/UI/index.html",
WidgetVisibility.Visible -- Is Visible on Screen
)

Using a WebUI as Mesh Material​

Client/Index.lua
-- Spawns a WebUI hidden from the screen, not transparent, not auto resized and with size of 500x500
local my_ui = WebUI("Awesome Site", "https://nanos-world.com", WidgetVisibility.Hidden, false, false, 500, 500)

-- Spawns a StaticMesh (can be any mesh)
local static_mesh = StaticMesh(Vector(0, 0, 100), Rotator(), "nanos-world::SM_Cube")

-- Sets the mesh material to use the WebUI
static_mesh:SetMaterialFromWebUI(my_ui)

Communicating between Lua and JS (WebUI)​

Client/Index.lua
local my_ui = WebUI("Awesome UI", "file://UI/index.html")

local param1 = 123
local param2 = "hello"

-- Calls a JS event
my_ui:CallEvent("MyEvent", param1, param2)

-- Subscribes to receive JS events
my_ui:Subscribe("MyAnswer", function(param1)
Console.Log("Received back! %s", param1)
-- Will output 'Received back! Hey there!'
end)
script.js
// Register for "MyEvent" from Lua
Events.Subscribe("MyEvent", function(param1, param2) {
console.log("Triggered! " + param1 + " " + param2);
// Will output 'Triggered! 123 hello'

// Triggers "MyAnswer" on Lua
Events.Call("MyAnswer", "Hey there!");
});

// It is also possible to unsubscribe from an event to make it stop triggering
Events.Unsubscribe("MyEvent");

Pretty Scroll Bar​

Since we migrated from Webkit to CEF, some scrollbars got ugly. Here's a small CSS snippet to make them almost like the Webkit ones:

::-webkit-scrollbar {
width: 6px;
}

::-webkit-scrollbar-thumb {
border-radius: 10px;
background-color: #494949;
}

body {
scrollbar-gutter: stable both-edges;
}

More related examples:

User Interfacecore-concepts/scripting/user-interface Basic HUD (HTML)getting-started/tutorials-and-examples/basic-hud-html
tip

You can also use a Canvas as a mesh material, with the :SetMaterialFromCanvas() method!

📚 Libraries & Frameworks​

Here a list of Community Created Libraries & Frameworks making use of WebUIs expanding it's possibilities:

🛠 Constructors​

Default Constructor​

No description provided

local my_webui = WebUI(name, path, visibility?, is_transparent?, auto_resize?, width?, height?)

Parameters

TypeNameDefaultDescription
stringname Required parameter Used for debugging logs
HTML Pathpath Required parameter Web URL or HTML File Path as file://my_file.html
WidgetVisibilityvisibilityWidgetVisibility.Visibleif WebUI is visible on screen
booleanis_transparenttrueif WebUI background is transparent
booleanauto_resizetrueif should auto resize when screen changes its size (useful OFF when you are painting meshes with WebUI)
integerwidth0size of the WebUI width when you are not using auto_resize
integerheight0size of the WebUI height when you are not using auto_resize

🔍 HTML Path Searchers​

Loading a .html file supports the following searchers, which are looked in the following order:

  1. Relative to current-file-path/
  2. Relative to current-package/Client/
  3. Relative to current-package/
  4. Relative to Packages/

🗿 Static Functions​

Inherited Entity Static Functions
WebUI inherits from Base Entity Class, sharing it's methods and functions:
Base Entityscripting-reference/classes/base-classes/entity
ReturnsNameDescription
table of Base EntityGetAllReturns a table containing all Entities of the class this is called on
Base EntityGetByIndexReturns a specific Entity of this class at an index
integerGetCountReturns how many Entities of this class exist
table of tableGetInheritedClassesGets a list of all directly inherited classes from this Class created with the Inheriting System
iteratorGetPairsReturns an iterator with all Entities of this class to be used with pairs()
table or nilGetParentClassGets the parent class if this Class was created with the Inheriting System
tableInheritInherits this class with the Inheriting System
booleanIsChildOfGets if this Class is child of another class if this Class was created with the Inheriting System
functionSubscribeSubscribes to an Event for all entities of this Class
functionSubscribeRemoteSubscribes to a custom event called from server
UnsubscribeUnsubscribes all callbacks from this Event in this Class within this Package, or only the callback passed

This class doesn't have own static functions.

🦠 Functions​

Inherited Entity Functions
WebUI inherits from Base Entity Class, sharing it's methods and functions:
Base Entityscripting-reference/classes/base-classes/entity
ReturnsNameDescription
BroadcastRemoteEventCalls a custom remote event directly on this entity to all Players
BroadcastRemoteInRadiusEventCalls a custom remote event directly on this entity to all Players in a radius
CallRemoteEventCalls a custom remote event directly on this entity to a specific Player
CallRemoteEventCalls a custom remote event directly on this entity
CallRemotePlayersEventCalls a custom remote event directly on this entity to a list of Players
DestroyDestroys this Entity
FinishSpawnFinish the spawning process and send the entity to clients if it was spawned with a deferred spawn_mode
table of stringGetAllValuesKeysGets a list of all values keys
tableGetClassGets the class of this entity
integerGetIDGets the universal network ID of this Entity (same on both client and server)
anyGetValueGets a Value stored on this Entity at the given key
booleanHasAuthorityGets if the local context has authority over this Entity (true if spawned by the client, false if spawned by the server)
booleanIsARecursively checks if this entity is inherited from a Class
booleanIsBeingDestroyedReturns true if this Entity is being destroyed
booleanIsSpawnedGets if this Entity finished spawning
booleanIsValidReturns true if this Entity is valid (i.e. wasn't destroyed and points to a valid Entity)
SetSpawnModeOverrides when this Entity finishes spawning, from the Constructor of an Inherited Class
SetValueSets a Value in this Entity
functionSubscribeSubscribes to an Event on this specific entity
functionSubscribeRemoteSubscribes to a custom event called from server on this specific entity
UnsubscribeUnsubscribes all callbacks from this Event in this Entity within this Package, or only the callback passed
ReturnsNameDescription
BringToFrontPuts this WebUI in the front of all WebUIs and Widgets
CallEventCalls an Event on the Browser's JavaScript
CloseDevToolsCloses the Developer Tools for this WebUI, it only works if the Client.SetDebugEnabled() not disabled
ExecuteJavaScriptExecutes a JavaScript code in the Browser
stringGetNameGets this WebUI name
Vector2DGetSizeGets the current size of this WebUI
WidgetVisibilityGetVisibilityReturns the current WebUI visibility
booleanHasNodeFocusReturns if this WebUI has any DOM Node focused
booleanIsFrozenReturns if this WebUI is currently frozen
booleanIsReadyReturns if this WebUI is ready
LoadHTMLLoads a pure HTML in this Browser
LoadURLLoads a new File/URL in this Browser
OpenDevToolsOpens the Developer Tools for this WebUI, it only works if the Client.SetDebugEnabled() was not disabled
RemoveFocusRemoves the focus from this WebUI (and sets it back to game viewport)
SendKeyEventSends a Key Event into the WebUI programmatically
SendMouseClickEventSends a Mouse Click into the WebUI programmatically
SendMouseMoveEventSends a Mouse Move Event into the WebUI programmatically
SendMouseWheelEventSends a Mouse Event into the WebUI programmatically
SetFocusEnables the focus on this browser (i.e. can receive Keyboard input and will trigger input events)
SetFreezeFreezes the WebUI rendering to the surface (JavaScript keeps running)
SetLayoutSets the Layout as Canvas on Screen
SetVisibilitySets the visibility in screen
SoundSpawnSoundSpawns a Sound entity that plays this WebUI audio

Optimal

BringToFront​

Puts this WebUI in the front of all WebUIs and Widgets
my_webui:BringToFront()

Moderate

CallEvent​

Calls an Event on the Browser's JavaScript
my_webui:CallEvent(event_name, args...)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter The Event Name to trigger the event
anyargs... Required parameter Arguments to pass to the event

Optimal

CloseDevTools​

Closes the Developer Tools for this WebUI, it only works if the Client.SetDebugEnabled() not disabled
my_webui:CloseDevTools()

Optimal

ExecuteJavaScript​

Executes a JavaScript code in the Browser
Note: This method is experimental and should be used cautiously. Events are still the preferred way of communicating between Packages and WebUI.
my_webui:ExecuteJavaScript(javascript_code)

Parameters

TypeParameterDefaultDescription
stringjavascript_code Required parameter The JavaScript code to run

Optimal

GetName​

Gets this WebUI name
local name = my_webui:GetName()

Returns

TypeDescription
stringThis WebUI name

Optimal

GetSize​

Gets the current size of this WebUI
local ret = my_webui:GetSize()

Returns

TypeDescription
Vector2DCurrent size of this WebUI

Optimal

GetVisibility​

Returns the current WebUI visibility
local visibility = my_webui:GetVisibility()

Returns

TypeDescription
WidgetVisibilityCurrent WebUI visibility

See also SetVisibility.


Optimal

HasNodeFocus​

Returns if this WebUI has any DOM Node focused
local ret = my_webui:HasNodeFocus()

Returns

TypeDescription
booleanWhether any DOM node is focused

Optimal

IsFrozen​

Returns if this WebUI is currently frozen
local freeze = my_webui:IsFrozen()

Returns

TypeDescription
booleanWhether this WebUI is currently frozen

Optimal

IsReady​

Returns if this WebUI is ready
local ready = my_webui:IsReady()

Returns

TypeDescription
booleanWhether this WebUI is ready

Optimal

LoadHTML​

Loads a pure HTML in this Browser
my_webui:LoadHTML(html)

Parameters

TypeParameterDefaultDescription
stringhtml Required parameter The HTML content to load

Optimal

LoadURL​

Loads a new File/URL in this Browser
my_webui:LoadURL(url)

Parameters

TypeParameterDefaultDescription
HTML Pathurl Required parameter The URL or package file to load, e.g. file://UI/index.html or https://...

Moderate

OpenDevTools​

Opens the Developer Tools for this WebUI, it only works if the Client.SetDebugEnabled() was not disabled
my_webui:OpenDevTools()

Optimal

RemoveFocus​

Removes the focus from this WebUI (and sets it back to game viewport)
You MUST call this after you don't need keyboard input anymore
my_webui:RemoveFocus()

Optimal

SendKeyEvent​

Sends a Key Event into the WebUI programmatically
my_webui:SendKeyEvent(key_type, key_code, modifiers?)

Parameters

TypeParameterDefaultDescription
WebUIKeyTypekey_type Required parameter Whether it is a key down, key up or char event
integerkey_code Required parameter The JavaScript key code
WebUIModifiermodifiers?WebUIModifier.None
Supports several modifiers separating by | (using bit-wise operations)

Optimal

SendMouseClickEvent​

You must send both Down and Up to make it work properly
my_webui:SendMouseClickEvent(mouse_x, mouse_y, mouse_type, is_mouse_up, modifiers?, click_count?)

Parameters

TypeParameterDefaultDescription
integermouse_x Required parameter Position X of the mouse
integermouse_y Required parameter Position Y of the mouse
WebUIMouseTypemouse_type Required parameter Which mouse button
booleanis_mouse_up Required parameter Whether the event was up or down
WebUIModifiermodifiers?WebUIModifier.None
Supports several modifiers separating by | (using bit-wise operations)
integerclick_count?1
Use 2 for double click event

Optimal

SendMouseMoveEvent​

Sends a Mouse Move Event into the WebUI programmatically
my_webui:SendMouseMoveEvent(mouse_x, mouse_y, modifiers?, mouse_leave?)

Parameters

TypeParameterDefaultDescription
integermouse_x Required parameter Position X of the mouse
integermouse_y Required parameter Position Y of the mouse
WebUIModifiermodifiers?WebUIModifier.None
Supports several modifiers separating by | (using bit-wise operations)
booleanmouse_leave?false
Whether the mouse left the WebUI

Optimal

SendMouseWheelEvent​

Sends a Mouse Event into the WebUI programmatically
my_webui:SendMouseWheelEvent(mouse_x, mouse_y, delta_x, delta_y)

Parameters

TypeParameterDefaultDescription
integermouse_x Required parameter Position X of the mouse
integermouse_y Required parameter Position Y of the mouse
floatdelta_x Required parameter Horizontal scroll amount
floatdelta_y Required parameter Vertical scroll amount

Optimal

SetFocus​

Enables the focus on this browser (i.e. can receive Keyboard input and will trigger input events)
Note: Only one browser can have focus per time.
my_webui:SetFocus()

Optimal

SetFreeze​

Freezes the WebUI rendering to the surface (JavaScript keeps running)
my_webui:SetFreeze(freeze)

Parameters

TypeParameterDefaultDescription
booleanfreeze Required parameter Whether to freeze the rendering

Optimal

SetLayout​

Sets the Layout as Canvas on Screen. Anchors:

my_webui:SetLayout(location?, size?, anchors_min?, anchors_max?, alignment?)

Parameters

TypeParameterDefaultDescription
Vector2Dlocation?Vector2D(0, 0)
Screen location, or the left and top offsets when the anchors are stretched
Vector2Dsize?Vector2D(0, 0)
Size, or the right and bottom offsets when the anchors are stretched
Vector2Danchors_min?Vector2D(0, 0)
Min anchor, from (0, 0) top-left to (1, 1) bottom-right
Vector2Danchors_max?Vector2D(1, 1)
Max anchor, from (0, 0) top-left to (1, 1) bottom-right
Vector2Dalignment?Vector2D(0.5, 0.5)
Pivot of the WebUI, (0.5, 0.5) is the center

Optimal

SetVisibility​

Sets the visibility in screen
my_webui:SetVisibility(visibility)

Parameters

TypeParameterDefaultDescription
WidgetVisibilityvisibility Required parameter The new visibility in screen

See also GetVisibility.


Optimal

SpawnSound​

Spawns a Sound entity that plays this WebUI audio
local ret = my_webui:SpawnSound(location?, is_2d?, volume?, inner_radius?, falloff_distance?, attenuation_function?)

Parameters

TypeParameterDefaultDescription
Vectorlocation?Vector(0, 0, 0)
World location of the Sound, if 3D
booleanis_2d?true
Whether the Sound is 2D
floatvolume?1.0
Volume multiplier
integerinner_radius?400
Radius within which the volume is 100%, if 3D
integerfalloff_distance?3600
Distance at which the sound becomes inaudible, if 3D
AttenuationFunctionattenuation_function?AttenuationFunction.Linear
How the volume fades with distance, if 3D

Returns

TypeDescription
SoundThe spawned Sound

🚀 Events​

Inherited Entity Events
WebUI inherits from Base Entity Class, sharing it's events:
Base Entityscripting-reference/classes/base-classes/entity
NameDescription
ClassRegisterTriggered when a new Class is registered with the Inheriting System
DestroyTriggered when an Entity is destroyed
SpawnTriggered when an Entity is spawned/created
ValueChangeTriggered when an Entity has a value changed with :SetValue()
NameDescription
FailTriggered when this page fails to load
ReadyTriggered when this page is fully loaded

Fail​

Triggered when this page fails to load
WebUI.Subscribe("Fail", function(self, error_code, message)
-- Fail was called
end)

Arguments

TypeArgumentDescription
WebUIselfThe Web UI that triggered the event
integererror_codeThe CEF error code
stringmessageThe error message

Ready​

Triggered when this page is fully loaded
WebUI.Subscribe("Ready", function(self)
-- Ready was called
end)

Arguments

TypeArgumentDescription
WebUIselfThe Web UI that triggered the event