Luawe All pages

Types

Every value has a type: what type says it is. A number is a number, a word in quotes a string, a thing of the level a thing, the event a thing has when it is touched an event, an item like cat.Ease.quadratic an Ease.

The editor follows the types of a script's values as it is written, so it can offer the right names after a dot and mark a value that can't be right. Most of the time there is nothing to write for that:

for _, part in world:allInside() do
    part.alpha = 0.5 -- the editor knows part is a thing, and offers alpha, tween, name...
end

world.thingAdded:connect(function(added)
    print(added.name) -- ...and that thingAdded gives its function a thing
end)

It knows what every function of the game takes and gives (the reference shows it for each, as a small table), what a variable was given, what a for loop goes through, and what an event hands its functions.

Saying what a value is

Where the editor can't see it, a script can say what a name is, with a : and the type after the name:

local door: thing or nil = things.find("Door")
local names: list of string = {}
local weather = cat.new({ "sunny", "rainy" })
local today: weather = weather.sunny

function hurt(who: thing, amount: number): number
    return amount * 2
end

local crate = {}
function crate:open(by: thing): boolean
    return by ~= nil
end

for _, part: thing in world:allInside() do
end
  • After a name being made (a variable, a function's value, a for loop's variable), : type says what it is.
  • After a function's brackets, : type says what it gives back; ): number, string for two.
  • A type is one of the words type gives (number, string, boolean, table, function, nil, thing, world, level, player, camera, event, connection, color, gradient, curve, range2, range3, random, sound, shader, marker, maid, task), a category's name (Ease, or weather for one of your own), cat (a category itself), or any.
  • list of string is a list whose items are strings. thing or nil is a thing, or nothing: or joins types.

Calling a method is as it always was: door:tween(...), crate:open(door). A : says a type only right after a name being made, or after a function's brackets.

A type said this way is for the editor only: the editor checks it, and the script never does. The script runs the same without it, and it costs nothing while it runs. A wrong value is not stopped at the :: it goes on, and stops later, where the script uses it as what it is not. The editor uses it to offer names, and marks (in yellow) a value given that can't be of that type:

local count: number = "five" -- marked: count is a number, and this is a string

Asking what a value is

is asks, while the script runs, whether a value is of a type: it is true or false.

local function describe(value)
    if value is thing then
        return "a thing called " .. value.name
    elseif value is number then
        return "the number " .. value
    end
    return "something else"
end
print(describe(5), describe("hi"))

It prints

the number 5 something else

Inside if value is thing then, the editor knows value is a thing. After is comes one type (or list, which asks for a table): value is thing or value is nil asks two things.

What the editor marks

The editor never stops a script for a type. It marks, in yellow:

  • a value that can't be what a function takes: tween(door, {}, "slow") (its seconds are a number);
  • a word where a category's item is wanted: tween(door, {}, 1, "quadd");
  • a word where an item is wanted, with the item to write: write cat.Ease.quadratic: an item, not the word "quadratic";
  • a name a value doesn't have, when it is nearly one it has: door.colr (did you mean color?).

Where it doesn't know a value's type, it says nothing. is and of are only words of their own where a type follows them: a variable called is still works as before.