GetElementChild: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}This function returns an element based on it's parent.
{{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.
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 14: Line 14:


==Example==
==Example==
To spawn a player at the red team's third spawnpoint:
If the .map file containts:
<syntaxhighlight lang="xml">
<syntaxhighlight lang="xml">
<team1 id="red">
<team1 id="red">
    <spawnpoint id="spawnpoint_0" posX="2507.8715820313" posY="2772.6071777344" posZ="10.8203125" rot="270" skin="285"/>
<base name="Mountain Top" />
    <spawnpoint id="spawnpoint_1" posX="2508.060546875" posY="2780.3647460938" posZ="10.8203125" rot="270" skin="285"/>
<base name="Docks" />
    <spawnpoint id="spawnpoint_2" posX="2508.0053710938" posY="2776.2897949219" posZ="10.8203125" rot="270" skin="285"/>
<base name="Airport" />
    <spawnpoint id="spawnpoint_3" posX="2510.6899414063" posY="2778.3745117188" posZ="10.8203125" rot="270" skin="285"/>
</team1>
</team1></syntaxhighlight>
</syntaxhighlight>
This outputs the name of the specified base. For example: 'teamBase 0' for the first base.  
<syntaxhighlight lang="lua">
function showTeamBase(player,command,index)
local team = 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.
if (base ~= false) then -- if a base exists
outputChatBox("Team base "..index..": "..getElementData(base,"name"),player) -- output it to the player
else
outputChatBox("Base not found.",player)
end
end
addCommandHandler("teamBase",showTeamBase)
</syntaxhighlight>


You could use the following code:
Note that if there are other child elements, you'd have to check whether the element is a base. For example: 'teamBase 3' would output 'Team Base 3: Factory' with the .map file below, though it's no base.
<syntaxhighlight lang="lua">
 
teamRed = getElementByID ( "red" ) -- find the parent element by it's ID and assign it to teamRed variable
<syntaxhighlight lang="xml">
teamRedSpawn = getElementChild ( teamRed, 2 ) -- find teamRed's third child element (which happens to be a spawnpoint)
<team1 id="red">
spawnPlayer ( somePlayer, teamRedSpawn ) -- spawn a player at the spawnpoint
<base name="Mountain Top" />
<base name="Docks" />
<base name="Airport" />
<target name="Factory" />
</team1>
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Element_functions}}
{{Element_functions}}

Revision as of 12:05, 29 July 2007

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.

Syntax

element getElementChild ( element parent, int index ) 

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 containts:

<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(player,command,index)
	local team = 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.
	if (base ~= false) then -- if a base exists
		outputChatBox("Team base "..index..": "..getElementData(base,"name"),player) -- output it to the player
	else
		outputChatBox("Base not found.",player)
	end
end
addCommandHandler("teamBase",showTeamBase)

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

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

See Also