Skip to main content

Fields

A field is a property of an entity that can store a value.

field hero.Health
pub fn hero.Example() {
hero.Health = 100
Health = 75 // `hero` is implicit
Transmission { "Your health is: " + Health }
delete Health // delete the field
}

Declaration

Every field must be declared with an object parameter. The object parameter designates the entity on which the value will be stored. The object parameter can be any identifier beginning with a lowercase letter, or World to make a global field. When accessing the field, the object will be found implicitly from context if not specified explicitly.

A field's name must begin with an uppercase letter.

field hero.Health
field World.NumBananas

Fields can be declared as public using the pub keyword, which allows them to be accessed from all files in the program. Otherwise they are private and can only be accessed from within the same file. If two private fields have the same name but are in different files, they are not the same field and will not interfere with each other.

pub field hero.Health
pub field World.NumBananas

Working with fields

Reading a field using its name. The object will be found implicitly if not specified explicitly.

let value = Health // implicit object (recommended)
let value = hero.Health // or explicit object

Write to a field by using an assignment statement =.

Health = 100 // implicit object (recommended)
hero.Health = 100 // or explicit object

Fields can also be deleted to remove their value from memory.

delete Health // implicit object (recommended)
delete hero.Health // or explicit object

IDs

When a field is declared with an ID, it stores multiple values that can only be accessed with when using the same ID. This enables code to be more generic.

The ID may be optional.

pub symbol HeadSlot
pub symbol HandSlot
field hero.Inventory<Id>

pub fn hero.StoreItem<Id>(item) {
hero.Inventory<Id> = item
}

pub fn hero.Example() {
StoreItem<HandSlot>($swordOrRighteousness)
StoreItem<HeadSlot>($helmetOfWisdom)
}