Difference between revisions of "OnElementDataChange"

From Multi Theft Auto: Wiki
m (Parameters)
m (Example)
Line 21: Line 21:
 
==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(dataName, oldValue)
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
+
local newValue = getElementData(source, dataName) -- find the new value
outputChatBox("Your element data '"..tostring(dataName).."' 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(dataName, 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 (dataName == "special_thing") and (client ~= source) then
        if (dataName == "special_thing" and client ~= source) then
+
outputChatBox("Illegal setting of " .. tostring(dataName) .. "' by '" .. tostring(getPlayerName(client)))
            outputChatBox( "Illegal setting of "..tostring(dataName).."' by '"..tostring(getPlayerName(client)) )
+
setElementData(source, dataName, 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}}

Revision as of 21:23, 2 April 2018

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

string theName, var theOldValue
  • theName: The name of the element data entry that changed.
  • theOldValue: The old value of this entry before it changed. The new value can be accessed using getElementData ( source, theName ).

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. (Only works in versions above 1.3.4-5937)

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

See Also

Element events

Event functions