Skip to content

Field

Global · Field · server and client scripts

Field builds typed field and signal parameter descriptors for World.DefineComponent and Net message definitions. Plain default values in fields also define primitive fields directly: number, boolean, string, vector (Vector3), Vector2, Color, Rotation, and UIVector.

Entity

PropertyRead only
Entity: FieldDesc<Entity?>

Entity declares an optional entity reference field. Call Field.Entity({ OnTargetDestroyed = "Clear" }) to clear the reference automatically when the target entity is destroyed, or "Keep" to retain the dead handle.

Example

-- Server script.
local LockedTarget = World.DefineComponent("LockedTarget", {
    Actor = Field.Entity({ OnTargetDestroyed = "Clear" }),
})
print(LockedTarget)

List

Property
List: <T>(item: T) -> { T }

List declares a variable-length array field whose elements match the item template item.

Example

-- Server script.
local Tags = World.DefineComponent("Tags", {
    Items = Field.List(""),
})
print(Tags)

Enum

Function
Enum(values: { string }, default: string) -> string

Enum declares a string field restricted to the allowed values list, with initial value default.

Example

-- Server script.
local Squad = World.DefineComponent("Squad", {
    Side = Field.Enum({ "Red", "Blue" }, "Red"),
})
print(Squad)

Optional

Property
Optional: <T>(item: T) -> T?

Optional declares a nullable field that holds either a value matching item or nil.

Example

-- Server script.
local Buff = World.DefineComponent("Buff", {
    Label = Field.Optional(""),
})
print(Buff)

Buffer

Function
Buffer(maxBytes: number) -> buffer

Buffer declares a binary buffer field capped at maxBytes bytes for Net message definitions. Components do not accept Field.Buffer; use a Store or a Net event for binary data.

Example

-- Server script.
local SyncChunk = Net.ToClients("SyncChunk", {
    Bytes = Field.Buffer(256),
})
print(SyncChunk)

Arg

Property
Arg: <T>(name: string, item: T) -> T

Arg names a single parameter in a component's ComponentOptions.Signals declaration, pairing parameter name with an item template item.

Example

-- Server script.
local Alert = World.DefineComponent("Alert", {
    Level = 1,
}, {
    Signals = {
        Triggered = { Field.Arg("reason", "") },
    },
})
print(Alert)