GetElementByIndex: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Change example)
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__ {{Important Note|If you wish to retrieve more than one element at a time, it is more efficient to use [[getElementsByType]] instead.}}
This function returns an element of the specified type from index. It is important to note that if you wish to retrieve more than one element at a time, it is more efficient to use [[getElementsByType]].
{{Server client function}}
This function returns an element of the specified type with the specified index.  


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
element getElementByIndex ( string type, int index )   
element getElementByIndex ( string theType, int index )   
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP|This function is a static function inside the Element class.|[[Element]].getByIndex||}}


===Required Arguments===  
===Required Arguments===  
*'''type:''' the type of the element to be returned. Examples include "player", "vehicle", or a custom type.
*'''theType:''' 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).
*'''index:''' the element's index (0 for the first element, 1 for the second, etc).


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


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


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

Latest revision as of 19:26, 29 October 2023

[[{{{image}}}|link=|]] Important Note: If you wish to retrieve more than one element at a time, it is more efficient to use getElementsByType instead.

This function returns an element of the specified type with the specified index.

Syntax

element getElementByIndex ( string theType, int index )  

OOP Syntax Help! I don't understand this!

Note: This function is a static function inside the Element class.
Method: Element.getByIndex(...)


Required Arguments

  • theType: 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 generates the name of the specified player that is on the server. For example: 'player 0' would return the name of the first player on the server.

function showPlayer(command, index)
	local thePlayer = getElementByIndex("player", tonumber(index))
	if thePlayer then
		outputChatBox("Player " .. index .. " is a: " .. getPlayerName(thePlayer))
	else
		outputChatBox("Player not found.")
	end
end
addCommandHandler("player", showPlayer)

See Also