UI
UI, a static global class AND an Object class. It is the method to interact with custom UI elements. It allows you to read/write attributes of elements defined in the XML of the UI. It also allows you to receive information from various inputs (like buttons) on-screen and on objects.
Attention
This class allows for the manipulation of UI at runtime. It does NOT modify or fetch the original XML in the editor, but rather what is displayed as it continues to run during a game. Just like with Lua, you can only get/set dynamic values during runtime. You can use onSave and onLoad to record any data you want to persist through save/load/undo.
For more information on how to build UI elements within XML, view the UI API.
Global and Object
UI can either be placed on the screen by using the Global UI or placed on an Object using Object UI. Depending on which you are using, these commands are used differently.
Example of calling a function targeted at the Global UI: UI.getAttributes(id) Example of calling a function targeted at an Object UI: object.UI.getAttributes(id)
Inputs
Input Elements are able to trigger a function. By default, Global UI will trigger a function in Global and Object UI will trigger a function in the Object's script. To change the target script for an input, view more details here.
When creating the input element in XML, you will select the name of the function it activates. Regardless of its name, it always will pass parameters
functionName(player, value, id)
function onButtonClick(player, value, id) print(player.steam_name) print(id) end
Element Function Summary
Element Function Details
getAttribute(...)
Obtains the value of a specified attribute of a UI element. What it returns will typically be a string or a number.
getAttribute(id, attribute)
self.UI.getAttribute("testElement", "fontSize")
getAttributes(...)
Returns the attributes and their values of a UI element. It only returns the attributes (and values) for elements that have had those attributes set by the user.
Return table
IMPORTANT: This return table is an example of one you may get back from using it on a RawImage element type. The attribute keys you get back and their values will depend on the element you use the function on as well as the attributes you, the user, have assigned to it.
getCustomAssets()
Returns information on all custom assets uploaded to the UI ASSETS pane.
Return table
function onLoad() local assets = UI.getCustomAssets() log(assets) end
getValue(...)
Obtains the value between elements tags, like: <Text>ValueObtained</Text>
string = UI.getAttribute("testElement") print(string)
getXmlTable()
Obtain the run-time UI formatted as a Lua table of data.
Example Returned Table:
{ { tag="HorizontalLayout", attributes={ height=200, width=1000, color="rgba(0,0,0,0.7)", }, children={ { tag="Text", attributes={ fontSize=100, color="red", }, value="Example", }, { tag="Text", attributes={ text="Message", fontSize=100, color="blue", }, }, } } }
What the XML would look like which returns that table:
<HorizontalLayout height="200" width="1000" color="rgba(0,0,0,0.7)"> <Text fontSize="100" color="red">Example</Text> <Text text="Message" fontSize="100" color="blue" /> </HorizontalLayout>
hide(...)
Hides the given UI element. Unlike the "active" attribute, hide triggers animations.
self.UI.hide("testElement")
setAttribute(...)
Sets the value of a specified attribute of a UI element.
Important
This will override the run-time value from the XML UI for all players, forcing them to see the same value.
setAttribute(id, attribute, value)
self.UI.setAttribute("testElement", "fontSize", 200)
setAttributes(...)
Updates the value of the supplied attributes of a UI element. You do not need to set every attribute with the data table, an element will continue using any previous values you do not overwrite.
Important
This will override the run-time value from the XML UI for all players, forcing them to see the same value.
setAttributes(id, data)
Example data table
IMPORTANT: This table is an example of one you may use when setting a text UI element. The attribute keys you use and their values will depend on the element you use the function on.
attributeTable = { fontSize = 300, color = "#000000" } self.UI.setAttributes("exampleText", attributeTable)
setCustomAssets(...)
Sets the UI ASSETS (like custom images) for Global or an Object. Passing nothing as a parameter results in the clearing of the UI Assets.
This function will overwrite any currently existing assets in Custom UI Assets, not add to them.
setCustomAssets(table)
function onLoad() local assets = { { name = "Image 1", url = "http://placehold.it/120x120&text=image1" }, { name = "Image 2", url = "http://placehold.it/120x120&text=image2" }, } UI.setCustomAssets(assets) end
setValue(...)
Updates the value between elements tags, like: <Text>ValueChanged</Text>
setValue(id, value)
UI.setValue("testElement", "New Text To Display")
setXml(...)
Replaces the run-time UI with the XML string.
self.UI.setXml("<Text>Test</Text>")
Warning
setXml takes 1 frame to update the runtime UI. This means any change or get of xml/attributes during this frame will not be recognized correctly.
setXmlTable(...)
Replaces the run-time UI with an XML string which is generated from a table of data.
setXmlTable(data)
- data: A table containing sub-tables. One sub-table for each element being created.
- tag: The element type.
- attributes: A table containing attribute names for keys. Available attribute types depend on tag's element type.
- Optional, defaults to not being used.
- Example key/value pairs: text="Test", color="black"
- value: Text that appears
<Text>Here</Text>
, between the<>
and</>
.- Optional, defaults to an empty string.
- children: A table containing more sub-tables, formatted as above. This does mean the sub-tables can contain their own children as well, containing sub-sub tables, etc.
- Optional, defaults to not being used.
function onLoad() UI.setXmlTable({ { tag="HorizontalLayout", attributes={ height=200, width=1000, color="rgba(0,0,0,0.7)", }, children={ { tag="Text", attributes={ fontSize=100, color="red", }, value="Example", }, { tag="Text", attributes={ text="Message", fontSize=100, color="blue", }, }, } } }) end
Warning
setXmlTable takes 1 frame to update the runtime UI. This means any change or get of xml/attributes during this frame will not be recognized correctly.
show(...)
Shows the given UI element. Unlike the "active" attribute, show triggers animations.
self.UI.show("testElement")