Chestfunctions
Based on game build 714014 | Last updated: 2026-03-08
Overview
chestfunctions is a scenario-scoped utility module that offers helpers for initializing container loot and handling chest trap logic. It manages random loot selection, chance-based item spawning, and trap activation (e.g., Statue Traps), integrating with the container, talker, and scenario runner systems. Functions are exported for use in scenario worldgen and entity initialization logic.
Usage example
-- Initialize a chest with loot and set up trap behavior
local chest = SpawnPrefab("chest")
chest:AddComponent("container")
chest:AddComponent("talker")
chestfunctions.AddChestItems(chest, {
{ item = "log", count = 3 },
{ item = "twigs", count = 5, chance = 0.8 },
{ item = { "seeds", "berries" }, count = 1 },
})
chestfunctions.InitializeChestTrap(chest, scenariorunner, trapfn, 0.75)
Dependencies & tags
Components used: container, talker
Tags: None identified.
Properties
No public properties.
Main functions
PickLootItems(number, loot)
- Description: Randomly selects up to
numberitems from theloottable without replacement and returns a new table with the selected items. - Parameters:
number(number) - Maximum number of items to pick.
loot(table) - List of item definitions or prefabs to select from. - Returns: (table) A new table containing up to
numberrandomly selected items. - Error states: If
numberexceeds#loot, it selects all available items.
AddChestItems(chest, loot, num)
- Description: Spawns loot items into a chest's container based on chance and count rules. It ensures the chest is non-empty by recursively retrying if initially empty.
- Parameters:
chest(Entity) - The entity with acontainercomponent.
loot(table) - Array of loot definitions; each entry may be a string (prefab name) or a table with keysitem,count,chance, and/orinitfn.
num(number?, optional) - Maximum slots to fill; defaults tochest.components.container.numslots. - Returns: Nothing.
- Error states: If
SpawnPrefabfails for an item, prints a"Cant spawn"message to console and skips that item. Recalls itself if the chest ends up empty after processing.
OnOpenChestTrap(inst, openfn, scenariorunner, data, chance)
- Description: Evaluates whether a chest trap triggers on open/activation using luck-based rolling. If triggered, spawns FX, plays sound, calls
openfn, and prompts the player to announce the trap event. - Parameters:
inst(Entity) - The chest entity, may or may not have atalkercomponent.
openfn(function) - Callback function invoked when trap triggers. Signature:openfn(inst, scenariorunner, data).
scenariorunner(Entity/ScenarioRunner) - Scenario runner instance passed toopenfn.
data(table) - Event data containing at leastplayer,doer, orworker.
chance(number?, optional) - Base trap activation chance; defaults to0.66. - Returns: Nothing.
- Error states: Early exit if
TryLuckRollreturnsfalse. Skips talker calls ifinst.components.talkeris missing (e.g., Deerclops). FX and sound may be skipped ifSpawnPrefabreturnsnil.
InitializeChestTrap(inst, scenariorunner, openfn, chance)
- Description: Attaches event listeners to a chest entity to trigger trap logic on
onopenorworkedevents. - Parameters:
inst(Entity) - The chest entity.
scenariorunner(Entity/ScenarioRunner) - Scenario runner reference.
openfn(function) - Trap response function (passed toOnOpenChestTrap).
chance(number?, optional) - Base trap chance. - Returns: Nothing.
- Error states: Creates a closure
inst.scene_triggerfn; this is cleaned up inOnDestroy.
OnDestroy(inst)
- Description: Cleans up event listeners registered by
InitializeChestTrapto prevent memory leaks. - Parameters:
inst(Entity) - The chest entity being destroyed. - Returns: Nothing.
Events & listeners
- Listens to:
onopen,worked— handled viaInitializeChestTrap; later removed byOnDestroy. - Pushes: No events directly; relies on scenario infrastructure and external event systems.