Builtin Library
README.md
entity_reflect
High-level entity introspection and manipulation utilities. Wraps the
__reflect FFI global with a cleaner API — no per-script component
declaration needed; every method goes through the reflection layer so
it works on any entity that carries the relevant component.
Exposed as the Entity global via the --!global Entity directive.
Exports
Entity.getField(entityId, component, field) -> any/Entity.setField(entityId, component, field, value) -> boolean— generic reflected read/write.Entity.getPosition(id) -> Vec3?/Entity.setPosition(id, pos)—Transform.positionshortcuts.Entity.getRotation(id) -> Quat?—Transform.rotationshortcut.Entity.getScale(id) -> Vec3?/Entity.setScale(id, scale)—Transform.scaleshortcuts.Entity.getName(id) -> string?—Namecomponent read.Entity.isVisible(id) -> boolean—Visiblecomponent read; missing component is treated as visible.Entity.getComponents(id) -> { string }?— list reflected components on this entity.Entity.getSchema(componentName) -> { ComponentFieldSchema }?— list field names + types for a component type.Entity.snapshot(id) -> any— full{ ComponentName = { field = value, ... }, ... }dump.Entity.patch(id, component, fields) -> boolean— multi-field update.Entity.distance(idA, idB) -> number?— straight-line distance between two entities'Transform.position.Entity.allTypes() -> { string }— list every registered reflectable component type.
Types:
Vec3 = { x: number, y: number, z: number }Quat = { x: number, y: number, z: number, w: number }ComponentFieldSchema = { name: string, type: string }
Usage
-- `Entity` is a global (--!global Entity); no require needed.
local pos = Entity.getPosition("player")
Entity.setPosition("player", { x = 0, y = 1, z = 0 })
local snap = Entity.snapshot("player")
Entity.patch("player", "Transform", {
position = { x = 0, y = 1, z = 0 },
scale = { x = 1, y = 1, z = 1 },
})
Notes
- Every method goes through the
__reflectFFI —nilreturns mean the component or field is missing, not that the entity is gone. UseEntity.getComponents(id)to enumerate what's there. isVisibletreats a missingVisiblecomponent as visible (v == nilreturnstrue).Entity.distancereturnsnilwhen either entity lacks a position; callers should branch onnilbefore using the result.- The
--!global Entitydirective is what makesEntitythe global name — the module's localEvariable is just an implementation detail.