Arrays / Tables
In Lua, an Array is literally just a Table.
Arguments are the values provided within a function call, for example:
1 | local MyFunction = function (ArgumentONE, ArgumentTWO, ArgumentTHREE) |
2 | return (ArgumentONE^ 2 + ArgumentTWO^ 2 = = ArgumentTHREE^ 2 ) |
A table is an object that can contain key-value pairs:
With tables, we can also hardcode definitions in by defining the key and value in the table before hand:
Make sure to have a semicolon (;) or a comma (,) to seperate keys! Whitespace will not do it!
Parameters are the variables that are to be defined when the function is called:
function newFunction(a, b, c)
: in this case, a, b, and c are all parameters.
newFunction(a,b,c)
: in this case, a, b, and c are all arguments.
Tuples
Tuples are an ordered set of values that can be assigned to various other variables.
For example, let's say I have a function.
1 | function myNewFunction(adjacent,opposite,hypotenuse) |
2 | return adjacent^ 2 + opposite^ 2 , adjacent^ 2 + opposite^ 2 = = hypotenuse |
In the return value, I return two values (seperated by commas, NOT semicolons.)
I then call the function and assign the tuple to one value.
1 | local VariableONE = myNewFunction( 2 , 2 , 8 ); |
VariableONE is only set to the result of the pythagorean theorem, and not both. The amount of values captured is equal to the amount of variables set "equal" to the tuple.
So, if I wanted to capture all two values of the tuple, I'd use two arguments in this case:
1 | local VariableONE, variableTWO = myNewFunction( 2 , 2 , 8 ); |
3 | print (VariableONE, variableTWO) |
This example will print "8" and "true" to console.
Variants
A variable is called a "variant" when the return value is not defined and set in stone. For example, a function that returns a random type (instance, bool, or number) is a variant.
For example:
--variant function MyFunction, function type is determined by the return value
02 | local MyRandomValue = math.random( 100 ); |
03 | if (MyRandomValue < 100 / 3 ) then |
05 | elseif (MyRandomValue > 100 / 3 and MyRandomValue < 100 / 2 ) then |
08 | return Instance.new( 'Part' , workspace) |
Since the return value of the function is random (meaning we can't accurately measure the value by convenient measures), the return value is a variant.
That's why when you see a function, sometimes it says
variant MyFunction()
, because it returns a Variant value.
Pretty neat, huh?