Tool
Overview
The Tool component provides core tool functionality for entities in the Entity Component System. It enables an entity to declare which actions it can perform and with what effectiveness, manages a "tough work" capability, and automatically triggers a "toolbroke" event on its owner when its usage percentage reaches zero—unless the entity is rechargeable.
Dependencies & Tags
- Component Dependencies: Relies on
inventoryitemand optionallyrechargeablebeing present on the same entity. - Tags Added:
"tool": Added unconditionally on construction."ACTIONID_tool": Added per action type whenSetActionis called (e.g.,"chop_tool").
- Tags Removed: All
"tool"and"ACTIONID_tool"tags are removed when the component is removed from the entity.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
actions | table | {} | Maps action objects to their numeric effectiveness values (default: 1). |
tough | boolean? | false | Indicates whether the tool can perform tough work; controlled via EnableToughWork and queried via CanDoToughWork. |
Note: No explicit _ctor is defined beyond the Class wrapper. All properties are initialized directly in the constructor function.
Main Functions
EnableToughWork(tough)
- Description: Enables or disables the tool’s ability to perform tough work.
- Parameters:
tough(boolean): Iftrue, the tool is marked as capable of tough work. Iffalseor omitted, tough work is disabled.
CanDoToughWork()
- Description: Returns whether the tool is currently enabled for tough work.
- Returns:
boolean—trueiftough == true, otherwisefalse.
GetEffectiveness(action)
- Description: Retrieves the effectiveness value for a given action.
- Parameters:
action(stringoraction object): The action to query.
- Returns:
number— Effectiveness value if the action is registered, otherwise0.
SetAction(action, effectiveness)
- Description: Registers an action and its effectiveness with this tool, and adds the corresponding
_tooltag. - Parameters:
action(stringoraction object): The action to register; must be a valid entry inTOOLACTIONS.effectiveness(number?): Optional effectiveness value (default:1).
- Throws:
asserterror ifaction.idis not a validTOOLACTIONSkey.
CanDoAction(action)
- Description: Checks whether the tool supports a given action.
- Parameters:
action(stringoraction object): The action to check.
- Returns:
boolean—trueif the action is registered inself.actions, otherwisefalse.
OnRemoveFromEntity()
- Description: Cleanup handler invoked when the component is removed from the entity. Removes all event listeners and tags associated with the tool.
Events & Listeners
- Listens to:
"percentusedchange": TriggersPercentChangedwhen the entity’s usage percentage changes.
- Triggers:
"toolbroke": Pushed on the tool’s owner when usage reaches0%, provided the tool is not rechargeable and has an owner with aninventoryitemcomponent.
Note: The PercentChanged function is a local event handler, not a method of the Tool class.