SetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:
bool setElementData ( element theElement, string key, [bool synchronize] var value )
bool setElementData ( element theElement, string key, [bool synchronize] var value )
</syntaxhighlight>  
</syntaxhighlight>  
</section>
<section name="Server" class="server" show="true">  
<section name="Server" class="server" show="true">  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">

Revision as of 17:30, 14 February 2008

This function stores element data under a certain key, attached to an element. Element data set using this is then synced with all clients or the server (depending if used client-side or not). As such you should avoid passing data that is not able to be synced into this function (i.e. xmlnodes, acls, aclgroups etc.) Server-created elements are able to be synced.

Syntax

Click to expand [+]
Client
Click to collapse [-]
Server
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. See element data for a list of acceptable datatypes.

Returns

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

Example

<section name="Server" class="server" show="false"> This example allows for a player to add a custom tag onto their nickname, and also revert it back to normal if they wish

function addPlayerCustomTag ( thePlayer, command, newTag )
	--Let's make sure the newTag param has been entered...
	if ( newTag ) then
		--Grab their current playername for saving.
		local sPlayerNickname = getClientName ( thePlayer )
		--Create their new nickname with their tag
		local sNewPlayerNickname = newTag .. " " .. sPlayerNickname
		
		--Let's first load the element data, see if it's there already
		--The reason for this is that if a player were to do /addtag twice,
		--the tag would be prepended a second time
		local sOldNick = getElementData( thePlayer, "tempdata.originalnick" )
		if ( sOldNick == false ) then
			--Save their orignal nickname in their element data
			setElementData ( thePlayer, "tempdata.originalnick", sPlayerNickname )
		end
		
		--Set their new nickname globally
		setClientName ( thePlayer, sNewPlayerNickname )
		
		--Tell them it's done
		outputChatBox ( "Your new nickname has been set, to put it back to its original state you can use /deltag", thePlayer )
	else
		--The newTag param was not entered, give an error message
		outputChatBox ( "/addtag - Incorrect syntax, Correct: /addtag <newtag>", thePlayer )
	end
end
addCommandHandler ( "addtag", addPlayerCustomTag )

function removePlayerCustomTag ( thePlayer, command )
	--We first need to check that they have already used /addtag, let's do that now
	local sOldNick = getElementData( thePlayer, "tempdata.originalnick" )
	if ( sOldNick ) then
		--Great, they have a tag added, let's reset them
		
		--First we will want to reset the element data back to its default (that being false)
		setElementData ( thePlayer, "tempdata.originalnick", false )
		
		--Now set the client name back
		setClientName ( thePlayer, sOldNick )
		
		--Notify them
		outputChatBox ( "Your old nickname has been set", thePlayer )
	end
end
addCommandHandler ( "deltag", removePlayerCustomTag )

See Also