GetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Undo revision 59043 by Lizzalka (talk))
Line 1: Line 1:
<font face="sans-serif">
__NOTOC__
<div style="background:#333;">
{{Server client function}}
<div style="height:4px;background:#AAA;"></div>
This function retrieves [[element data]] attached to an element under a certain key.
<font color="#FFF" size="5">
 
<p>&nbsp;Ваша версия Adobe Flash Player устарела</p>
==Syntax==
</font>
<syntaxhighlight lang="lua">var getElementData ( element theElement, string key [, inherit = true] )</syntaxhighlight>
<div style="background:#FFF;">
{{OOP||[[element]]:getData||setElementData}}
<font color="#F00" size="2">
 
<p>Требуется срочное обновление до текущей версии!</p>
===Required Arguments===
</font>
*'''theElement:''' This is the element with data you want to retrieve.
<font color="#000" size="4">
*'''key:''' The name of the element data entry you want to retrieve. (Maximum 31 characters.)
<p>Adobe Flash Player 30.0.0.164 <font color="#888" size="2">(~18 kB)</font></p>
 
</font>
===Optional Arguments===
<font color="#444" size="2">
*'''inherit:''' - toggles whether or not the function should go up the hierarchy to find the requested key in case the specified element doesn't have it.
<p><b>Операционная система:</b> Windows<br><b>Язык:</b> Выбирает пользователь</p>
 
</font>
===Returns===
<font color="#000" size="4">
This function returns a ''variable'' containing the requested element data, or ''false'' if the element or the element data does not exist. When getting data corresponding to a XML attribute, this is always a ''string''.
<p>Скачать обновление с Яндекс.Диска: yadi.sk/d/AfbiMAr1PkGdww</p>
 
</font>
==Example==
</div>
This example stores the tick count when a player joins and then allows players to see how long they are connected using a console function 'joinTime'.
</div>
<section show="true" name="Server" class="server">
</font>
<syntaxhighlight lang="lua">
function joinTime ( )
    setElementData ( source, "joinTime", getTickCount() ) -- Store the current tick count in the player's data with the key 'joinTime'
end
-- Make our 'joinTime' function be called when a player joins
addEventHandler ( "onPlayerJoin", root, joinTime )
 
function showJoinTime ( source, commandName, playerName )
if ( playerName ) then -- see if a player was specified
thePlayer = getPlayerFromName ( playerName ) -- get the player element for the specified player
if ( thePlayer ) then -- if one was found...
local timeOnline = (getTickCount() - getElementData ( thePlayer, "joinTime" )) / 1000 -- calculates the time since join
outputChatBox ( getPlayerName ( thePlayer ).." joined "..timeOnline.." seconds ago", source ) -- output the player's join time
else
outputChatBox ( "Couldn't find '" .. playerName .. "'", source ) -- display an error
end
else
-- display when the player who used the function joined and inform how to see other people's join time
local timeOnline = (getTickCount() - getElementData ( source, "joinTime" )) / 1000 -- calculate the time since join
outputChatBox ( "You joined " ..timeOnline.." seconds ago", source )
outputChatBox ( "Use 'join_time <player name>' to see other people's join time", source )
end
end
-- Add a console command joinTime, that takes an optional parameter of a player's name
addCommandHandler ( "joinTime", showJoinTime )
</syntaxhighlight>
</section>
 
==See Also==
{{Element functions}}

Revision as of 06:03, 9 September 2018

This function retrieves element data attached to an element under a certain key.

Syntax

var getElementData ( element theElement, string key [, inherit = true] )

OOP Syntax Help! I don't understand this!

Method: element:getData(...)
Counterpart: setElementData


Required Arguments

  • theElement: This is the element with data you want to retrieve.
  • key: The name of the element data entry you want to retrieve. (Maximum 31 characters.)

Optional Arguments

  • inherit: - toggles whether or not the function should go up the hierarchy to find the requested key in case the specified element doesn't have it.

Returns

This function returns a variable containing the requested element data, or false if the element or the element data does not exist. When getting data corresponding to a XML attribute, this is always a string.

Example

This example stores the tick count when a player joins and then allows players to see how long they are connected using a console function 'joinTime'.

Click to collapse [-]
Server
function joinTime ( )
    setElementData ( source, "joinTime", getTickCount() ) -- Store the current tick count in the player's data with the key 'joinTime'
end
-- Make our 'joinTime' function be called when a player joins
addEventHandler ( "onPlayerJoin", root, joinTime )

function showJoinTime ( source, commandName, playerName )
	if ( playerName ) then -- see if a player was specified
		thePlayer = getPlayerFromName ( playerName ) -- get the player element for the specified player
		if ( thePlayer ) then -- if one was found...
			local timeOnline = (getTickCount() - getElementData ( thePlayer, "joinTime" )) / 1000 -- calculates the time since join
			outputChatBox ( getPlayerName ( thePlayer ).." joined "..timeOnline.." seconds ago", source ) -- output the player's join time
		else
			outputChatBox ( "Couldn't find '" .. playerName .. "'", source ) -- display an error
		end
	else
		-- display when the player who used the function joined and inform how to see other people's join time
		local timeOnline = (getTickCount() - getElementData ( source, "joinTime" )) / 1000 -- calculate the time since join
		outputChatBox ( "You joined " ..timeOnline.." seconds ago", source )
		outputChatBox ( "Use 'join_time <player name>' to see other people's join time", source )
	end
end
-- Add a console command joinTime, that takes an optional parameter of a player's name
addCommandHandler ( "joinTime", showJoinTime )

See Also