GetElementByIndex: 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__  
{{Note box|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]] instead.}}
{{Note box|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]] instead.}}
This function returns an element of the specified type from index.  
This function returns an element of the specified type with the specified index.  


==Syntax==  
==Syntax==  
Line 13: Line 13:


===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==  

Revision as of 21:01, 28 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 loops through each player and sends them a message. A more efficient method would be to use getElementsByType.

totalplayers = getPlayerCount () -- get the amount of players in the server
local i = 0
while ( i < totalplayers ) do -- step through each player, from 0 to total
  pyr = getElementByIndex ( "player", i ) -- get a player element from index and assign it to variable pyr
  outputChatBox ( "Hello " .. getClientName ( pyr ) .. "!", pyr ) -- send player pyr a message
  i = i + 1
end

See Also