GetElementByIndex: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 16: Line 16:


==Example==  
==Example==  
This example loops through each player and sends them a message. A more efficient method would be to use [[getElementsByType]].
This example outputs the name of the specified vehicle currently existant in the XML tree. For example: 'vehicle 0' would return the first vehicle.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
totalplayers = getPlayerCount () -- get the amount of players in the server
function showVehicle(player,command,index)
local i = 0
if (
while ( i < totalplayers ) do -- step through each player, from 0 to total
local vehicle = getElementByIndex("vehicle",tonumber(index))
  pyr = getElementByIndex ( "player", i ) -- get a player element from index and assign it to variable pyr
if (vehicle ~= false) then
  outputChatBox ( "Hello " .. getClientName ( pyr ) .. "!", pyr ) -- send player pyr a message
outputChatBox("Vehicle "..index.." is a: "..tostring(getVehicleName(vehicle)),player)
  i = i + 1
else
outputChatBox("Vehicle not found.",player)
end
end
end
addCommandHandler("vehicle",showVehicle)
</syntaxhighlight>
</syntaxhighlight>


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

Revision as of 11:40, 29 July 2007

This template is no longer in use as it results in poor readability. This function returns an element of the specified type with the specified index.

Syntax

element getElementByIndex ( string type, int index )  

Required Arguments

  • type: the type of the element to be returned. Examples include "player", "vehicle", or a custom type.
  • index: the element's index (0 for the first element, 1 for the second, etc).

Returns

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

Example

This example outputs the name of the specified vehicle currently existant in the XML tree. For example: 'vehicle 0' would return the first vehicle.

function showVehicle(player,command,index)
	if (
	local vehicle = getElementByIndex("vehicle",tonumber(index))
	if (vehicle ~= false) then
		outputChatBox("Vehicle "..index.." is a: "..tostring(getVehicleName(vehicle)),player)
	else
		outputChatBox("Vehicle not found.",player)
	end
end
addCommandHandler("vehicle",showVehicle)

See Also