ItemUsable is the base class for items that do something when they are used. A derived item overrides use(actor) with its effect and, to forbid the use in some cases, can_use(actor). The max_uses field sets how many times the item can be used before it is removed from the inventory, 1 by default. The uses that are left are kept in the uses custom data of the instance; when max_uses is above 1 they are shown in the name of the item and on its inventory slot, and the weight of the item shrinks with them.

Summary

Methods

Called by ItemUsable:on_use before the item is used.
Returns the translated name of the item, followed by the amount of uses left if the item can be used more than once.
Returns the amount of uses the item has left.
Returns the weight of the item, scaled by the share of uses it has left.
Called on the server by the 'PlayerUseItem' hook when a player uses the item.
Called when the inventory slot of the item is painted over.
Called by ItemUsable:on_use when a player uses the item.

Methods

ItemUsable:can_use(_actor)

shared plugins/items/plugin/items/bases/sh_item_usable.lua:84

Called by ItemUsable:on_use before the item is used. Override it and return false to prevent the item from being used.

Parameters

  • _actor Player

Returns

  • Boolean false to prevent the use, nil otherwise

Returns the translated name of the item, followed by the amount of uses left if the item can be used more than once.

Returns

  • String

Returns the amount of uses the item has left.

Returns

  • Number

Returns the weight of the item, scaled by the share of uses it has left.

Returns

  • Number

Called on the server by the 'PlayerUseItem' hook when a player uses the item. Calls ItemUsable:use unless ItemUsable:can_use returns false, and spends one use. Returning nothing/nil removes the item from the inventory as soon as it's used, false prevents the item from being used at all, true prevents the item from being removed upon use.

Parameters

  • actor Player

Returns

  • Boolean true if there are uses left, false if it cannot be used, nil if used up

ItemUsable:paint_over_slot(w, _h)

client plugins/items/plugin/items/bases/sh_item_usable.lua:22

Called when the inventory slot of the item is painted over. Draws the amount of uses left if the item can be used more than once.

Parameters

  • w Number width of the slot panel
  • _h Number height of the slot panel

Called by ItemUsable:on_use when a player uses the item. Override it to make the item do something.

Parameters

  • _actor Player

Defined in