feat(composition): expose get_container for inventory readback
Add composition.get_container(entity) -> table or nil. 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 the
entity was not created by composition.
The internal storage (tpl.container) was already present since v0.2.0
(stashed on the template record with a comment "B.2 reads this") but
had no public getter. This fills the oversight without any semantic
change to v0.2.0.
This commit is contained in:
10
README.md
10
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, ...}`
|
||||
|
||||
17
init.lua
17
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
|
||||
|
||||
Reference in New Issue
Block a user