Predefined variables list: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
(26 intermediate revisions by 10 users not shown)
Line 1: Line 1:
'''Server variables'''
== Lua Predefined variables ==
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
_G -- Table, contains all global variables.
_G -- returns a table of all global variables
_VERSION -- Version LUA in server.
coroutine -- returns a table containing functions for threads
coroutine -- Table, contains functions for thread.
debug -- returns a table containing debug functions
debug -- Table, contains debug functions.
math -- returns a table that contains mathematical functions
exports -- Table, contains all export functions server.
string -- returns a table containing functions for strings
math -- Table, contains mathematical functions.
table -- returns a table that contains functions for tables
resource -- Element, current resource.
_VERSION -- returns a string of the version of lua in format "Lua 5.1"
resourceRoot -- Element, root element current resource.
self -- used in methods
root -- Element, root element server.
arg -- used in functions which use '...' as an argument (https://www.lua.org/pil/5.2.html)
string -- Table, contains string functions.
table -- Table, contains functions for tables.
</syntaxhighlight>
</syntaxhighlight>
== MTA Predefined variables ==
=== Global ===
<section name="Shared" class="both" show="true">
<syntaxhighlight lang="lua">
exports -- returns a table of resource names containing all export functions
resource -- returns a resource element of the resource the snippet was executed in
resourceRoot -- returns a resource root element of the resource the snippet was executed in
root -- returns the root element of the server
</syntaxhighlight>
</section>
</section>
'''Client variables'''
<section name="Client only" class="client" show="true">
<section name="Client" class="client" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
guiRoot -- Element, root element all GUI elements.
guiRoot -- returns the root element of all GUI elements.
localPlayer -- Element, local player.
localPlayer -- returns the player element of the local player.
_G -- Table, contains all global variables.
_VERSION -- Version LUA in client.
coroutine -- Table, contains functions for thread.
debug -- Table, contains debug functions.
exports -- Table, contains all export functions client.
math -- Table, contains mathematical functions.
resource -- Element, current resource.
resourceRoot -- Element, root element current resource.
root -- Element, root element client.
string -- Table, contains string functions.
table -- Table, contains functions for tables.
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


List hidden local variables, that can be in functions-handlers:


'''Server / Client'''
=== Event Handlers ===
<section name="Server/Client" class="both" show="true">
[https://wiki.multitheftauto.com/wiki/AddEventHandler More details about hidden variables in functions and events]
 
<section name="Shared" class="both" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
source -- Element, who call event.
source -- The player or element the event was attached to
this -- Element, which was attached function-handler.
this -- Element, which was attached function-handler.
sourceResource -- Resource, which was called event.
eventName -- the name of the event ("onResourceStart", "onPlayerWasted" etc.)
sourceResourceRoot -- Element, root element resource, which was called event.
client -- Client, which was called event. If event called not client - not used.
eventName -- Name event, which called function-handler.
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


List create user [http://forum.mtasa.com/memberlist.php?mode=viewprofile&u=59875 Fro], [http://wiki.multitheftauto.com/wiki/AddEventHandler More details on the functios-handlers.]
<section name="Server only" class="server" show="true">
<syntaxhighlight lang="lua">
client -- the client that called the event
sourceResource -- the resource that called the event
sourceResourceRoot -- the root of the resource that called the event
</syntaxhighlight>
</section>
 
 
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.
 
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:
 
<syntaxhighlight lang="lua">
function init()
    local player = source -- source is a pre-defined variable for this event, containing the player element
 
    local func = function()
        print(source) -- prints 'nil', source isn't available anymore
        print(player) -- prints player element
    end
 
    setTimer(func, 1000, 1) -- run above function after 1 second
end
addEventHandler("onPlayerResourceStart", root, init)
</syntaxhighlight>
 
=== Timer Callbacks ===
<section name="Shared" class="both" show="true">
<syntaxhighlight lang="lua">
sourceTimer -- current timer in callback function.
</syntaxhighlight>
</section>
 
 
=== HTTP ===
List Predefined variables available in the HTTP files [https://wiki.multitheftauto.com/wiki/Resource_Web_Access (more info about it)]:
<syntaxhighlight lang="lua">[php]
requestHeaders -- table, contains all HTTP headlines current page.
form -- table, contains all POST and GET settings, transferred current page.
cookies -- table, contains all COOKIE, transferred current page.
hostname -- string, contains IP or name host, which requested current page.
url -- string, URL current page.
user -- element, account user, which requested current page.
</syntaxhighlight>


[[Element_tree]]


If list is broken or change - write down update list and this post will be updated.
== See Also ==
[[Element_tree|Element Tree]]


Original post by [http://forum.mtasa.com/memberlist.php?mode=viewprofile&u=45053 MX_Master]
[[Category:Scripting Concepts]]
[http://forum.mtasa.com/viewtopic.php?f=141&t=37420 link]

Revision as of 21:45, 13 December 2021

Lua Predefined variables

_G -- returns a table of all global variables
coroutine -- returns a table containing functions for threads
debug -- returns a table containing debug functions
math -- returns a table that contains mathematical functions
string -- returns a table containing functions for strings
table -- returns a table that contains functions for tables
_VERSION -- returns a string of the version of lua in format "Lua 5.1"
self -- used in methods
arg -- used in functions which use '...' as an argument (https://www.lua.org/pil/5.2.html)


MTA Predefined variables

Global

Click to collapse [-]
Shared
exports -- returns a table of resource names containing all export functions
resource -- returns a resource element of the resource the snippet was executed in
resourceRoot -- returns a resource root element of the resource the snippet was executed in
root -- returns the root element of the server
Click to collapse [-]
Client only
guiRoot -- returns the root element of all GUI elements.
localPlayer -- returns the player element of the local player.


Event Handlers

More details about hidden variables in functions and events

Click to collapse [-]
Shared
source -- The player or element the event was attached to
this -- Element, which was attached function-handler.
eventName -- the name of the event ("onResourceStart", "onPlayerWasted" etc.)
Click to collapse [-]
Server only
client -- the client that called the event
sourceResource -- the resource that called the event
sourceResourceRoot -- the root of the resource that called the event


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.

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:

function init()
    local player = source -- source is a pre-defined variable for this event, containing the player element

    local func = function()
        print(source) -- prints 'nil', source isn't available anymore
        print(player) -- prints player element
    end

    setTimer(func, 1000, 1) -- run above function after 1 second
end
addEventHandler("onPlayerResourceStart", root, init)

Timer Callbacks

Click to collapse [-]
Shared
sourceTimer -- current timer in callback function.


HTTP

List Predefined variables available in the HTTP files (more info about it):

[php]
requestHeaders -- table, contains all HTTP headlines current page.
form -- table, contains all POST and GET settings, transferred current page.
cookies -- table, contains all COOKIE, transferred current page.
hostname -- string, contains IP or name host, which requested current page.
url -- string, URL current page.
user -- element, account user, which requested current page.


See Also

Element Tree