Browser
A web page drawn over the game: events between the page and Lua, showing, moving, resizing, focus.
Pages are opened with createBrowser. The page's side of the events is window.gmp. In the examples page is any Browser.
Arguments between Lua and the page are converted like JSON: numbers, strings, booleans and tables go
both ways (a table with keys 1..n becomes an array, any other table an object); nil / null is
nil; functions and other Lua values cannot be sent.
local page = createBrowser("login/index.html", 0, 0, getScreenSize())
page:on("ready", function() -- the page loaded and registered its gmp.on handlers
page:trigger("setNickname", getLocalPlayer():getName())
end)
page:on("login", function(data) -- gmp.trigger("login", { user: "...", password: "..." })
triggerServerEvent("login", data.user, data.password)
end)Methods
trigger
page:trigger(name, ...)Calls the page's gmp.on(name, fn) handlers with the arguments. Handlers the page registers later do
not get it - send data after the page said it is ready (gmp.trigger("ready") -> browser:on("ready")).
hud:trigger("stats", { health = 120, maxHealth = 200, gold = 35 })| Parameter | Type | Description |
|---|---|---|
name | string | Event name (1-64 characters). |
... | any | Arguments (converted to JSON, at most 4 MB together). |
Returns: boolean - false when the browser was destroyed.
on
page:on(name, handler)Registers a function called when the page calls gmp.trigger(name, ...); it gets the page's arguments.
Several functions for one name are called in the order of registration.
page:on("buy", function(item, amount) -- gmp.trigger("buy", "ItMi_Gold", 5)
triggerServerEvent("shopBuy", item, amount)
end)| Parameter | Type | Description |
|---|---|---|
name | string | Event name (1-64 characters). |
handler | function | Function called with the page's arguments. |
Returns: boolean - false when the browser was destroyed.
off
page:off(name, handler)Removes a function registered with browser:on.
| Parameter | Type | Description |
|---|---|---|
name | string | Event name. |
handler | function | The registered function. |
Returns: boolean - false when it was not registered.
execute
page:execute(code)Runs JavaScript code in the page (in its global scope, without a result). Usually browser:trigger is
the cleaner way; this is for quick changes.
page:execute("document.body.style.opacity = '0.5'")| Parameter | Type | Description |
|---|---|---|
code | string | JavaScript code (at most 4 MB). |
Returns: boolean - false when the browser was destroyed.
setVisible
page:setVisible(visible)Shows or hides the page. A hidden page does not render (no CPU time) and gets no clicks or keys; it keeps its state (form contents, JavaScript variables).
| Parameter | Type | Description |
|---|---|---|
visible | boolean |
Returns: boolean - false when the browser was destroyed.
isVisible
page:isVisible()Whether the page is shown (see browser:setVisible).
Returns: boolean - false also when the browser was destroyed.
setPosition
page:setPosition(x, y)Moves the page; CSS pixels of a 1920x1080 screen.
| Parameter | Type | Description |
|---|---|---|
x | integer | Left edge. |
y | integer | Top edge. |
Returns: boolean - false when the browser was destroyed.
getPosition
page:getPosition()Position of the page (CSS px).
Returns:
| Value | Type | Description |
|---|---|---|
x | integer | nil | nil when the browser was destroyed. |
y | integer |
setSize
page:setSize(width, height)Changes the size of the page (CSS px, 1-8192); the page lays itself out again (CSS media queries, resize events).
| Parameter | Type | Description |
|---|---|---|
width | integer | |
height | integer |
Returns: boolean - false when the browser was destroyed.
getSize
page:getSize()Size of the page (CSS px).
Returns:
| Value | Type | Description |
|---|---|---|
width | integer | nil | nil when the browser was destroyed. |
height | integer |
setOrder
page:setOrder(order)Sets which page is on top where pages overlap: higher numbers are drawn over lower ones and get the
clicks first. Default 100; the chat is 50 (below the pages of scripts).
| Parameter | Type | Description |
|---|---|---|
order | integer | 0-1000. |
Returns: boolean - false when the browser was destroyed.
loadPage
page:loadPage(path)Loads another page of the server's ui folder into this browser (its browser:on handlers stay).
| Parameter | Type | Description |
|---|---|---|
path | string | Path of the page file in the server's ui folder. |
Returns: boolean - false when the browser was destroyed.
focus
page:focus()Gives the keyboard to this page while the cursor is shown (showCursor(true)) - like a click on it, so
the player can type into a focused text field at once.
showCursor(true)
login:focus() -- the page's <input autofocus> gets the keys without a clickReturns: boolean - false when the browser was destroyed.
destroy
page:destroy()Closes the page. The scripts' pages are also closed when the scripts stop (leaving the server).
Returns: boolean - false when it was already destroyed.
isValid
page:isValid()Whether the browser still exists.
Returns: boolean
