Luawe All pages

Attributes

A name says which thing something is, and a tag says what kind of thing it is. An attribute is a value a thing keeps for itself: a door's Damage, an enemy's Health, the Key a lock waits for. Each attribute has a name and a value, and a thing can have up to 32 of them.

Every thing has its own. Two things with the same name do not share their attributes, and a copy of a thing starts with its original's, then goes its own way: changing one never changes the other.

Giving a thing attributes

In the editor, a thing's Attributes section has a row for each one. Add an attribute picks its kind, and the row holds its value, its name and its kind. Several things picked together show the attributes they all have, and a value set there goes to all of them.

A script reads one by its name, after thing.attributes:

local door = things.find("door")
print(door.attributes.Damage)

A name the thing has no attribute of gives nil. A name with a space in it is written in brackets, as a table's is: door.attributes["Max speed"].

Setting and taking off

Setting a value changes the attribute. A name the thing does not have yet adds one, and nil takes one off:

local door = things.find("door")

door.attributes.Damage = 7       -- changed
door.attributes.Opened = true    -- a new one
door.attributes.Opened = nil     -- taken off

What a script sets is the run's, as everything a script does to the level: the level file keeps the values the editor gave, and a new try starts from those.

Kinds of value

An attribute holds one of these kinds. They are the words the editor's Add an attribute list uses, and the kinds a marker's values have too:

KindExample
Numberdoor.attributes.Damage = 5
Booleandoor.attributes.Open = false
Stringdoor.attributes.Label = "Front door"
Thingdoor.attributes.Key = things.find("key")
Tabledoor.attributes.Loot = { "coin", "gem", Count = 3 }
Colordoor.attributes.Tint = color.rgb(255, 136, 0)
Gradientdoor.attributes.Fade = gradient.new({ [0] = { color.red, 1 }, [1] = { color.blue, 0.5 } })
Curvedoor.attributes.Swell = curve.new({ [0] = 0.2, [1] = { 0, 0.1 } })
Range2door.attributes.Reach = range2.new(1, 6)
Range3door.attributes.Health = range3.new(0, 100, 100)

A table is copied in when it is set and copied out when it is read, so changing the table a script holds afterwards changes nothing: set it again. A range is the thing's own, so door.attributes.Health.current -= 20 changes the attribute itself. Functions, events and the like can't be attributes.

Going through them all

for name, value in thing.attributes goes through every attribute a thing has:

local door = things.find("door")
for name, value in door.attributes do
    print(name, value)
end

Hearing a change

thing.attributeChanged happens when one of a thing's attributes is set to another value or taken off, by a script or by a Set property node. It gives the attribute's name and its value now (nil when it was taken off):

local door = things.find("door")
door.attributeChanged:connect(function(name, value)
    print(name .. " is now " .. tostring(value))
end)

Setting an attribute to the value it has already is no change, and nothing is heard. When the thing goes, everything that listens to it is let go of, as for its other events.

Attributes and nodes

Nodes read and set attributes too. A Set property node's Property list has the attributes of its things: the node gives the attribute its sum (a number, yes or no, or words in quotes), as the kind the attribute is; a range3 takes the number as its current. A sum reads one with thing("Door", "attributes.Damage").

The rules

  • A thing has up to 32 attributes. Their values hold 256 values between them at most, counting every entry of their tables.
  • A name is up to 64 bytes, with no space at either end. A string is up to 200 bytes. Tables go 3 deep, with 32 entries each.
  • A copy (a script's thing:clone(), a Copy node's, the editor's copy, paste and duplicate) starts with its original's attributes as they are at that moment.
  • A thing attribute set in the editor is that one thing whatever it is called later. If that thing is deleted in the editor, the attribute is emptied.

Next: the world and the tree.