Tables

A table is Lunaris’ general-purpose container for storing data.

Use a table when you want to group data together without defining a named class.

Creating a table

local player = {
    Name = "Lithrun",
    Health = 100
}

print(player.Name) // Lithrun
print(player.Health) // 100

Accessing Fields

Table fields can be read and changed with . access.

local player = {
    Name = "Lithrun"
}

player.Name = "Lithrun"
print(player.Name) // Lithrun

Modifying Fields

Table fields can dynamically be added / removed.

local player = { }
player.Name = "Lithrun" // Adds a new field "Name" with "Lithrun" as the value

print(player.Health) // nil as non-existent table fields return nil
player.Name = nil // The "Name" field is now removed

Nested Tables

A table can also contain other tables.

local quest = {
    Title = "The First Step",
    Rewards = {
        Gold = 100,
        Experience = 25
    }
}

print(quest.Rewards.Gold) // 100

Structured data

Tables are flexible, but they do not enforce structure by themselves. If you want stronger structure and behavior, use a Class instead.

Categories:

Updated: