OnElementDataChange: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Add comma)
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Server event}}
__NOTOC__  
__NOTOC__  
{{Server event}}
This event is triggered ''after'' an element's [[element data|data]] entry is changed. Such changes can be made on the client or the server using [[setElementData]].
This event is triggered when an elementdata entry for an element changes. A client can perform this change on the element or it can be done using [[setElementData]].


==Parameters==
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string theName, var theOldValue
string theKey, var oldValue, var newValue
</syntaxhighlight>  
</syntaxhighlight>  


*'''theName''': The name of the element data entry that changed
*'''theKey''': The name of the element data entry that has changed.
*'''theOldValue''': The old value of this entry before it changed. The new value can be accessed using [[getElementData]] ( source, theName ).
*'''oldValue''': The old value of this entry before it changed. See [[element data]] for a list of possible datatypes.
*'''newValue''': the new value of this entry after it changed. This will be equivalent to [[getElementData]](source, theKey).


==Global parameters==
==Global parameters==
*'''source''': The [[event system#Event source|source]] of this event is the [[element]] whose element data changed
*'''source''': The [[event system#Event source|source]] of this event is the [[element]] whose [[element data]] changed.
*'''client''': The [[event system#Event client|client]] global variable is set to the client that called [[setElementData]], or nil if it was called on the server.
*'''client''': The [[event system#Event client|client]] global variable is set to the client that called [[setElementData]], or '''nil''' if it was called on the server.
*'''sourceResource''': The [[resource]] which changed the element data - '''nil''', if client synced data, '''resource''' element otherwise.


==Cancelling==
==Cancelling==
Line 20: Line 22:
==Example==  
==Example==  
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<!-- Explain what the example is in a single sentance -->
This example outputs a message to players when any of their element data values is changed.
This example outputs a message to players when any of their element data values is changed.
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function outputChange(dataName,oldValue)
function outputChange(theKey, oldValue, newValue)
if getElementType(source) == "player" then -- check if the element is a player
    if (getElementType(source) == "player") then -- check if the element is a player
local newValue = getElementData(source,dataName) -- find the new value
        outputChatBox("Your element data '" .. tostring(theKey) .. "' has changed from '" .. tostring(oldValue) .. "' to '" .. tostring(newValue) .. "'", source) -- output the change for the affected player
outputChatBox("Your element data '"..tostring(dataName).."' has changed from '"..tostring(oldValue).."' to '"..tostring(newValue).."'",source) -- output the change for the affected player
    end
end
end
end
addEventHandler("onElementDataChange",getRootElement(),outputChange)
addEventHandler("onElementDataChange", root, outputChange)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<!-- Explain what the example is in a single sentance -->
This example checks and possibly reverses an element's data change.
This example checks and possibly reverses an element's data change.
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function checkChange(dataName,oldValue)
function checkChange(theKey, oldValue)
     if client then
     -- The client can only set 'special_thing' on its own player
        -- The client can only set 'special_thing' on its own player
    if (theKey== "special_thing") and (client ~= source) then
        if dataName == "special_thing" and client ~= source then
        outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client)))
            outputChatBox( "Illegal setting of "..tostring(dataName).."' by '"..tostring(getPlayerName(client)) )
        setElementData(source, theKey, oldValue) -- Set back the original value
            setElementData( source, dataName, oldValue ) -- Set back the original value
        end
     end
     end
end
end
addEventHandler("onElementDataChange",getRootElement(),checkChange)
addEventHandler("onElementDataChange", root, checkChange)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
{{See also/Server event|Element events}}
{{See also/Server event|Element events}}

Latest revision as of 18:17, 7 June 2023

This event is triggered after an element's data entry is changed. Such changes can be made on the client or the server using setElementData.

Parameters

string theKey, var oldValue, var newValue
  • theKey: The name of the element data entry that has changed.
  • oldValue: The old value of this entry before it changed. See element data for a list of possible datatypes.
  • newValue: the new value of this entry after it changed. This will be equivalent to getElementData(source, theKey).

Global parameters

  • source: The source of this event is the element whose element data changed.
  • client: The client global variable is set to the client that called setElementData, or nil if it was called on the server.
  • sourceResource: The resource which changed the element data - nil, if client synced data, resource element otherwise.

Cancelling

This event cannot be cancelled using cancelEvent. To reverse the effect, use setElementData with the old value. See Example.

Example

Click to collapse [-]
Server

This example outputs a message to players when any of their element data values is changed.

function outputChange(theKey, oldValue, newValue)
    if (getElementType(source) == "player") then -- check if the element is a player
        outputChatBox("Your element data '" .. tostring(theKey) .. "' has changed from '" .. tostring(oldValue) .. "' to '" .. tostring(newValue) .. "'", source) -- output the change for the affected player
    end
end
addEventHandler("onElementDataChange", root, outputChange)
Click to collapse [-]
Server

This example checks and possibly reverses an element's data change.

function checkChange(theKey, oldValue)
    -- The client can only set 'special_thing' on its own player
    if (theKey== "special_thing") and (client ~= source) then
        outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client)))
        setElementData(source, theKey, oldValue) -- Set back the original value
    end
end
addEventHandler("onElementDataChange", root, checkChange)

See Also

Element events


Event functions