SetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
Line 18: Line 18:
Returns ''true'' if the data was set succesfully, ''false'' otherwise.
Returns ''true'' if the data was set succesfully, ''false'' otherwise.


<section name="Server" class="server" show="false">
==Example==  
==Example==  
This example stores the date at which the player joined the server in their element data, and allows them to view it by typing a command:
This example allows for a player to add a custom tag onto their nickname, and also revert it back to normal if they wish
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Make our 'joinTime' function be called when a player joins
function addPlayerCustomTag ( player, command, newTag )
addEventHandler ( "onPlayerJoin", getRootElement(), "joinTime" )
--Lets check that the newTag param has been entered...
function joinTime ( )
if ( newTag ) then
    setElementData ( source, "joinTime", os.date() ) -- Store the current time in the player's data with the key 'joinTime'
--Grab their current playername for saving.
local sPlayerNickname = getClientName(player)
--Create their new nickname with their tag
local sNewPlayerNickname = newTag .. " " .. sPlayerNickname
--Lets first load the element data, see if its there allready
--The reason for this is that if a player were to do /addtag twice
--it would have saved their last tag as their orignal nickname
--without this check
local sOldNick = getElementData( player, "tempdata.orignalnick" )
if ( sOldNick == false ) then
--Save their orignal nickname in their element data
setElementData ( player, "tempdata.orignalnick", sPlayerNickname )
end
--Set their new nickname globally
setClientName ( player, sNewPlayerNickname )
--Tell them its done
outputChatBox ( "Your new nickname has been set, to put it back to its orignal state you can use /deltag", player )
else
--The newTag param was not entered, give a error message
outputChatBox ( "/addtag - Incorrect syntax, Correct: /addtag <newtag>", player )
end
end
end
addCommandHandler ( "addtag", addPlayerCustomTag )


-- Add a console command join_time, that takes an optional parameter of a player's name
function removePlayerCustomTag ( player, command )
addCommandHandler ( "join_time", "showJoinTime" )
--We first need to check that they have allready used /addtag, lets do that now
function showJoinTime ( source, commandName, playerName )
local sOldNick = getElementData( player, "tempdata.orignalnick" )
    if ( playerName ) then -- see if a player was specified
if ( sOldNick ) then
        thePlayer = getPlayerFromNick ( playerName ) -- get the player element for the specified player
--Great, they have a tag added, lets reset them
        if ( thePlayer ) then -- if one was found...
            outputChatBox ( getClientName ( thePlayer ) .. " joined " .. getElementData ( thePlayer, "joinTime" ), source ) -- output the player's join time
--First we will want to reset the element data back to its default (that being false)
        else
setElementData ( player, "tempdata.orignalnick", false )
            outputChatBox ( "Couldn't find '" .. playerName .. "'", source ) -- display an error
        end
--Now set the client name back
    else
setClientName ( player, sOldNick )
        -- display when the player who used the function joined and inform how to see other people's join time
        outputChatBox ( "You joined " .. getElementData ( source, "joinTime" ), source )
--Notify them
        outputChatBox ( "Use 'join_time <player name>' to see other people's join time" )
outputChatBox ( "Your old nickname has been set", player )
    end
end
end
end
addCommandHandler ( "deltag", removePlayerCustomTag )
</syntaxhighlight>
</syntaxhighlight>
</section>


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

Revision as of 10:21, 12 August 2007

This function stores data attached to an element. This data can be any primitive LUA type or MTA element (but not a text item or display).

Element data is a useful way to store data attached to players.

Syntax

bool setElementData ( element theElement, string key, var value ) 

Required Arguments

  • theElement: The element you wish to attach the data to
  • key: The key you wish to store the data under
  • value: The value you wish to store

Returns

Returns true if the data was set succesfully, false otherwise.

Click to expand [+]
Server

See Also