Skip to content

atl

import "atl"

AllTheLanguages TinyGo SDK: import atl and atl/sm (and the atl/sm/... packages you use) to get the whole sm API.

var SDK_VERSION = SdkVersion{
Major: 1,
Minor: 0,
Micro: 0,
}
func Defer(fn func())

Queues fn to run at the start of the next callback.

func CurrentSelf() int32

The script instance whose code is running, which owns new callbacks and GUIs.

func EndCall(f CallFrame)

Ends a call started with BeginCall.

func Dispatch(self int32, fn func() Value) int32

Runs fn as a call from the loader on behalf of script instance self, and returns fn's result as a new raw handle.

func BindCallback(fn Callback) (string, int)

Stores fn in a free slot and returns the name of a Lua method that calls it, and the slot id.

func UnbindCallback(id int)

Frees a callback slot. Safe to call from inside that callback.

func RegisterCleanup(hook CleanupHook) bool

Adds a cleanup hook. Returns true so it can initialize a package variable.

func ReleaseOwnedBy(self int32, destroying bool)

Runs the cleanup hooks for a script instance and frees the callbacks it owns.

func SetMod(m any)

Registers m as the mod-wide lifecycle object (see Mod).

func ModID() string

This mod's id.

func ToString(x any) string

Turns a value into text for Format and the logging functions: strings, booleans, integers in decimal, floats as Lua prints them, and Stringers.

func Format(format string, args ...any) string

Builds a string by replacing each "{}" in format with the next argument's ToString, e.g. Format("pos {} speed {}", pos, 12.5). Arguments beyond the last "{}" are ignored.

func Keep[T Wrapper, P interface {
*T
FromLua
}](x T) T

Keeps a game object (see Value.Keep).

func As[T any](v Value) T

Converts the value to T.

func FieldAs[T any](v Value, field string) T

t[field] converted to T.

func AsSlice[T any](v Value) []T

Converts an array table to a slice of T; anything but a table gives an empty slice.

func CallAs[R any](fn Value, args ...any) R

Calls fn and returns its first result as R. A Lua error traps the mod.

func MethodAs[R any](obj Value, name string, args ...any) R

Calls obj:name(args...) and returns its first result as R. A Lua error traps the mod.

type SdkVersion struct {
Major int32
Minor int32
Micro int32
}
type Callback func(args Value) Value

A stored callback: receives the Lua arguments as one Value, returns a Value.

type CallFrame struct {
// contains filtered or unexported fields
}

A call from the loader in progress (see BeginCall).

func BeginCall(self int32) CallFrame

Starts a call from the loader on behalf of script instance self and runs deferred work. Values created until the matching EndCall are freed by it.

type CleanupHook func(self int32, destroying bool)

Called when a script instance is destroyed or its client/server side goes away.

type Mod interface {
// [server] Called once when the mod starts.
ServerOnCreate()
// [server] Called every physics tick (40 per second); dt is in seconds.
ServerOnFixedUpdate(dt float64)
// [server] Called when the mod shuts down.
ServerOnDestroy()
// [client] Called once when the mod starts.
ClientOnCreate()
// [client] Called every physics tick.
ClientOnFixedUpdate(dt float64)
// [client] Called every frame.
ClientOnUpdate(dt float64)
// [client] Called when the mod shuts down.
ClientOnDestroy()
}

Mod-wide lifecycle, independent of any part: define any of these methods on a type and pass it to SetMod. The loader's runtime tool (or a custom game's script) calls these once per tick.

type Stringer interface {
String() string
}

Something that can turn itself into text for Format and the logging functions. Implement it on your own type to make it formattable.

type Type int32

The Lua type of a Value. Game objects (Shape, Vec3, ...) are TypeUserdata.

const (
TypeNil Type = iota
TypeBoolean
TypeNumber
TypeString
TypeTable
TypeFunction
TypeUserdata
TypeThread
)
type IntoLua interface {
IntoLua() Value
}

