Skip to content

Commit 82ec70b

Browse files
Add Optional_Arguments note and page
1 parent 5757193 commit 82ec70b

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

types/nil.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
description: |
2+
*Nil* is a type with a single value, *nil*, whose main property is to be different from any other value. A global variable has a *nil* value by default, before a first assignment, and you can assign *nil* to a global variable to delete it. Lua uses *nil* as a kind of non-value, to represent the absence of a useful value.
3+
4+
Reference: [Nil in Programming in Lua](https://www.lua.org/pil/2.1.html)

web/src/pages/Optional_Arguments.mdx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
2+
import { getSeeAlsoLinksFromList } from '@src/utils/general';
3+
import SeeAlsoSection from '@src/components/SeeAlsoSection.astro';
4+
5+
<StarlightPage frontmatter={{
6+
template: 'doc',
7+
title: 'Optional Arguments',
8+
tableOfContents: false,
9+
}}>
10+
11+
**Optional arguments** are arguments which can be optionally passed to any Lua function, including MTA\:SA built-in ones. They are not required for the function to run, and can be unset ([`value == nil`](/nil)). Often, this kind of arguments do not make a huge difference in the behaviour of the function.
12+
13+
It's a Lua convention that the arguments written between square brackets are optional, so in this Wiki you can find functions with some arguments between square brackets too. That means they are optional and that providing them is not necessary.
14+
15+
Let's take a look at this common function:
16+
17+
```lua
18+
vehicle createVehicle ( int model, float x, float y, float z, [ float rx, float ry, float rz ] )
19+
```
20+
21+
In this example, `rx`, `ry`, and `rz` are optional arguments, because they are between square brackets. They do not need to be provided to the function; it will default to no rotation in every axis. If provided, the function will use the rotations specified instead.
22+
23+
### Why are optional arguments used?
24+
25+
More often than not, it's boring having to provide all the arguments of a certain function, especially if they are the same over and over again. The optional arguments allow the scripter to only pass the data that is really needed for their script, and that helps improve code readability and size.
26+
27+
### Use of the optional arguments
28+
29+
Optional arguments are used just like normal arguments. The only difference is that they will default to a certain value if not provided (in other words, `if optionalArgument == nil then optionalArgument = defaultValue`).
30+
31+
One common problem for new scripters is when they want to provide only an optional argument, without setting the ones that are before it. Well, this is really simple to "fix". Normally, they can be set to `nil`, so they will default to their corresponding values while the scripter is still able to set the one they really want. For example, if you want to only set the rotation in the Z axis of the `createVehicle` function (and don't know that `rx` and `ry` default to zero):
32+
33+
```lua
34+
vehicle createVehicle ( int model, float x, float y, float z, [ float rx, float ry, float rz ] )
35+
```
36+
37+
You can use:
38+
39+
```lua
40+
local myAwesomeVehicle = createVehicle( getVehicleModelFromName( "Infernus" ), 0, 0, 5, nil, nil, 180 )
41+
```
42+
43+
And the result will be an Infernus rotated 180 degrees in the Z axis in the center of the map.
44+
45+
> ⚠️ **Warning**
46+
> There are some built-in MTA\:SA functions that don't follow this convention, especially when an optional argument is closely related to another (e.g. color arguments in DX functions, and probably others): instead of replacing the `nil` value, they output a 'bad argument' error and do nothing.
47+
> **Always check if this works with a function before using it in a script.**
48+
49+
### Scripting custom functions with optional arguments
50+
51+
Scripting functions with optional arguments is as easy as using `if` statements:
52+
53+
```lua
54+
function aNotSoUsefulFunction( text )
55+
if type( text ) == "string" or type( text ) == "nil" then -- Check if correct arguments have been provided
56+
local realTextToOutput
57+
if text == nil then
58+
realTextToOutput = "I <3 optional arguments" -- If the text is nil (not specified), use a default one
59+
else
60+
realTextToOutput = text -- Use the text that the user specified if defined
61+
end
62+
outputChatBox( realTextToOutput ) -- Output the text
63+
end
64+
end
65+
```
66+
67+
You can also use [short-circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation), if you prefer to:
68+
69+
```lua
70+
function aNotSoUsefulFunction( text )
71+
local realTextToOutput = ( type( text ) == "string" or type( text ) == "nil" ) and ( type( text ) == "string" and text or "I <3 optional arguments" ) or nil
72+
if realTextToOutput then -- Check if there is something to output
73+
outputChatBox( realTextToOutput ) -- Output the text
74+
end
75+
end
76+
```
77+
78+
79+
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksFromList([
80+
'article:Scripting_Introduction',
81+
])} currentId='' />
82+
83+
</StarlightPage>

web/src/pages/[func].astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
164164
{syntax.parameters.some((param: any) => param.default) && (
165165
<>
166166
<h5>Optional Arguments</h5>
167+
<p><strong>NOTE</strong>: When using <a href="/Optional_Arguments">optional arguments</a>, you might need to supply all arguments before the one you wish to use.</p>
167168
<ul>
168169
{syntax.parameters
169170
.filter((param: any) => param.default)

0 commit comments

Comments
 (0)