Skip to content

Commit 9583a70

Browse files
committed
Predefined variables page
1 parent 8cdf36e commit 9583a70

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

web/astro.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export default defineConfig({
7272
label: "Resource Web Access",
7373
link: "/reference/Resource_Web_Access",
7474
},
75+
{label: 'Predefined variables', link: '/reference/Predefined_variables'},
7576
{
7677
label: "Functions",
7778
collapsed: true,

web/src/components/CodeExamplesSection.astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ export interface Props {
1515
codeExamples: CodeExample[];
1616
examplesRequired?: boolean;
1717
theItem?: any;
18+
hideHeader?: boolean;
1819
}
1920
20-
const { codeExamples, examplesRequired, theItem } = Astro.props;
21+
const { codeExamples, examplesRequired, theItem, hideHeader } = Astro.props;
2122
2223
if (codeExamples.length === 0 && !examplesRequired) {
2324
return null;
2425
}
2526
---
2627

2728
<div class="examples-section">
28-
<h4>Code Examples</h4>
29+
{!hideHeader && (
30+
<h4>Code Examples</h4>
31+
)}
2932

3033
{codeExamples.length === 0 && theItem ? (
3134
<NeedsExample theItem={theItem} />
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)