GetElementByID: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Fix OOP syntax (this caused me a nice ~30 minutes of debugging ;) ))
 
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Incomplete]]
{{Server client function}}
 
__NOTOC__  
__NOTOC__  
This fake function is for use with blah & blah and does blahblahblabhalbhl
This function returns an element from the specified ID. If more than one element with the same ID exists, only the first one in the order it appears in the XML tree will be returned by this function.
{{Note|The ID in this context is the value of the 'id' data item of the element (the <nowiki>id=".."</nowiki> attribute in the .map file), NOT the model ID, weapon ID or similiar.}}


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element getElementByID ( string id )   
element getElementByID ( string id [, int index = 0 ] )   
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP|This function is a static function underneath the Element class.|[[Element]].getByID||}}


===Required Arguments===  
===Required Arguments===  
*'''id:''' The ID of the element as it appears in the xml file.
*'''id:''' The ID of the element as it appears in the XML file or as set by [[setElementID]].
 
===Optional Arguments===
{{optionalArg}}
*'''index:''' If there are two or more elements of the same ID it will return the element with the specified index starting at 0.


===Returns===
===Returns===
Returns the element with the given ID, or ''false'' if no such element exists.
Returns the [[element]] with the given ID, or ''false'' if no such element exists.


==Example==  
==Example==  
This example finds
Assuming we have this in the .map file:
<syntaxhighlight lang="xml">
<vehicle id="vipVehicle" posX="10" posY="10" posZ="4" model="602" />
</syntaxhighlight>
Then this example would retrieve the element and output the vehicle name.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Assuming an element with the id 'offense' exists (i.e. <team1 id="offense">), get it's element
function showVipVehicle()
offenseElement = getElementByID ( "offense" )
local vipVehicle = getElementByID("vipVehicle")
outputChatBox("Vip Vehicle is a: "..getVehicleName(vipVehicle))
end
addCommandHandler("vipVehicle",showVipVehicle)
</syntaxhighlight>
</syntaxhighlight>


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

Latest revision as of 14:41, 26 March 2017

This function returns an element from the specified ID. If more than one element with the same ID exists, only the first one in the order it appears in the XML tree will be returned by this function.

[[{{{image}}}|link=|]] Note: The ID in this context is the value of the 'id' data item of the element (the id=".." attribute in the .map file), NOT the model ID, weapon ID or similiar.

Syntax

element getElementByID ( string id [, int index = 0 ] )  

OOP Syntax Help! I don't understand this!

Note: This function is a static function underneath the Element class.
Method: Element.getByID(...)


Required Arguments

  • id: The ID of the element as it appears in the XML file or as set by setElementID.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • index: If there are two or more elements of the same ID it will return the element with the specified index starting at 0.

Returns

Returns the element with the given ID, or false if no such element exists.

Example

Assuming we have this in the .map file:

<vehicle id="vipVehicle" posX="10" posY="10" posZ="4" model="602" />

Then this example would retrieve the element and output the vehicle name.

function showVipVehicle()
	local vipVehicle = getElementByID("vipVehicle")
	outputChatBox("Vip Vehicle is a: "..getVehicleName(vipVehicle))
end
addCommandHandler("vipVehicle",showVipVehicle)

See Also