GetRootElement: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(7 intermediate revisions by 5 users not shown)
Line 4: Line 4:


It is often used to attach handler functions to events triggered for any element, or also to make a scripting function affect all elements.
It is often used to attach handler functions to events triggered for any element, or also to make a scripting function affect all elements.
 
{{Note|All resources have a [[Predefined_variables_list|predefined global variable]] called ''root'' that has the root element as value. The variable exists server side as well as client side.}}
Note: all resources have a predefined global variable called "root" that has the root element as value. This means you can simply use "root" instead of getRootElement(). This variable exists server side as well as client side.


==Syntax==
==Syntax==
Line 18: Line 17:
This example will output the number of loaded resources by counting ''resource'' elements that are children of the ''root'' node.
This example will output the number of loaded resources by counting ''resource'' elements that are children of the ''root'' node.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local root = getRootElement()
--By default, predefined variable 'root' is getRootElement()
local rootChildren = getElementChildren( root )
local rootChildren = getElementChildren( root )


Line 33: Line 32:
==See Also==
==See Also==
{{Element functions}}
{{Element functions}}
[[hu:getRootElement]]
[[ru:getRootElement]]

Latest revision as of 16:52, 18 July 2018

This function returns the root node of the element tree, called root. This node contains every other element: all resource root elements, players and remote clients. It is never destroyed and cannot be destroyed using destroyElement.

It is often used to attach handler functions to events triggered for any element, or also to make a scripting function affect all elements.

[[{{{image}}}|link=|]] Note: All resources have a predefined global variable called root that has the root element as value. The variable exists server side as well as client side.

Syntax

element getRootElement ( )

Returns

Returns the root element.

Example

This example will output the number of loaded resources by counting resource elements that are children of the root node.

--By default, predefined variable 'root' is getRootElement()
local rootChildren = getElementChildren( root )

local resourceCount = 0
for k, child in ipairs( rootChildren ) do
	if getElementType( child ) == "resource" then
		resourceCount = resourceCount + 1
	end
end

outputChatBox( "There are " .. resourceCount .. " loaded resources." )

See Also