GetElementChild: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
m (|)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}This function returns an element based on it's parent. Without passing it to [[getElementType]] you won't know what type of element do you have though.
{{Server client function}}
This function returns one of the child elements of a given parent element. The child element is selected by its index (0 for the first child, 1 for the second and so on).
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element getElementChild ( element parent, int index )  
element getElementChild ( element parent, int index )  
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[element]]:getChild||}}


===Required Arguments===  
===Required Arguments===  
Line 11: Line 13:


===Returns===
===Returns===
Returns the requested element if it exists, or false if it doesn't.
Returns the requested element if it exists, or ''false'' if it doesn't.


==Example==
==Example==
If the .map file containts:
If the .map file contains:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<team1 id="red">
<team1 id="red">
Line 24: Line 26:
This outputs the name of the specified base. For example: 'teamBase 0' for the first base.  
This outputs the name of the specified base. For example: 'teamBase 0' for the first base.  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function showTeamBase(player,command,index)
function showTeamBase(thePlayer, command, index)
local team = getElementByID("red") -- get the team element
local theTeam = getElementByID("red")                   -- get the team element
local base = getElementChild(team,tonumber(index)) -- get the Child of the element, based on the 'index' the player specified.
local base = getElementChild(theTeam, tonumber(index)) -- get the Child of the element, based on the 'index' the player specified.
if (base ~= false) then -- if a base exists
if (base ~= false) then                                 -- if the base exists
outputChatBox("Team base "..index..": "..getElementData(base,"name"),player) -- output it to the player
outputChatBox("Team base " .. index .. ": " .. getElementData(base, "name"), thePlayer) -- output it to the player
else
else
outputChatBox("Base not found.",player)
outputChatBox("Base not found.", thePlayer)
end
end
end
end
addCommandHandler("teamBase",showTeamBase)
addCommandHandler("teamBase", showTeamBase)
</syntaxhighlight>
</syntaxhighlight>


Note that if there are other child elements, you'd have to check whether the element is a base with the getElementType function. For example: 'teamBase 3' would output 'Team Base 3: Factory' with the .map file below, even though it's no base.
Note that if there are other child elements, you'd have to check whether the element is a base with the [[getElementType]] function. For example: 'teamBase 3' would output 'Team Base 3: Factory' with the .map file below, even though it's no base.


<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">

Latest revision as of 03:29, 1 January 2015

This function returns one of the child elements of a given parent element. The child element is selected by its index (0 for the first child, 1 for the second and so on).

Syntax

element getElementChild ( element parent, int index ) 

OOP Syntax Help! I don't understand this!

Method: element:getChild(...)


Required Arguments

  • parent: the element above the one to be returned in the hierarchy.
  • index: the element's index (0 for the first element, 1 for the second, etc).

Returns

Returns the requested element if it exists, or false if it doesn't.

Example

If the .map file contains:

<team1 id="red">
	<base name="Mountain Top" />
	<base name="Docks" />
	<base name="Airport" />
</team1>

This outputs the name of the specified base. For example: 'teamBase 0' for the first base.

function showTeamBase(thePlayer, command, index)
	local theTeam = getElementByID("red")                   -- get the team element
	local base = getElementChild(theTeam, tonumber(index))  -- get the Child of the element, based on the 'index' the player specified.
	if (base ~= false) then                                 -- if the base exists
		outputChatBox("Team base " .. index .. ": " .. getElementData(base, "name"), thePlayer)  -- output it to the player
	else
		outputChatBox("Base not found.", thePlayer)
	end
end
addCommandHandler("teamBase", showTeamBase)

Note that if there are other child elements, you'd have to check whether the element is a base with the getElementType function. For example: 'teamBase 3' would output 'Team Base 3: Factory' with the .map file below, even though it's no base.

<team1 id="red">
	<base name="Mountain Top" />
	<base name="Docks" />
	<base name="Airport" />
	<target name="Factory" />
</team1>

See Also