Selfstacker
Based on game build 722832 | Last updated: 2026-04-26
Overview
SelfStacker allows items to autonomously search for and combine with compatible stack partners within a defined radius. This component is typically used by item-stacking gnomes or automated stacking systems. It works alongside the stackable component to merge item stacks and coordinates bidirectionally with partner entities to prevent duplicate stacking attempts.
Usage example
local inst = CreateEntity()
inst:AddComponent("selfstacker")
inst:AddComponent("stackable")
inst.components.selfstacker:SetIgnoreMovingFast(true)
inst.components.selfstacker:FindItemToStackWith()
Dependencies & tags
Components used:
bait-- checksIsFree()to ensure item is not trappedburnable-- checksIsBurning()to prevent stacking while on fireinventoryitem-- checksIsHeld()to ensure item is not in player inventorystackable-- usesCanStackWith,Get,IsFull,Put,RoomLeftfor stack operationsPhysics-- simulation object, accessesGetVelocity()for movement speed checks
Tags:
selfstacker-- added on component initialization, removed on entity removaloutofreach-- excluded from stack partner search
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | entity | --- | The owning entity instance. |
searchradius | number | 20 | Maximum distance to search for stack partners. |
stackpartner | entity | nil | Reference to the entity this item will stack with. |
ignoremovingfast | boolean/nil | nil | When true, skips velocity check in CanSelfStack(). |
stacktask | task | nil | Scheduled task reference for delayed stacking operation. |
SELFSTACKER_MUST_TAGS | constant (local) | { "selfstacker" } | Required tags for valid stack partners in FindItemToStackWith(). |
SELFSTACKER_CANT_TAGS | constant (local) | { "outofreach" } | Excluded tags for stack partner search in FindItemToStackWith(). |
Main functions
SetIgnoreMovingFast(ignorespeedcheck)
- Description: Configures whether the component should skip the movement speed check when determining if stacking is allowed.
- Parameters:
ignorespeedcheck-- boolean; iftrue, setsignoremovingfasttotrue, otherwise sets tonil. - Returns: nil
- Error states: None
CanSelfStack()
- Description: Determines if the entity is eligible for automatic stacking. Checks multiple conditions: not in a trap, not burning, has room in stack, not held in inventory, not moving fast (unless ignored), and has no existing stack partner.
- Parameters: None
- Returns:
trueif all conditions pass,falseotherwise. - Error states: Errors if
inst.Physicsis nil (no nil guard beforeGetVelocity()call).
OnRemoveEntity()
- Description: Cleanup handler called when the entity is removed. Cancels any pending stack task and removes the
selfstackertag. - Parameters: None
- Returns: nil
- Error states: None --
stacktaskis nil-checked before callingCancel().
FindItemToStackWith()
- Description: Searches within
searchradiusfor a valid stack partner usingFindEntity. When a partner is found, establishes a bidirectional link by setting the partner'sstackpartnerto this entity. - Parameters: None
- Returns: The partner entity instance, or
nilif no valid partner found. - Error states: Errors if the found
stackpartnerdoes not have aselfstackercomponent (accessesself.stackpartner.components.selfstacker.stackpartnerwithout nil guard).
DoStack()
- Description: Executes the stacking operation. Cancels any existing stack task, finds a stack partner, calculates available room in this stack, retrieves items from the partner, and puts them into this entity's stack.
- Parameters: None
- Returns: nil
- Error states: Errors if
self.instlacksstackablecomponent (no nil guard onself.inst.components.stackable). Errors ifstackpartnerlacksstackablecomponent (no nil guard onself.stackpartner.components.stackable).
OnEntityWake()
- Description: Called when the entity becomes active (player nearby). Resets
stackpartnertonil, checks if self-stacking is allowed, and schedules aDoStack()call with a random delay between 0-0.1 seconds. - Parameters: None
- Returns: nil
- Error states: Errors if
inst.Physicsis nil (inherited fromCanSelfStack()call).
Events & listeners
- Listens to: None identified
- Pushes: None identified