Useabletargeteditem
Based on game build 714014 | Last updated: 2026-03-03
Overview
Useabletargeteditem is a component that tracks and manages the state of items which are used in a targeted manner — i.e., when an item is actively being used on a specific target entity. It maintains internal state flags (inuse_targeted, inventory_disableable, useablemounted) and automatically synchronizes them with entity tags for use by other systems (e.g., UI, AI, or gameplay logic). The component supports optional custom callback functions for the start and stop of item usage.
This component does not directly handle the item's effect logic, but provides hooks (onusefn, onstopusefn) and state management to enable modular, reusable targeted item behaviors.
Usage example
local inst = CreateEntity()
inst:AddComponent("useabletargeteditem")
-- Configure target prefab (e.g., "tree") to attach a "tree_targeter" tag
inst.components.useabletargeteditem:SetTargetPrefab("tree")
-- Set custom usage logic: succeeds always for this example
inst.components.useabletargeteditem:SetOnUseFn(function(item, target, doer)
-- ... perform effect ...
return true
end)
-- Set stop-usage logic (e.g., cleanup)
inst.components.useabletargeteditem:SetOnStopUseFn(function(item)
print("Item usage stopped")
end)
-- Start using the item on a target
local success, reason = inst.components.useabletargeteditem:StartUsingItem(target_entity, doer_entity)
Dependencies & tags
Components used: None identified
Tags:
inuse_targeted— added/removed based oninuse_targetedstate.useabletargeteditem_inventorydisable— added/removed based oninventory_disableablestate.<prefab_name>_targeter— added/removed dynamically based onuseabletargetprefab.useabletargeteditem_mounted— added/removed based onuseablemountedstate.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inuse_targeted | boolean | false | Whether the item is currently in use on a target. Controlled via the inuse_targeted tag. |
inventory_disableable | boolean | false | Whether the item should be disabled in inventory while in use. Controls the useabletargeteditem_inventorydisable tag. |
useabletargetprefab | string or nil | nil | Prefab name of the valid target, used to attach a dynamic _targeter tag. |
useablemounted | boolean or nil | nil | Whether the item is mounted (e.g., turret-like). Controls the useabletargeteditem_mounted tag. |
onusefn | function or nil | nil | Optional callback: fn(item, target, doer) → success: boolean, failReason: string?. |
onstopusefn | function or nil | nil | Optional callback: fn(item). |
Main functions
SetTargetPrefab(prefab_name)
- Description: Sets the expected target prefab name and updates the corresponding
_targetertag. Removes any old_targetertag if a previous prefab was set. - Parameters:
prefab_name(string) — the name of the prefab this item can target. - Returns: Nothing.
SetUseableMounted(enable)
- Description: Enables or disables the "mounted" state for this item, affecting the
useabletargeteditem_mountedtag. - Parameters:
enable(boolean) — whether the item is mounted. - Returns: Nothing.
SetOnUseFn(fn)
- Description: Sets the callback function invoked when item usage begins (via
StartUsingItem). Used to define item-specific activation logic. - Parameters:
fn(function) — a function with signaturefn(item, target, doer)returningsuccess: boolean, failReason: string?. - Returns: Nothing.
SetOnStopUseFn(fn)
- Description: Sets the callback function invoked when item usage ends (via
StopUsingItem). Typically used for cleanup or state reset. - Parameters:
fn(function) — a function with signaturefn(item). - Returns: Nothing.
SetInventoryDisable(value)
- Description: Enables or disables inventory restrictions (e.g., prevents equipping or using elsewhere) while this item is in use.
- Parameters:
value(boolean) — whether inventory usage should be disabled while active. - Returns: Nothing.
CanInteract()
- Description: Checks whether the item can be interacted with (i.e., started being used again). Returns
falseif currently in use on a target. - Parameters: None.
- Returns:
trueif not currently in use;falseotherwise.
StartUsingItem(target, doer)
- Description: Attempts to start using the item on the given target. Invokes
onusefnif set; otherwise assumes success. If successful, setsinuse_targeted = trueand adds theinuse_targetedtag. - Parameters:
target(Entity ornil) — the target entity the item is being used on.doer(Entity ornil) — the entity performing the action (e.g., player or AI).
- Returns:
usesuccess(boolean) —trueif usage succeeded,falseotherwise.usefailreason(string ornil) — optional reason string if usage failed.
- Error states: Returns
falseandnilasfailreasonifonusefnexplicitly returnsfalse. Also returns early ifself.instbecomes invalid mid-execution.
StopUsingItem()
- Description: Ends the current use session. Resets
inuse_targeted = false, removes theinuse_targetedtag, and invokesonstopusefnif set. - Parameters: None.
- Returns: Nothing.
Events & listeners
- Listens to:
inuse_targeted— sets theinuse_targetedtag.inventory_disableable— sets theuseabletargeteditem_inventorydisabletag.useabletargetprefab— manages dynamic_targetertag.useablemounted— sets/removes theuseabletargeteditem_mountedtag.
- Pushes: None identified.