Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commit44fd9e59bd

README.md

entityOps.spawn

Spawn entities from a template — you name the components they carry and their field values, plus a transform.

Many entities in ONE call

Pass a { defaults, items } list: defaults holds what every entity shares, each items entry overrides only what differs. A six-walled room is one call, not six.

entityOps.spawn({
  defaults = { components = { Model = { model = "cube" }, BoxCollider = {} },
               scale = { 4, 3, 0.2 } },
  items = {
    { name = "wall_n", position = { 0, 1.5, -2 } },
    { name = "wall_s", position = { 0, 1.5,  2 } },
    { name = "wall_e", position = { 2, 1.5,  0 }, rotation = { 0, 90, 0 } },
    { name = "wall_w", position = {-2, 1.5,  0 }, rotation = { 0, 90, 0 } },
  },
})
  • A per-item key wins over the matching defaults key.
  • components merges per component: an item naming Model replaces only that entry and keeps the shared Collider.
  • Every item is validated before any entity is created — a typo in the last entry fails the whole call rather than leaving a half-built scene.
  • An unrecognised key is an error naming the accepted set, at the top level, in defaults, and in an item alike.
  • Returns one { id, name, components } record per item, in item order.

One entity

entityOps.spawn(
  { Model = { model = "cube" }, BoxCollider = {} },
  { position = {0, 1, 0} })

entityOps.spawn(
  { Light = { kind = "point", intensity = 5 } },
  { name = "lamp", position = {0, 3, 0} })

entityOps.spawn({}, { name = "group" })   -- bare entity, no components

Components is a { [ComponentName] = { field = value } } map. Each is an authored component (leaf name like "Model", a full identity, or a VFS path) added with its data so it serializes — native ECS components are intentionally not exposed here, so you never author something that renders at runtime but vanishes on save.

Transform: position {x,y,z}; rotation a {x,y,z,w} quaternion or {pitch,yaw,roll} euler degrees; scale a number or {x,y,z}. Also temporary, internal, synced (replicate gameplay edits to peers), and attributes.

Returns { id, name, components } with the component leaf names added.

To instantiate an existing asset (a bundle / avatar / mesh / material / texture) rather than hand-name components, use entityOps.fromAsset.