Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit0a13e47b57

README.md

sceneInstantiable

Can be placed in a scene. A type that lists sceneInstantiable under interfaces: in its type.yaml guarantees that instantiate composes any of its assets under or onto an owning entity, synchronously, and returns the live root and the map naming what it spawned. A consumer places any such asset with one call, source:as("sceneInstantiable"):instantiate(target, opts), and it works for types the consumer has never heard of: the Asset component, a viewport drop, a procedural generator and a tool argument all place assets this way.

Implementing it

# marker.assetType/type.yaml
interfaces: [sceneInstantiable]
-- marker.assetType/behavior.luau
local Instantiable = require("@builtin::assetTypes.assetType.shared.instantiable")
local M = {}
M.ref = {
    instantiate = function(self, target, opts)
        local root = Instantiable.root(self, target, opts)
        root.component.add("Text3D", { text = self.name })
        return Instantiable.result(self, root, {})
    end,
}
return M

A type whose own instantiate means something else meets the member under M.interfaces.sceneInstantiable.instantiate.

The base inputs

target is the owning entity; with none, the type spawns a fresh root. opts.position, opts.rotation, opts.scale, opts.name and opts.temporary mean the same for every type, and Instantiable.root applies them to a root the type makes (Instantiable.place to one it adopts). opts.idMap, opts.diff and opts.sourceTag are what the Asset component passes to every type: a type that spawns no addressable children ignores them. opts.detached composes a permanent copy that nothing composes again. A type's own extras (raw, deferred, params) travel in opts beside these, are always optional, and never change a base input's meaning.

Required behaviour

  1. root exists on return, with everything composed under it.
  2. idMap maps each original id to the spawned id; {} for none.
  3. Passing the returned idMap back reproduces the same ids.
  4. temporary = true keeps the root and everything under it out of the saved scene.
  5. Despawning root, or the composition the Asset component despawns, removes everything the call put in effect.

The conformance suite, sceneInstantiableConformance beside this file, holds one asset of every implementing type (or of the types a run names) to the base options' meanings, the returned map reproducing its ids when passed back, and a detached composition's live root. asset.validate of a world's own type runs it over that type.

Legacy types

A type that defines M.ref.instantiate and lists nothing (one written before interfaces: existed, or defined by its behavior.luau alone) implements sceneInstantiable with a warning (interfaces.implied) naming the line to add.