Occupiable
Overview
This component manages the concept of "occuppation" for an entity, allowing another entity (the occupant) to occupy it—such as a character sitting on a stool or a creature inside a cage. It maintains state via a reference to the occupant, dynamically adjusts entity tags (occupied, <type>_occupiable), and handles lifecycle events like saving, loading, harvesting, and cleanup on removal.
Dependencies & Tags
- Component Dependencies: Relies on the
occupiercomponent being present on the entity attempting to occupy (viaoccupier:SetOwner()andHasTag()checks). - Tags Added/Removed:
- Adds/Removes
"occupied"tag based on whether an occupant is present. - Adds/Removes
<occupanttype>_occupiabletag whenoccupanttypechanges or is cleared, provided no occupant is currently present.
- Adds/Removes
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | Reference to the host entity (set in constructor). |
occupant | Entity? | nil | Reference to the currently occupying entity. |
occupanttype | string? | nil | Tag name (e.g., "pig", "player") indicating what kind of entity can occupy this. |
Main Functions
IsOccupied()
- Description: Returns whether an occupant is currently occupying this entity.
- Parameters: None.
GetOccupant()
- Description: Returns the current occupant entity, or
nilif unoccupied. - Parameters: None.
CanOccupy(occupier)
- Description: Determines whether the given
occupierentity is eligible to occupy this entity. Checks include:- This entity is not already occupied.
occupanttypeis set on this component.occupierhas a matching tag (e.g.,"pig"matchesoccupanttype = "pig").occupierhas anoccupiercomponent.
- Parameters:
occupier: The potential occupier entity.
Occupy(occupier)
- Description: Makes
occupieroccupy this entity. Internally:- Registers cleanup callbacks for
"perished"and"onremove"events on the occupant. - Links the occupant’s
occupiercomponent to this entity viaSetOwner(). - Adds the occupant as a child and removes it from the scene.
- Invokes optional
onoccupiedcallbacks (on both this component and the occupier).
- Registers cleanup callbacks for
- Parameters:
occupier: The entity to become the occupant. Must be valid and have anoccupiercomponent.
- Returns:
trueif occupation succeeded,falseotherwise.
Harvest()
- Description: Removes the occupant without destroying it, effectively "unloading" it from this entity (e.g., for picking up an item placed in a container). Returns the occupant entity to the scene and clears internal references.
- Parameters: None.
- Returns: The previously occupying entity (now restored to world state), or
nilif no occupant or it lackedinventoryitem.
OnSave()
- Description: Prepares serializable data for the current state—specifically, the save record of the occupant if valid.
- Parameters: None.
- Returns: Table with optional
occupantfield containing the occupant’s save record ornil.
OnLoad(data, newents)
- Description: Restores occupation state on deserialization by spawning and re-occupying the saved occupant entity (if present).
- Parameters:
data: Table fromOnSave()(or saved game data).newents: Table of newly spawned entities used during save load.
OnRemoveFromEntity()
- Description: Cleans up tags and references when component is removed from the entity. Explicitly removes
"occupied"and<type>_occupiabletags. - Parameters: None.
Events & Listeners
- Listens For:
"perished"on occupant → triggersoccupier.occupiableonperish"onremove"on occupant → triggersoccupier.occupiableonremove
- Triggers (via
inst:PushEventor callback hooks):onoccupied(self.inst, occupier)— when an occupant successfully occupies.onemptied(self.inst)— when occupancy ends (viaHarvest,onremove, orperished).onperishfn(self.inst, self.occupant)— if set, called when occupant perishes.