Difference between revisions of "OnElementDataChange"

From Multi Theft Auto: Wiki
m (See Also for server events)
(Added an example)
Line 16: Line 16:
 
==Example==  
 
==Example==  
 
<!-- Explain what the example is in a single sentance -->
 
<!-- Explain what the example is in a single sentance -->
This example does...
+
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 -->
 
<!-- 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">
--This line does...
+
function outputChange(dataName,oldValue)
blah()
+
if getElementType(source) == "player" then -- check if the element is a player
--This line does this...
+
local newValue = getElementData(source,dataName) -- find the new value
mooo
+
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",getRootElement(),outputChange)
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 14:27, 9 April 2010

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 ).

Source

The source of this event is the element whose elementdata changed.

Example

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",getRootElement(),outputChange)

See Also

Element events

Event functions