Converts a Go value to Lua. Implement it to pass your own types to Lua.

type FromLua interface {
FromLua(v Value)
}

Converts a Lua value to Go. Implement it on a pointer to read your own types from Lua. v is only valid during the call, unless the type is a Wrapper.

type Wrapper interface {
Value() Value
}

Implemented by Object and the game object types that embed it.

type Value struct {
// contains filtered or unexported fields
}

A handle to any Lua value: tables, functions, game objects, strings... A Value lives until the callback that created it returns; Keep it to store it for later.

func Adopt(h int32) Value

Takes ownership of a raw handle.

func Borrow(h int32) Value

Wraps a raw handle without taking ownership; it isn't freed.

func Global(path string) Value

Looks up a dotted global path, e.g. Global("sm.vec3.new").

func Table() Value

A new empty table.

func ValueOf(x any) Value

Converts a Go value to Lua.

func List[T any](items []T) Value

A 1-based array table holding the items.

func Cached(path string) Value

A Value for a global path, looked up once and cached, e.g. CallAsint.

Methods

func (v Value) Release() int32

Gives up ownership and returns a raw handle the caller must free.

func (v Value) Keep() Value

A copy that stays valid after the current callback returns, until it is freed with Free.

func (v Value) Free()

Frees the handle now. The Value and its copies can't be used afterwards.

func (v Value) Handle() int32

The raw handle.

func (v Value) IsNil() bool

True for nil.

func (v Value) Type() Type

The Lua type.

func (v Value) Is(t Type) bool

True when the type is t.

func (v Value) Truthy() bool

Lua truthiness: false only for nil and false.

func (v Value) ToNumber() float64

The value as a number (0 if it isn't one).

func (v Value) String() string

Lua's tostring() of the value.

func (v Value) Field(field string) Value

t[field].

func (v Value) Index(index int) Value

t[index]. Lua indices start at 1.

func (v Value) Get(key Value) Value

t[key] with any key.

func (v Value) Number(field string) float64

t[field] as a number, without creating a handle.

func (v Value) Set(field string, x any) Value

t[field] = x. Returns v for chaining.

func (v Value) SetIndex(index int, x any) Value

t[index] = x. Returns v for chaining.

func (v Value) Len() int

The length operator #t.

func (v Value) Append(x any) Value

Appends x at t[#t + 1].

func (v Value) Pairs() func(yield func(key, value Value) bool)

Iterates the table's key/value pairs: for k, v := range t.Pairs().

func (v Value) Call(args ...any) Value

Calls the value as a function and returns its first result. A Lua error traps the mod.

func (v Value) Method(name string, args ...any) Value

Calls value:name(args...) and returns its first result. A Lua error traps the mod.

func (v Value) Invoke(args ...any)

Calls the value as a function, ignoring its results. A Lua error traps the mod.

func (v Value) InvokeMethod(name string, args ...any)

Calls value:name(args...), ignoring its results. A Lua error traps the mod.

func (v Value) TryCall(args ...any) ([]Value, bool)

Calls the value as a function and returns every result, or false if it raised an error, so the mod can handle the failure itself.

func (v Value) MethodAll(name string, args ...any) []Value

Calls value:name(args...) and returns every result.

func (v Value) CallAll(args ...any) []Value

Calls the value as a function and returns every result.

func (v Value) Equals(o Value) bool

Lua equality (==).

type Object struct {
// contains filtered or unexported fields
}

Base type of the game object wrappers (Shape, Body, Player, ...). Holds the Lua object and calls its methods.

func NewObject(v Value) Object

Wraps a Lua object.

Methods

func (o Object) Value() Value

The wrapped Lua value.

func (o Object) Valid() bool

True when the wrapper holds an object (it may still have been destroyed).

func (o Object) Exists() bool

True when the object still exists in the game (sm.exists).

func (o Object) Equals(other Wrapper) bool

Same object.

func (o Object) String() string

tostring() of the Lua object.

func (o *Object) FromLua(v Value)

Wraps the Lua object v.