GetRootElement: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Updated description, replaced example)
Line 1: Line 1:
__NOTOC__
__NOTOC__
This function returns the root node for the [[element tree]], called ''map''. This represents the ''map'' node at the root of the XML document that contains map data.
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]].


This is a special element that is consistent across map changes. It is never destroyed and cannot be destroyed using [[destroyElement]]. It contains directly, or indirectly every other element in the map.
It is often used to attach handler functions to events triggered for any element, or also to make a scripting function affect all elements.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element getRootElement ( void )
element getRootElement ( )
</syntaxhighlight>
</syntaxhighlight>


===Returns===
===Returns===
Returns the root [[element]] object.
Returns the root [[element]].


==Example==
==Example==
This example will retrieve the version of the currently loaded map, by reading the ''version'' argument from the root element of the element tree.
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">
root = getRootElement ()
local root = getRootElement()
version = getElementData ( root, "version" )
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." )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Element functions}}
{{Element functions}}

Revision as of 16:16, 31 July 2007

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.

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.

local root = 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