GetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 11: Line 11:


===Optional Arguments===
===Optional Arguments===
*'''inherit:''' ''available starting at DP3'' - 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.
*'''inherit:''' ''available starting at 1.0'' - 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===
===Returns===

Revision as of 16:13, 30 January 2009

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

Syntax

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

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.

Optional Arguments

  • inherit: available starting at 1.0 - 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", getRootElement(), joinTime )

function showJoinTime ( source, commandName, playerName )
	if ( playerName ) then -- see if a player was specified
		thePlayer = getPlayerFromNick ( 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 ( getClientName ( 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