SetElementData: Difference between revisions
(Remove dot.) |
(Direct link to elementdata_whitelisted) |
||
(One intermediate revision by one other user not shown) | |||
Line 26: | Line 26: | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
*'''synchronize:''' Determines whether or not the data will be synchronized with the clients(server-side variation) or server(client-side variation) | *'''synchronize:''' Determines whether or not the data will be synchronized with the clients (server-side variation) or server (client-side variation) | ||
===Returns=== | ===Returns=== | ||
Line 35: | Line 35: | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool setElementData ( element theElement, string key, var value [, | bool setElementData ( element theElement, string key, var value [, string syncMode="broadcast", string clientChangesPolicy="default" ] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[element]]:setData||getElementData}} | {{OOP||[[element]]:setData||getElementData}} | ||
Line 49: | Line 49: | ||
**''"local"'' - Don't synchronise. You can also parse ''false'' for this option. | **''"local"'' - Don't synchronise. You can also parse ''false'' for this option. | ||
**''"subscribe"'' - Only synchronise to specific clients. See [[addElementDataSubscriber]] and [[removeElementDataSubscriber]]. | **''"subscribe"'' - Only synchronise to specific clients. See [[addElementDataSubscriber]] and [[removeElementDataSubscriber]]. | ||
{{New items|6.0160|1.6.0-22790| | |||
*'''clientChangesPolicy:''' Client changes policy. | |||
**''"default"'' - Use '''elementdata_whitelisted''' setting from [https://wiki.multitheftauto.com/wiki/Server_mtaserver.conf#elementdata_whitelisted mtaserver.conf] | |||
**''"allow"'' - Trust changes from clients. | |||
**''"deny"'' - Deny client changes. The server will trigger the [[onPlayerChangesProtectedData]] event when the client attempts to change the value. | |||
}} | |||
</section> | </section> | ||
Latest revision as of 20:36, 19 December 2024
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 be heavy on performance. Events are much more efficient for sending data from a client to the server only, or from the server to a specific client.
Usage of element data should be discouraged where your goal can be achieved with events like above, and tables for storing and retrieving data.
Tip: A simple and efficient way to make a variable known to the server and clients is to use setElementData on the root element. |
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 128 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 successfully, false otherwise.
Example
Example 1
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
- attachElements
- createElement
- destroyElement
- detachElements
- getAttachedElements
- getElementAlpha
- getElementAttachedOffsets
- getElementAttachedTo
- getElementByIndex
- getElementByID
- getElementChild
- getElementChildren
- getElementChildrenCount
- getElementCollisionsEnabled
- getElementColShape
- getElementData
- getAllElementData
- hasElementData
- getElementDimension
- getElementHealth
- getElementID
- getElementInterior
- getElementMatrix
- getElementModel
- getElementParent
- getElementPosition
- getElementRotation
- getElementsByType
- getElementsWithinColShape
- getElementsWithinRange
- getElementType
- getElementVelocity
- getLowLODElement
- getRootElement
- isElement
- isElementAttached
- isElementCallPropagationEnabled
- isElementDoubleSided
- isElementFrozen
- isElementInWater
- isElementLowLOD
- isElementWithinColShape
- isElementWithinMarker
- setElementAlpha
- setElementAngularVelocity
- getElementAngularVelocity
- setElementAttachedOffsets
- setElementCallPropagationEnabled
- setElementCollisionsEnabled
- setElementData
- setElementDimension
- setElementDoubleSided
- setElementFrozen
- setElementHealth
- setElementID
- setElementInterior
- setElementModel
- setElementParent
- setElementPosition
- setElementRotation
- setElementVelocity
- setLowLODElement
- getPedContactElement
- getResourceDynamicElementRoot
- getResourceRootElement