SetElementID: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(12 intermediate revisions by 6 users not shown)
Line 2: Line 2:
__NOTOC__  
__NOTOC__  
This function sets the ID of an element to a string. This can be anything from an identifying number, to a name.
This function sets the ID of an element to a string. This can be anything from an identifying number, to a name.
 
You can only change the ID of an element clientside if that element has been created clientside as well.
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool setElementID ( element theElement, string name )  
bool setElementID ( element theElement, string name )  
</syntaxhighlight>  
</syntaxhighlight>  
{{OOP||[[element]]:setID|id|getElementID}}


===Required Arguments===  
===Required Arguments===  
Line 17: Line 18:
==Example==  
==Example==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local players = getElementsByType( "player" ) -- Get a table of all players.
local players = getElementsByType( "player" )
for i=1,#players do
  setElementID( players[i], "player" .. i ) -- Change element IDs to 'player1', 'players2', 'players3'...
  outputDebugString( "Player[" .. i .. "] = " .. getElementID( players[i] ) ) -- Output all the new element IDs
end
 
 
-- Could also be --


for i=1,#players do
for i=1,#players do
   setElementID( players[i], "player" .. tostring(i) ) -- Loop the table and change the element IDs to 'player1', 'player2', 'player3'...
   setElementID( players[i], getPlayerName( players[i] ) ) -- Change the element ID to the players name.
   outputDebugString( "Player[" .. i .. "] = " .. getElementID( players[i] ) ) -- Output the new element IDs to debugstring.
   outputDebugString( "Player[" .. i .. "] = " .. getElementID( players[i] ) )
end
end
</syntaxhighlight>
</syntaxhighlight>
Line 27: Line 36:
==See Also==
==See Also==
{{element_functions}}
{{element_functions}}
[[Category:Needs Example]]

Latest revision as of 19:54, 15 January 2018

This function sets the ID of an element to a string. This can be anything from an identifying number, to a name. You can only change the ID of an element clientside if that element has been created clientside as well.

Syntax

bool setElementID ( element theElement, string name ) 

OOP Syntax Help! I don't understand this!

Method: element:setID(...)
Variable: .id
Counterpart: getElementID


Required Arguments

  • theElement: The element you want to set the ID of.
  • name: The new ID for theElement.

Returns

This returns true if successful. It will return false if theElement is invalid, or does not exist, or if name is invalid, or is not a string.

Example

local players = getElementsByType( "player" )
 
for i=1,#players do
   setElementID( players[i], "player" .. i )	-- Change element IDs to 'player1', 'players2', 'players3'...
   outputDebugString( "Player[" .. i .. "] = " .. getElementID( players[i] ) ) -- Output all the new element IDs
end


-- Could also be --

for i=1,#players do
   setElementID( players[i], getPlayerName( players[i] ) )	-- Change the element ID to the players name.
   outputDebugString( "Player[" .. i .. "] = " .. getElementID( players[i] ) )
end

See Also