Selfstacker
Overview
The SelfStacker component implements autonomous stacking behavior for items: it periodically checks for nearby compatible items (same prefab, skin, and stackability), establishes a mutual pairing, and merges stacks when conditions permit. It is designed for items that should self-organize in the world—such as food or tools—without player intervention.
Dependencies & Tags
- Adds Tag:
"selfstacker"on instantiation and removes it inOnRemoveEntity. - Requires Components:
stackable,inventoryitem,Physics. - Conditionally Depends On:
burnable: Used inCanSelfStack()to prevent stacking while burning.bait: Used inCanSelfStack()to ensure not attached (i.e.,IsFree()must be true).
- No direct
AddComponent()calls—assumes callers have already added required components.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | Reference to the entity this component is attached to. |
searchradius | number | 20 | Radius (in world units) within which to search for stack partners. |
stackpartner | Entity? | nil | Currently paired entity for stacking; nil if none or pairing broken. |
ignoremovingfast | boolean? | nil | If true, disables velocity check (allows stacking while moving). |
isvalidpartnerfn | function | — | Predicate function to validate potential stack partners (same prefab, skin, and valid state). |
stacktask | Task? | nil | Pending delayed task that executes DoStack(); cancels on entity wake/destroy. |
Main Functions
SetIgnoreMovingFast(ignorespeedcheck)
- Description: Enables or disables the velocity check during
CanSelfStack(). - Parameters:
ignorespeedcheck(boolean): Iftrue, allows stacking regardless of entity movement speed.
CanSelfStack()
- Description: Determines whether the entity currently meets all conditions to participate in stacking.
- Returns:
boolean—trueif the entity is stable and eligible to initiate stacking. - Conditions Checked:
- Not attached to bait (
bait == nilorIsFree()is true). - Not burning (
burnable == nilorIsBurning()is false). - Has a non-full
stackablecomponent. - Not held in inventory (
inventoryitem:IsHeld()is false). - Stationary or
ignoremovingfastis true (Physics:GetVelocity():LengthSq() < 1). - No existing
stackpartner.
- Not attached to bait (
OnRemoveEntity()
- Description: Cleanup method called when the entity is removed from the world. Cancels pending stacking tasks and removes the
"selfstacker"tag.
FindItemToStackWith()
- Description: Searches for a valid partner within
searchradiusthat satisfiesisvalidpartnerfn. If found, sets mutualstackpartnerreferences. - Returns:
Entity?— The found stack partner, ornil. - Search Logic: Uses
FindEntity()with:must_tags = {"selfstacker"}cant_tags = {"outofreach"}.
DoStack()
- Description: Merges the current entity’s stack with its
stackpartner. Attempts to transfer as many items as the current entity’s stack can accept (RoomLeft()). - Behavior:
- Cancels any pending
stacktask. - Calls
FindItemToStackWith()to refresh the partner. - If a partner exists:
- Requests
stackpartner.components.stackable:Get(num)items. - Puts them into
self.inst.components.stackable.
- Requests
- Cancels any pending
OnEntityWake()
- Description: Triggered when the entity becomes active (e.g., due to world re-entry or physics update). Resets
stackpartner, and if stackable, schedules a delayedDoStack()call (0–0.1 seconds) with jitter. - Behavior:
- Clears
stackpartner. - Evaluates
CanSelfStack(). - If true, cancels any existing
stacktaskand schedules a new one.
- Clears
Events & Listeners
- Listens for:
"wake"→ callsOnEntityWake()"onremove"→ callsOnRemoveEntity()
- Does not push any custom events.