Objectspawner
Based on game build 714014 | Last updated: 2026-03-03
Overview
ObjectSpawner is a lightweight component that maintains a list of prefabs it has spawned or been explicitly given ownership of. It is designed for entities (often world or room-related) that need to track child objects—for example, to preserve references across saves or trigger actions when new objects are added. It does not manage object lifecycle beyond ownership tracking; object spawning is delegated to the global SpawnPrefab function.
This component integrates with the game’s save/load system through OnSave() and LoadPostPass() methods, storing only GUIDs to re-establish references after loading.
Usage example
local inst = CreateEntity()
inst:AddComponent("objectspawner")
-- Spawn a new object and take ownership
inst.components.objectspawner:SpawnObject("fireflies")
-- Or manually take ownership of an existing object
local new_obj = SpawnPrefab("torch")
inst.components.objectspawner:TakeOwnership(new_obj)
Dependencies & tags
Components used: None identified
Tags: None identified
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | (assigned automatically) | The entity instance that owns this component. |
objects | table | {} | List of owned object entities. |
onnewobjectfn | function? | nil | Optional callback invoked when a new object is taken ownership of. |
Main functions
OnSave()
- Description: Prepares the component’s state for persistence. Collects GUIDs of all owned objects and returns them as data.
- Parameters: None.
- Returns:
- If
objectsis non-empty:{ objects = table_of_GUIDs }, references— wherereferencesis identical toobjects. - If
objectsis empty:nil.
- If
LoadPostPass(newents, data)
- Description: Restores references to spawned objects after loading is complete. Uses
newents(a map of GUID → entity) to lookup and take ownership of each stored object. - Parameters:
newents(table) — Map of GUID strings to entity instances.data(table) — Saved data containingdata.objects(list of GUIDs).
- Returns: Nothing.
- Error states: Silently skips entries where
newents[v]isnil(e.g., if a referenced object failed to load).
TakeOwnership(obj)
- Description: Registers an existing object as owned by this spawner, invokes the optional
onnewobjectfncallback, and adds the object to theobjectslist. - Parameters:
obj(Entity) — The object to take ownership of.
- Returns: Nothing.
- Error states: Does nothing if
objisnil. Does not prevent duplicate entries.
SpawnObject(obj, linked_skinname, skin_id)
- Description: Spawns a new prefab using
SpawnPrefab()and automatically takes ownership of the result. - Parameters:
obj(string) — Prefab name to spawn.linked_skinname(string?, optional) — Skin name to apply (e.g.,"linked").skin_id(number?, optional) — Numeric skin ID.
- Returns:
Entity— The newly spawned object (same as returned bySpawnPrefab).
Events & listeners
- Listens to: None identified
- Pushes: None identified