atl
import "atl"AllTheLanguages TinyGo SDK: import atl and atl/sm (and the atl/sm/... packages you use) to get the whole sm API.
Variables
Section titled "Variables"var SDK_VERSION = SdkVersion{ Major: 1, Minor: 0, Micro: 0,}Functions
Section titled "Functions"func Defer(fn func())Queues fn to run at the start of the next callback.
func CurrentSelf() int32The 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) int32Runs 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) boolAdds 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() stringThis mod's id.
func ToString(x any) stringTurns 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) stringBuilds 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) TKeeps a game object (see Value.Keep).
func As[T any](v Value) TConverts the value to T.
func FieldAs[T any](v Value, field string) Tt[field] converted to T.
func AsSlice[T any](v Value) []TConverts an array table to a slice of T; anything but a table gives an empty slice.
func CallAs[R any](fn Value, args ...any) RCalls fn and returns its first result as R. A Lua error traps the mod.
func MethodAs[R any](obj Value, name string, args ...any) RCalls obj:name(args...) and returns its first result as R. A Lua error traps the mod.
SdkVersion
Section titled "SdkVersion"type SdkVersion struct { Major int32 Minor int32 Micro int32}Callback
Section titled "Callback"type Callback func(args Value) ValueA stored callback: receives the Lua arguments as one Value, returns a Value.
CallFrame
Section titled "CallFrame"type CallFrame struct { // contains filtered or unexported fields}A call from the loader in progress (see BeginCall).
func BeginCall(self int32) CallFrameStarts 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.
CleanupHook
Section titled "CleanupHook"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.
Stringer
Section titled "Stringer"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 int32The Lua type of a Value. Game objects (Shape, Vec3, ...) are TypeUserdata.
const ( TypeNil Type = iota TypeBoolean TypeNumber TypeString TypeTable TypeFunction TypeUserdata TypeThread)IntoLua
Section titled "IntoLua"type IntoLua interface { IntoLua() Value}Converts a Go value to Lua. Implement it to pass your own types to Lua.
FromLua
Section titled "FromLua"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.
Wrapper
Section titled "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) ValueTakes ownership of a raw handle.
func Borrow(h int32) ValueWraps a raw handle without taking ownership; it isn't freed.
func Global(path string) ValueLooks up a dotted global path, e.g. Global("sm.vec3.new").
func Table() ValueA new empty table.
func ValueOf(x any) ValueConverts a Go value to Lua.
func List[T any](items []T) ValueA 1-based array table holding the items.
func Cached(path string) ValueA Value for a global path, looked up once and cached, e.g. CallAsint.
Methods
func (v Value) Release() int32Gives up ownership and returns a raw handle the caller must free.
func (v Value) Keep() ValueA 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() int32The raw handle.
func (v Value) IsNil() boolTrue for nil.
func (v Value) Type() TypeThe Lua type.
func (v Value) Is(t Type) boolTrue when the type is t.
func (v Value) Truthy() boolLua truthiness: false only for nil and false.
func (v Value) ToNumber() float64The value as a number (0 if it isn't one).
func (v Value) String() stringLua's tostring() of the value.
func (v Value) Field(field string) Valuet[field].
func (v Value) Index(index int) Valuet[index]. Lua indices start at 1.
func (v Value) Get(key Value) Valuet[key] with any key.
func (v Value) Number(field string) float64t[field] as a number, without creating a handle.
func (v Value) Set(field string, x any) Valuet[field] = x. Returns v for chaining.
func (v Value) SetIndex(index int, x any) Valuet[index] = x. Returns v for chaining.
func (v Value) Len() intThe length operator #t.
func (v Value) Append(x any) ValueAppends 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) ValueCalls the value as a function and returns its first result. A Lua error traps the mod.
func (v Value) Method(name string, args ...any) ValueCalls 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) []ValueCalls value:name(args...) and returns every result.
func (v Value) CallAll(args ...any) []ValueCalls the value as a function and returns every result.
func (v Value) Equals(o Value) boolLua equality (==).
Object
Section titled "Object"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) ObjectWraps a Lua object.
Methods
func (o Object) Value() ValueThe wrapped Lua value.
func (o Object) Valid() boolTrue when the wrapper holds an object (it may still have been destroyed).
func (o Object) Exists() boolTrue when the object still exists in the game (sm.exists).
func (o Object) Equals(other Wrapper) boolSame object.
func (o Object) String() stringtostring() of the Lua object.
func (o *Object) FromLua(v Value)Wraps the Lua object v.