Workable
Overview
The Workable component enables entities (e.g., trees, rocks, crops) to be acted upon by players through manual labor or tools. It manages work progress (workleft), defines what action is required (e.g., chopping, mining), and handles progress decrement, completion callbacks, and state persistence. It also manages entity tags (e.g., "chop_workable") for UI and logic integration, and integrates with recoil and work multiplier systems.
Dependencies & Tags
- Component Dependencies: None directly required (but typically used alongside
repairable,inventory,tool,diseaseable, orplantcomponents in practice). - Tags Added/Removed:
"workrepairable"(added/removed byonworkableorOnRemoveFromEntitywhenrepairableexists and conditions are met).<action.id>_workable(e.g.,"chop_workable") — added/removed based onworkleft > 0andworkablestate; managed dynamically whenworkleft,maxwork,workable, oractionchanges.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
workleft | number | 10 | Remaining work units needed to finish; reduced on each successful work action. |
maxwork | number | -1 | Maximum work units for the entity. If <= 0, workleft is not clamped to a cap. When > 0, workleft is clamped between 1 and maxwork. |
action | Action | ACTIONS.CHOP | The action type (e.g., chop, mine, dig) the entity responds to. |
workable | boolean | true | Whether the entity can currently be worked on. |
savestate | boolean | false | Whether the component’s state (workleft, maxwork) should be saved to disk. |
tough | boolean | nil | Optional flag indicating if tough worker capability is required (via SetRequiresToughWork). Not set by default. |
Note: Other fields like onwork, onfinish, workmultiplierfn, shouldrecoilfn, and onloadfn are internal callbacks or functions, not serialized state properties.
Main Functions
Workable:Destroy(destroyer)
- Description: Forces completion of remaining work, triggering
WorkedByand potentiallyonfinishifCanBeWorked()returns true. Used when an entity is destroyed (e.g., by an action that bypasses full work cycles). - Parameters:
destroyer: TheGameObjectperforming the destruction.
Workable:WorkedBy(worker, numworks)
- Description: Handles a work attempt by a worker (typically a player). Applies tool/recoil logic and reduces
workleft. Calls internal logic (WorkedBy_Internal), triggers events ("worked","working"), and fires callbacks. - Parameters:
worker: TheGameObjectperforming the work.numworks: Number of work units to apply (may be adjusted by multiplier/recoil logic). Default:1.
Workable:WorkedBy_Internal(worker, numworks)
- Description: Core logic for decrementing work progress (after recoil checks). Handles floating-point precision, updates
lastworktimeandlastworker, fires"worked"/"working"events, and firesonworkcallback. Ifworkleft <= 0afterward, triggers"workfinished"andonfinishcallback. - Parameters:
worker: TheGameObjectperforming work.numworks: Work units to deduct (may be fractional).
Workable:SetWorkLeft(work)
- Description: Sets
workleftto a clamped value (ifmaxwork > 0) or at least1. Also forcesworkable = true. - Parameters:
work: Desired work value. Defaults to10ifnil.
Workable:CanBeWorked()
- Description: Returns
trueifworkableandworkleft > 0. - Returns:
boolean.
Workable:GetWorkLeft()
- Description: Returns current
workleftifworkable; otherwise returns0. - Returns:
number.
Workable:SetWorkable(able)
- Description: Sets the
workablestate flag. Automatically updates"workrepairable"and"<action>_workable"tags. - Parameters:
able:trueto make the entity workable;falseto disable.
Workable:SetMaxWork(work)
- Description: Sets the maximum work value (minimum
1). ResetsworkleftviaSetWorkLeftif updated (implicitly clamped). - Parameters:
work: Desired max work value.
Workable:SetWorkAction(act)
- Description: Updates the
actionfield (e.g.,ACTIONS.DIG) and refreshes associated tags ("<action.id>_workable"). - Parameters:
act: The newActionobject.
Workable:GetWorkAction()
- Description: Returns the current
action. - Returns:
Action.
Workable:SetOnWorkCallback(fn)
- Description: Registers a function to be called every time work is performed (i.e., inside
WorkedBy_Internalbefore completion). - Parameters:
fn: Function(inst, worker, workleft, numworks).
Workable:SetOnFinishCallback(fn)
- Description: Registers a function to be called when
workleftreaches0. - Parameters:
fn: Function(inst, worker).
Workable:SetRequiresToughWork(tough)
- Description: Enables/disables the "tough work" requirement. If
true, only entities with"toughworker"tag or tools with"tough"capability can work without recoil (zero work applied). - Parameters:
tough:trueto enforce tough work requirement.
Workable:SetWorkMultiplierFn(fn)
- Description: Sets a custom multiplier function for work input (e.g., tool efficiency). Takes
(inst, worker, numworks)and returns a multiplier. - Parameters:
fn: Function returning a numeric multiplier.
Workable:SetShouldRecoilFn(fn)
- Description: Registers a custom recoil function that may reduce or cancel work input.
- Parameters:
fn: Function(inst, worker, tool, numworks)returning(recoil: boolean, remainingworks: number).
Workable:OnSave()
- Description: Returns a table with
workleftandmaxworkifsavestateistrue; otherwise{}. Used for serialization. - Returns:
table.
Workable:OnLoad(data)
- Description: Restores
workleftandmaxworkfromdata, then callsonloadfnif set. - Parameters:
data: Table with optionalworkleftandmaxworkkeys.
Workable:GetDebugString()
- Description: Returns a debug-friendly string summary (e.g.,
"workleft: 5 maxwork: -1 workable: true"). - Returns:
string.
Workable:OnRemoveFromEntity()
- Description: Cleans up entity tags (
"workrepairable"and"<action.id>_workable") on component removal.
Workable:ShouldRecoil(worker, tool, numworks)
- Description: Checks if recoil should occur (e.g., non-tough worker on tough work, or custom logic). Returns
(recoil, remainingworks). - Parameters:
worker: TheGameObjectattempting work.tool: Tool in hands (nilif none).numworks: Raw work input.
- Returns:
boolean, number—recoilflag and adjustedremainingworks.
Events & Listeners
- Events Listen For / Trigger:
- Listens for property changes on
workleft,maxwork,action, andworkable(viaClassfield listeners:onworkable,onaction), updating tags automatically. - Emits events on work activity:
"worked"(onself.inst):{ worker = worker, workleft = self.workleft }"working"(onworker):{ target = self.inst }"workfinished"(onself.inst):{ worker = worker }"finishedwork"(onworker):{ target = self.inst, action = self.action }"plantkilled"(onTheWorld):{ doer = worker, pos = pos, workaction = self.action }— only for valid plants.
- Listens for property changes on