diff --git a/README.md b/README.md index 87630e1..d77e051 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Template-Only-Subset + set_tag + container declaration. - `list_by_template(id)`, `list_by_tag(tag)` - `destroy(entity)` - `set_tag(entity, tag, present)` — v0.2: runtime tag add/remove +- `get_container(entity)` — v0.2: read back the container block from entity's template **Deferred (Loud-Error if attempted):** - `slots` block → Phase D trigger (Composite Items with Sub-Items) @@ -180,6 +181,15 @@ when items move between world and inventory. destroys via `engine.entity.destroy`. Idempotent. If entity wasn't composition-created, falls through to `entity.destroy`. +### `composition.get_container(entity)` + +**Syntax:** `composition.get_container(entity) -> table or nil` + +**Description:** Returns the `container` block from the entity's template +(e.g. `{kind="list"}`), or `nil` if the template declared no `container` +block. Loud-Error if `entity` was not created by composition. Used by +`lib-core.inventory-list` to validate container entities at runtime. + ### `composition.list_templates()` **Syntax:** `composition.list_templates() -> {id, ...}` diff --git a/init.lua b/init.lua index 2a5b4a5..027373b 100644 --- a/init.lua +++ b/init.lua @@ -427,4 +427,21 @@ function M.list_templates() return out end +-- Returns the container block from the entity's template, or nil if the +-- template declared no container block. Loud-Error if entity was not +-- created by composition. +function M.get_container(target_entity) + local reg_id = target_entity and target_entity:get_property("composition.reg_id") + if not reg_id or reg_id == 0 then + error("composition.get_container: entity was not created by composition") + end + local meta = entity_meta[reg_id] + if not meta then + error("composition.get_container: entity was not created by composition") + end + local tpl = templates[meta.template_id] + if not tpl then return nil end + return tpl.container -- nil when not declared +end + return M