1+ ---
2+ import AutoStarlightPage from ' @src/components/AutoStarlightPage.astro' ;
3+ import CodeExamplesSection from ' @src/components/CodeExamplesSection.astro' ;
4+ import { Code } from ' @astrojs/starlight/components' ;
5+ import { getSeeAlsoLinksFromList } from ' @src/utils/general' ;
6+ import SeeAlsoSection from ' @src/components/SeeAlsoSection.astro' ;
7+
8+ const sharedVars = [
9+ {
10+ path: ' ' ,
11+ side: ' shared' ,
12+ luaCode: ` exports -- returns a table of resource names containing all export functions
13+ resource -- returns a resource element of the resource the snippet was executed in
14+ resourceRoot -- returns a resource root element of the resource the snippet was executed in
15+ resourceName -- returns the name of the resource the snippet was executed in
16+ root -- returns the root element of the server `
17+ },
18+ ];
19+
20+ const clientVars = [
21+ {
22+ path: ' ' ,
23+ side: ' client' ,
24+ luaCode: ` guiRoot -- returns the root element of all GUI elements.
25+ localPlayer -- returns the player element of the local player. `
26+ },
27+ ];
28+
29+ const sharedEventVars = [
30+ {
31+ path: ' ' ,
32+ side: ' shared' ,
33+ luaCode: ` source -- The player or element the event was attached to
34+ this -- Element, which was attached function-handler.
35+ eventName -- the name of the event ("onResourceStart", "onPlayerWasted" etc.)
36+ sourceResource -- the resource that called the event
37+ sourceResourceRoot -- the root of the resource that called the event `
38+ },
39+ ];
40+
41+ const serverEventVars = [
42+ {
43+ path: ' ' ,
44+ side: ' server' ,
45+ luaCode: ` client -- the client that called the event `
46+ },
47+ ];
48+
49+ const sharedTimerVars = [
50+ {
51+ path: ' ' ,
52+ side: ' shared' ,
53+ luaCode: ` sourceTimer -- current timer in callback function. `
54+ },
55+ ];
56+ ---
57+
58+ <AutoStarlightPage frontmatter ={ {
59+ template: ' doc' ,
60+ title: ' Predefined variables list' ,
61+ tableOfContents: {
62+ maxHeadingLevel: 4
63+ }
64+ }} >
65+
66+ <div id =" _top" />
67+
68+ <h3 >Lua Predefined variables</h3 >
69+ <hr />
70+
71+ <Code lang =" lua" code =`
72+ _G -- returns a table of all global variables
73+ coroutine -- returns a table containing functions for threads
74+ debug -- returns a table containing debug functions
75+ math -- returns a table that contains mathematical functions
76+ string -- returns a table containing functions for strings
77+ table -- returns a table that contains functions for tables
78+ _VERSION -- returns a string of the version of lua in format "Lua 5.1"
79+ self -- used in methods
80+ arg -- used in functions which use '...' as an argument (https://www.lua.org/pil/5.2.html)
81+ ` />
82+
83+ <h3 >MTA Predefined variables</h3 >
84+ <hr />
85+ <h4 >Global</h4 >
86+ <CodeExamplesSection codeExamples ={ sharedVars } hideHeader />
87+ <CodeExamplesSection codeExamples ={ clientVars } hideHeader />
88+
89+ <h4 >Event Handlers</h4 >
90+ <a href =" /reference/addEventHandler" >More details about hidden variables in functions and events</a >
91+
92+ <CodeExamplesSection codeExamples ={ sharedEventVars } hideHeader />
93+ <CodeExamplesSection codeExamples ={ serverEventVars } hideHeader />
94+
95+ <p >Please note, the above pre-defined variables may only be accessible in the relevant scope (i.e, an event handler callback function).
96+ Further functions defined within that scope may not have access to these pre-defined variables at the time of their execution.
97+ <br /><br />
98+ For this reason, you should always explicitly redefine pre-defined variables as local variables,
99+ to ensure they are always available to new functions declared within that scope - for example:</p >
100+
101+ <Code lang =" lua" code =`function init()
102+ local player = source -- source is a pre-defined variable for this event, containing the player element
103+
104+ local func = function()
105+ print(source) -- prints 'nil', source isn't available anymore
106+ print(player) -- prints player element
107+ end
108+
109+ setTimer(func, 1000, 1) -- run above function after 1 second
110+ end
111+ addEventHandler("onPlayerResourceStart", root, init)` />
112+
113+ <h4 >Timer callbacks</h4 >
114+ <CodeExamplesSection codeExamples ={ sharedTimerVars } hideHeader />
115+
116+ <h4 >HTTP</h4 >
117+ <p >List Predefined variables available in the HTTP files (<a href =" /reference/Resource_Web_Access" >more info about it</a >).</p >
118+
119+ <Code lang =" lua" code =`requestHeaders -- table, contains all HTTP headlines current page.
120+ form -- table, contains all POST and GET settings, transferred current page.
121+ cookies -- table, contains all COOKIE, transferred current page.
122+ hostname -- string, contains IP or name host, which requested current page.
123+ url -- string, URL current page.
124+ user -- element, account user, which requested current page.` />
125+
126+ <SeeAlsoSection seeAlsoLinks ={ getSeeAlsoLinksFromList ([
127+ ' reference:Element_tree' ,
128+ ])} currentId =' ' />
129+
130+ </AutoStarlightPage >
0 commit comments