Fields
CompilesA field is a variable declared at the top of a script, outside any function. Every componentcomponent: One script attached to one entity. The same script on ten entities is ten components, each with its own copy of the script's fields. made from the script gets its own copy, and fields live as long as the component does. They are how a script remembers things between one event and the next.
There are two kinds, and choosing between them is choosing between setting and state.
Constants: settings
Section titled “Constants: settings”// Seconds between greetings.let cooldown_seconds: float = 3.0
// Name of the pad to send players to.let destination_entity_name: string = ""
// A sound to play; picked in the level editor.let arrival_sound: Reference = nullA plain let field is a constantconstant: A field declared with plain let. The script cannot change it while running, but a level can set a different value for each placed component in the editor.. The value you write is the default. Whoever places
the component in a level sees every constant as a setting in the editor and can change it for that one placement:
one teleport pad can name plaza as its destination while the next names garage, both using the same script.
The script itself treats a constant as read-only. That’s what makes it a setting rather than state.
Mutables: state
Section titled “Mutables: state”let mut greeted: int = 0let mut last_greeting_ms: int = -100000let mut target: Component = nulllet mut visited: list = []let mut makes a mutablemutable: A field declared with let mut. The script's running state: it changes while the game runs.: the component’s running state. It starts at the value you give,
and your code changes it with =:
let mut greeted: int = 0
fn greet() { greeted = greeted + 1}Mutables are also what the network sends: when a component replicatesreplication: The server sending a component's changed fields to every client, so they all agree., it’s the mutables that travel. See Replication.
Choosing a starting value for time
Section titled “Choosing a starting value for time”A common pattern is remembering when something last happened, to wait before it happens again. Start that field far in the past, so the first time is never blocked:
let mut last_used_ms: int = -1000000Controlled fields
Section titled “Controlled fields”let mut controlled fade_timeline: Component = nulllet mut controlled is a mutable holding a component that your script takes control of when you store it,
the engine’s way of saying “this component answers to me”. It’s used for things like timelines and UI widgets that
one owner drives. Reading it is ordinary; each assignment also claims control. Controlled fields must be declared
at the top level, like any field.
Put all fields before the first function. The compiler accepts them anywhere, but every shipped script declares them first, and a reader expects to find a script’s settings and state at the top.
For programmers
Constants are instance fields with per-instance overrides serialised in the level, closer to Unity’s
[SerializeField] than to const. The compiler does not currently reject an assignment to a constant; treat
them as immutable anyway. Mutables are per-instance, default to replicated when the type replicates, and are the
only fields another script can read or write by name.