<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newton</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newton"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Newton"/>
	<updated>2026-05-21T11:47:33Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=AddEventHandler&amp;diff=78575</id>
		<title>AddEventHandler</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=AddEventHandler&amp;diff=78575"/>
		<updated>2023-11-11T18:05:37Z</updated>

		<summary type="html">&lt;p&gt;Newton: remove wrong information about sourceResource and sourceResourceRoot global variables&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__ &lt;br /&gt;
{{Server client function}} &lt;br /&gt;
{{Important Note|Do '''NOT''' use the same name for your handler function as the event name, as this can lead to confusion if multiple handler functions are used. On the same note, for multiple reasons, it is '''NOT''' a good idea to export the same functions that you use locally as remote event handlers.}}&lt;br /&gt;
This function will add an [[event]] handler. An event handler is a function that will be called when the event it's attached to is triggered. See [[event system]] for more information on how the event system works.&lt;br /&gt;
{{Important Note|See code for this note below}}&lt;br /&gt;
&lt;br /&gt;
Event handlers are functions that are called when a particular event happens. Each event specifies a specific set of variables that are passed to the event handler and can be read by your function. The following global variables are available for use in handler functions:&lt;br /&gt;
*'''source''': the element that triggered the event&lt;br /&gt;
*'''this''': the element that the event handler is attached to&lt;br /&gt;
*'''sourceResource''': the resource that triggered the event.&lt;br /&gt;
*'''sourceResourceRoot''': the root element of the resource that triggered the event.&lt;br /&gt;
*'''client''': the client that triggered the event using [[triggerServerEvent]]. Not set if the event was not triggered from a client.&lt;br /&gt;
{{New_feature|3|1.0|&lt;br /&gt;
*'''eventName''': the name of the event which triggered the handler function.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
It is important to remember that events pass up and down the [[element tree]]. An event triggered on the root element is triggered on every element in the tree. An event triggered on any other element is triggered on its ancestors (its parent element and its parent's parent etc) and its children, grandchildren and great-grandchildren. You can use the ''propagate'' argument to specify if you wish your handler to receive events that have propagated up or down the tree.&lt;br /&gt;
&lt;br /&gt;
The order in which event handlers are triggered is undefined, you should not rely on one event handler being executed before another.&lt;br /&gt;
{{Note|See [[Script security]] for tips on preventing cheaters when using events and element data}}&lt;br /&gt;
{{Note|See [[Event_Source_Element|Event Source Element]] for a descriptive visualization of the event system handling an event trigger.}}&lt;br /&gt;
&lt;br /&gt;
Each function closure can only be added once to each event. On the second attempt to add the function closure to the same event a warning will be emitted to the debug console and the call to addEventHandler will fail.&lt;br /&gt;
==Syntax== &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool propagate = true, string priority = &amp;quot;normal&amp;quot; ] )    &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt; &lt;br /&gt;
&lt;br /&gt;
===Required Arguments=== &lt;br /&gt;
*'''eventName:''' The name of the [[event]] you want to attach the handler function to. '''Note: The maximum allowed length is 100 ASCII characters (that is, English letters and numerals)'''&lt;br /&gt;
*'''attachedTo:''' The [[element]] you wish to attach the handler to. The handler will only be called when the event it is attached to is triggered for this element, or one of its children. Often, this can be the root element (meaning the handler will be called when the event is triggered for ''any'' element).&lt;br /&gt;
*'''handlerFunction:''' The handler function you wish to call when the event is triggered. This function will be passed all of the event's parameters as arguments, but it isn't required that it takes all of them.&lt;br /&gt;
&lt;br /&gt;
===Optional Arguments===&lt;br /&gt;
*'''propagate:''' A boolean representing whether the handler will be triggered if the event was propagated down or up the [[element tree]] (starting from the source), and not triggered directly on attachedTo (that is, handlers attached with this argument set to ''false'' will only be triggered if ''source == this''). In GUI events you will probably want to set this to ''false''.&lt;br /&gt;
{{New_feature|3.0131|1.3.1|&lt;br /&gt;
*'''priority :''' A string representing the trigger order priority relative to other event handlers of the same name. Possible values are:&lt;br /&gt;
**'''&amp;quot;high&amp;quot;'''&lt;br /&gt;
**'''&amp;quot;normal&amp;quot;'''&lt;br /&gt;
**'''&amp;quot;low&amp;quot;'''&lt;br /&gt;
''It is also possible to add finer priority control by appending a positive or negative number to the priority string. For example (in priority order for reference): &amp;quot;high+4&amp;quot; &amp;quot;high&amp;quot; &amp;quot;high-1&amp;quot; &amp;quot;normal-6&amp;quot; &amp;quot;normal-7&amp;quot; &amp;quot;low+1&amp;quot; &amp;quot;low&amp;quot; &amp;quot;low-1&amp;quot;''&lt;br /&gt;
{{Important Note|Anything bound to a specific element will be run before other handlers that are bound to something higher in the element tree (like root) This means that &amp;quot;high+10&amp;quot; bound to root '''won't''' trigger before &amp;quot;normal&amp;quot; bound directly to an element.}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns ''true'' if the event handler was attached successfully. Returns ''false'' if the specified event could not be found or any parameters were invalid.&lt;br /&gt;
&lt;br /&gt;
==Remarks==&lt;br /&gt;
Due to the additional set of global variables, the event-trigger specific variables it is '''NOT a good idea to use the same function locally as well as directly as an event handler'''. Event handlers often make use of the source element variable which would often find no use in generic functions. Inside of server-side remote event handlers it is important to add protections against exploits due to unexpected client event triggers or network-based load situations while generic functions, due to being part of a controlled call-stack, do not in general face the same issues. It is recommended to adapt a '''good-natured distancing''' principle between code meant to run from local logic in separation to code meant to run from remote logic.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&amp;lt;!-- This code is wrong. It WILL crash the server eventually because it is registering a new event handler every time &amp;quot;eventName&amp;quot; event is called.&lt;br /&gt;
&amp;lt;section name=&amp;quot;Code for important note above&amp;quot; class=&amp;quot;Important&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This code might not work as you expect: The handler added here won't be called until the next time the event is triggered. On the other hand, removing events works as expected.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
addEvent(&amp;quot;eventName&amp;quot;)&lt;br /&gt;
addEventHandler(&amp;quot;eventName&amp;quot;, root, function()&lt;br /&gt;
    print(&amp;quot;Existing called&amp;quot;)&lt;br /&gt;
    addEventHandler(&amp;quot;eventName&amp;quot;, root, function()&lt;br /&gt;
        print(&amp;quot;newly added called&amp;quot;)&lt;br /&gt;
    end)&lt;br /&gt;
end)&lt;br /&gt;
-- first time calling the event&lt;br /&gt;
triggerEvent(&amp;quot;eventName&amp;quot;, root) -- prints &amp;quot;Existing called&amp;quot;&lt;br /&gt;
-- second time - now both handlers are called&lt;br /&gt;
triggerEvent(&amp;quot;eventName&amp;quot;, root) -- prints &amp;quot;Existing called&amp;quot;, &amp;quot;newly added called&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This serverside example sends a message to everyone in the server when a player spawns.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- define our handler function&lt;br /&gt;
function onPlayerSpawnHandler ( )&lt;br /&gt;
	-- get the player's name, source is the player because he was spawned&lt;br /&gt;
	local playerName = getPlayerName( source )&lt;br /&gt;
	-- output in the chat box that they've spawned&lt;br /&gt;
	outputChatBox ( playerName .. &amp;quot; has spawned!&amp;quot; )&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
addEventHandler( &amp;quot;onPlayerSpawn&amp;quot;, root, onPlayerSpawnHandler ) -- root is a predefined global variable for getRootElement()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
{{ChangelogHeader}}&lt;br /&gt;
{{ChangelogItem|1.3.0-9.03795|Added priority argument}}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Event_functions}}&lt;br /&gt;
[[ru:addEventHandler]]&lt;/div&gt;</summary>
		<author><name>Newton</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Predefined_variables_list&amp;diff=78574</id>
		<title>Predefined variables list</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Predefined_variables_list&amp;diff=78574"/>
		<updated>2023-11-11T15:34:45Z</updated>

		<summary type="html">&lt;p&gt;Newton: move sourceResource and sourceResourceRoot variables to shared section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Lua Predefined variables ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
_G -- returns a table of all global variables&lt;br /&gt;
coroutine -- returns a table containing functions for threads&lt;br /&gt;
debug -- returns a table containing debug functions&lt;br /&gt;
math -- returns a table that contains mathematical functions&lt;br /&gt;
string -- returns a table containing functions for strings&lt;br /&gt;
table -- returns a table that contains functions for tables&lt;br /&gt;
_VERSION -- returns a string of the version of lua in format &amp;quot;Lua 5.1&amp;quot;&lt;br /&gt;
self -- used in methods&lt;br /&gt;
arg -- used in functions which use '...' as an argument (https://www.lua.org/pil/5.2.html)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== MTA Predefined variables ==&lt;br /&gt;
=== Global ===&lt;br /&gt;
&amp;lt;section name=&amp;quot;Shared&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
exports -- returns a table of resource names containing all export functions&lt;br /&gt;
resource -- returns a resource element of the resource the snippet was executed in&lt;br /&gt;
resourceRoot -- returns a resource root element of the resource the snippet was executed in&lt;br /&gt;
root -- returns the root element of the server&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&amp;lt;section name=&amp;quot;Client only&amp;quot; class=&amp;quot;client&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
guiRoot -- returns the root element of all GUI elements.&lt;br /&gt;
localPlayer -- returns the player element of the local player.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Event Handlers ===&lt;br /&gt;
[https://wiki.multitheftauto.com/wiki/AddEventHandler More details about hidden variables in functions and events]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Shared&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
source -- The player or element the event was attached to&lt;br /&gt;
this -- Element, which was attached function-handler.&lt;br /&gt;
eventName -- the name of the event (&amp;quot;onResourceStart&amp;quot;, &amp;quot;onPlayerWasted&amp;quot; etc.)&lt;br /&gt;
sourceResource -- the resource that called the event&lt;br /&gt;
sourceResourceRoot -- the root of the resource that called the event&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server only&amp;quot; class=&amp;quot;server&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
client -- the client that called the event&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please note, the above pre-defined variables may only be accessible in the relevant scope (i.e, an event handler callback function). Further functions defined within that scope may not have access to these pre-defined variables at the time of their execution.&lt;br /&gt;
&lt;br /&gt;
For this reason, you should always explicitly redefine pre-defined variables as local variables, to ensure they are always available to new functions declared within that scope - for example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function init()&lt;br /&gt;
    local player = source -- source is a pre-defined variable for this event, containing the player element&lt;br /&gt;
&lt;br /&gt;
    local func = function()&lt;br /&gt;
        print(source) -- prints 'nil', source isn't available anymore&lt;br /&gt;
        print(player) -- prints player element&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    setTimer(func, 1000, 1) -- run above function after 1 second&lt;br /&gt;
end&lt;br /&gt;
addEventHandler(&amp;quot;onPlayerResourceStart&amp;quot;, root, init)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timer Callbacks ===&lt;br /&gt;
&amp;lt;section name=&amp;quot;Shared&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
sourceTimer -- current timer in callback function.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== HTTP ===&lt;br /&gt;
List Predefined variables available in the HTTP files [https://wiki.multitheftauto.com/wiki/Resource_Web_Access (more info about it)]:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;[php]&lt;br /&gt;
requestHeaders -- table, contains all HTTP headlines current page.&lt;br /&gt;
form -- table, contains all POST and GET settings, transferred current page.&lt;br /&gt;
cookies -- table, contains all COOKIE, transferred current page.&lt;br /&gt;
hostname -- string, contains IP or name host, which requested current page.&lt;br /&gt;
url -- string, URL current page.&lt;br /&gt;
user -- element, account user, which requested current page.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
[[Element_tree|Element Tree]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Scripting Concepts]]&lt;/div&gt;</summary>
		<author><name>Newton</name></author>
	</entry>
</feed>