Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Since roblox new wiki has lack of lua doc i ask this:?

Asked by 5 years ago

What is: Variant, Tuple, Table, Arguments, Parameters, array

its been long time scripting but i have decided to return scripting but since the old wiki doesn't exists anymore

1 answer

Log in to vote
3
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

Arrays / Tables

In Lua, an Array is literally just a Table.

Arguments are the values provided within a function call, for example:

local MyFunction = function(ArgumentONE, ArgumentTWO, ArgumentTHREE)
    return (ArgumentONE^2 + ArgumentTWO^2 == ArgumentTHREE^2)

A table is an object that can contain key-value pairs:


local Table = {}; --Tables are initialized using curly brackets. --We can then add stuff using the table using keys or dot notation: Table.Fat = true; print(Table.Fat) -->> true; Table['Egg'] = 'green'; print(Table['Egg']) -->> 'green'

With tables, we can also hardcode definitions in by defining the key and value in the table before hand:


local Table = { Green = true; Eggs = false; And = true; Ham = false; };

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.

function myNewFunction(adjacent,opposite,hypotenuse)
    return adjacent^2 + opposite^2, adjacent^2 + opposite^2 == hypotenuse --return two values: the result of the pythagorean theorem, and if it's a valid triangle. 
end

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.

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:

local VariableONE, variableTWO = myNewFunction(2,2,8);
--Now, if I print variableONE, variableTWO:
print(VariableONE, variableTWO) --hey, look! we also provide a tuple in this print by seperating by commas!

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

function MyFunction()
    local MyRandomValue = math.random(100);
    if (MyRandomValue < 100/3) then
        return true;
    elseif (MyRandomValue > 100/3 and MyRandomValue < 100/2) then
        return 6.9; --nice
    else
        return Instance.new('Part', workspace)
    end
end;

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?

Ad

Answer this question