OnElementDataChange: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
(17 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Server event}}
__NOTOC__  
__NOTOC__  
This fake function is for use with blah & blah and does blahblahblabhalbhl
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 will be triggered when someone joins the server
==Parameters==
<syntaxhighlight lang="lua">
string theKey, var oldValue, var newValue
</syntaxhighlight>
 
*'''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).


==Example==
==Global parameters==
This example gets the joining client's name and outputs a welcome message including his name once he joins
*'''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.
*'''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==
<section name="Server" class="server" show="true">
This example outputs a message to players when any of their element data values is changed.
<syntaxhighlight lang="lua">
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)
</syntaxhighlight>
</section>
<section name="Server" class="server" show="true">
This example checks and possibly reverses an element's data change.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
 
function checkChange(theKey, oldValue)
addEventHandler ( "onPlayerJoin", root, "onPlayerJoin" )
    -- The client can only set 'special_thing' on its own player
function onPlayerJoin ( )
    if (theKey== "special_thing") and (client ~= source) then
joinplayer = getClientName ( source )
        outputChatBox("Illegal setting of " .. tostring(theKey) .. "' by '" .. tostring(getPlayerName(client)))
outputChatBox ( "Welcome " ..joinplayer.. " to the Server!" , source, 255, 255, 255 )
        setElementData(source, theKey, oldValue) -- Set back the original value
    end
end
end
addEventHandler("onElementDataChange", root, checkChange)
</syntaxhighlight>
</section>


</syntaxhighlight>
{{See also/Server event|Element events}}

Revision as of 12:28, 8 October 2018

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