SetElementData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Undo revision 59044 by Lizzalka (talk))
Line 1: Line 1:
<font face="sans-serif">
{{Server client function}}
<div style="background:#333;">
__NOTOC__
<div style="height:4px;background:#AAA;"></div>
This function stores [[element data]] under a certain key, attached to an element. Element data set using this is then synced with all clients and the server. The data can contain server created elements, but you should avoid passing data that is not able to be synced such as xmlnodes, acls, aclgroups etc.
<font color="#FFF" size="5">
 
<p>&nbsp;Ваша версия Adobe Flash Player устарела</p>
As element data is synced to all clients, it can generate a lot of network traffic and consume server CPU. Events are much more efficient for sending data from a client to the server only, or from the server to a specific client.
</font>
 
<div style="background:#FFF;">
{{Tip|A simple and efficient way to make a variable known to the server and clients is to use setElementData on the [[root]] element.}}
<font color="#F00" size="2">
{{Note|See [[Script security]] for tips on preventing cheaters when using events and element data}}
<p>Требуется срочное обновление до текущей версии!</p>
 
</font>
==Syntax==
<font color="#000" size="4">
 
<p>Adobe Flash Player 30.0.0.164 <font color="#888" size="2">(~18 kB)</font></p>
<syntaxhighlight lang="lua">
</font>
bool setElementData ( element theElement, string key, var value [, bool synchronize = true ] )
<font color="#444" size="2">
</syntaxhighlight>
<p><b>Операционная система:</b> Windows<br><b>Язык:</b> Выбирает пользователь</p>
{{OOP||[[element]]:setData||getElementData}}
</font>
 
<font color="#000" size="4">
===Required Arguments===
<p>Скачать обновление с Яндекс.Диска: yadi.sk/d/AfbiMAr1PkGdww</p>
*'''theElement:''' The [[element]] you wish to attach the data to.
</font>
*'''key:''' The key you wish to store the data under. (Maximum 31 characters.)
</div>
*'''value:''' The value you wish to store. See [[element data]] for a list of acceptable datatypes.
</div>
 
</font>
===Optional Arguments===
*'''synchronize:''' Determines whether or not the data will be synchronized with the clients(server-side variation) or server(client-side variation)
 
===Returns===
Returns ''true'' if the data was set succesfully, ''false'' otherwise.
 
===Issues===
{{Issues|
{{Issue|7389|[Fixed in 1.3.5-7389] Problem with floating numbers}}
}}
 
==Example==
<section name="Server" class="server" show="true">
This example allows a player to add a custom tag onto their nickname, and also reverts it back to normal if they wish.
<syntaxhighlight lang="lua">
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 = getPlayerName ( 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
setPlayerName ( 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
setPlayerName( thePlayer, sOldNick )
--Notify them
outputChatBox ( "Your old nickname has been set", thePlayer )
end
end
addCommandHandler ( "deltag", removePlayerCustomTag )
</syntaxhighlight>
</section>
[[tr:setElementData]]
 
==See Also==
{{Element_functions}}

Revision as of 06:04, 9 September 2018

This function stores element data under a certain key, attached to an element. Element data set using this is then synced with all clients and the server. The data can contain server created elements, but you should avoid passing data that is not able to be synced such as xmlnodes, acls, aclgroups etc.

As element data is synced to all clients, it can generate a lot of network traffic and consume server CPU. Events are much more efficient for sending data from a client to the server only, or from the server to a specific client.


[[{{{image}}}|link=|]] Tip: A simple and efficient way to make a variable known to the server and clients is to use setElementData on the root element.
[[{{{image}}}|link=|]] Note: See Script security for tips on preventing cheaters when using events and element data

Syntax

bool setElementData ( element theElement, string key, var value [, bool synchronize = true ] )

OOP Syntax Help! I don't understand this!

Method: element:setData(...)
Counterpart: getElementData


Required Arguments

  • theElement: The element you wish to attach the data to.
  • key: The key you wish to store the data under. (Maximum 31 characters.)
  • value: The value you wish to store. See element data for a list of acceptable datatypes.

Optional Arguments

  • synchronize: Determines whether or not the data will be synchronized with the clients(server-side variation) or server(client-side variation)

Returns

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

Issues

Issue ID Description
#7389 [Fixed in 1.3.5-7389] Problem with floating numbers

Example

Click to collapse [-]
Server

This example allows a player to add a custom tag onto their nickname, and also reverts 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 = getPlayerName ( 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
		setPlayerName ( 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
		setPlayerName( thePlayer, sOldNick )
		
		--Notify them
		outputChatBox ( "Your old nickname has been set", thePlayer )
	end
end
addCommandHandler ( "deltag", removePlayerCustomTag )

See Also