ItemBase is the class of every item: item templates and item instances are both ItemBase objects, and the item base classes extend it. The item pipeline creates one object per item file and exposes it as ITEM. The file sets its fields and defines its callbacks, optionally after taking over those of a base class with ItemBase:base_off.

Fields an item can set; Item.register fills in the defaults: name, print_name, description and category; model, skin, color and model_bodygroups (a table of bodygroup id or name to value, applied to the item entity); weight and cost; width and height (size in inventory slots), stackable and max_stack; pocket_size (true if the item fits into pockets); background_color and special_color (background and outline of its inventory slot); icon_data, icon_material and icon_model (how its inventory icon is rendered); use_text, take_text, drop_text, destroy_text, use_icon, take_icon, drop_icon and destroy_icon (options of its menu); destroyable (true or false to let or forbid players to destroy the item whatever the item_destroy config says); entity_health (how much damage the item takes while it lies in the world, instead of the item_entity_health config; 0 makes it indestructible); and data (default custom data, read and written with ItemBase:get_data and ItemBase:set_data).

Callbacks an item can define. Those of the first group are called on the server:

  • on_use(actor): a player uses the item. The use option is only shown when the item has this callback. Returning true keeps the item, returning false cancels the use, and returning nothing removes the item.
  • on_drop(actor): a player is about to drop the item; return false to prevent it.
  • on_destroy(actor): a player is about to destroy the item; return false, and optionally an error phrase, to prevent it.
  • on_created(): the instance has just been created.
  • on_loadout(owner) and on_save(owner): the player that has the item has spawned, or their character is about to be saved.
  • can_transfer(inventory, x, y) and can_move(inventory, x, y): the item is about to be moved to another inventory (or picked up into one) or inside of its inventory; return false, and optionally an error phrase, to prevent it.
  • on_transfer(new_inventory, old_inventory): the item is about to change its inventory.
  • on_entity_spawned(entity): the item has been put into the world as an fl_item entity, which includes the entities that are restored when the server starts.
  • on_entity_take_damage(entity, damage_info): the entity of the item is taking damage; return false to ignore the damage.
  • on_entity_destroyed(entity, damage_info): the entity of the item has lost all of its health and is about to be removed along with the item; return false to do without the default break effect.

On the client:

  • is_action_visible(action): return false to hide the 'use', 'take', 'drop' or 'destroy' option of the menu.
  • paint_slot(w, h) and paint_over_slot(w, h): draw on the inventory slot of the item.
  • on_entity_draw(entity): the entity of the item is about to be drawn; return false to keep its model from being drawn.

And on both:

  • on_entity_think(entity): called for the entity of the item once a second; return a number to be called again in that many seconds instead (0.1 at least).
  • on_entity_removed(entity): the entity of the item is being removed, be it because the item was picked up or destroyed or because the map is being cleaned up or shut down. A client also gets the call when it merely stops receiving the entity.

Custom menu options are added with ItemBase:add_button, and every menu action goes through ItemBase:do_menu_action.

Summary

Methods

Fancy output if you do print(item_obj).
Adds a custom button to the menu of the item.
Bases the item off an item base.
Performs a menu action on the item on behalf of the player.
Returns the title of the 'cancel' option in the item's menu.
Returns the color of the item's model.
Returns a custom data value of the item.
Returns the description of the item.
Returns the title of the 'destroy' option in the item's menu.
Returns the title of the 'drop' option in the item's menu.
Returns the health that the entity of the item gets when the item is put into the world: the entity_health of the item, or the 'item_entity_health' config if the item has none.
Returns the camera setup that is used to render the item's model in inventory slots.
Returns the material that is drawn in inventory slots instead of the item's model.
Returns the model that is shown in inventory slots instead of the item's own model.
Returns how many items of this kind can be stacked in a single inventory slot.
Returns the model of the item.
Returns the bodygroups of the item's model, which the entity of the item is given when the item lies in the world.
Returns the name of the item that is shown to players.
Finds the player that has the item in one of their inventories, also inside of a container item there.
Returns the internal name of the item, ignoring its print name.
Returns the skin of the item's model.
Returns the title of the 'take' option in the item's menu.
Returns the title of the 'use' option in the item's menu.
Returns the weight of the item.
Checks whether clients may request a menu action of the item: 'on_take' and 'on_drop' always, 'on_use' if the item has an on_use callback, 'on_destroy' if the item is destroyable, and the callbacks of the item's custom buttons.
init(id)shared
Initializes a new item table.
is(base)shared
Checks whether the item was based off the specified item base with ItemBase:base_off.
Checks whether players may destroy the item through its menu: the destroyable field of the item if it is set, and the 'item_destroy' config otherwise.
Called on the server when a player is about to destroy the item through its menu, after the 'PlayerCanDestroyItem' hook has allowed it.
Called on the server by the 'CanPlayerDropItem' hook when a player is about to drop the item.
Called on the server right after the player that has the item spawns with their character loaded.
Called on the server before the character of the player that has the item is saved.
Plays the sound of a menu action, if the item has one.
Registers the item as an item template under its id.
Sets the sound that is emitted when a menu action is performed on the item.
Sets a custom data value of the item.
Ties the item to the entity that represents it in the world.

Methods

Fancy output if you do print(item_obj).

Returns

  • String e.g. '#<Item:Test Item 12>'

ItemBase:add_button(name, data)

shared plugins/items/plugin/lib/classes/sh_item.lua:260

Adds a custom button to the menu of the item.

ITEM:add_button('item.option.open', {
  icon = 'icon16/briefcase.png',
  -- Calls ITEM:on_open(actor) on the server when the button is pressed.
  callback = 'on_open',
  -- Client-side. The button is hidden if this returns false.
  on_show = function(item_obj)
    return !IsValid(item_obj.entity)
  end
})

Parameters

  • name String id of the button; also its title, unless data has name or get_name
  • data Map button data: icon (String), callback (String name of the item's method to call on the server), and optionally name (String) or the client-side functions get_name, get_icon, on_show and on_click, each of which receives the item

ItemBase:base_off(what)

shared plugins/items/plugin/lib/classes/sh_item.lua:103

Bases the item off an item base. Copies every function of the base, and every other field that the item has not set yet. Also available as ItemBase:derive.

ITEM:base_off 'ItemContainer'
ITEM.name = 'Test Bag'

Parameters

  • what String/Map name of the base class ('Item' prefix may be omitted), or the class

ItemBase:do_menu_action(act, actor, ...)

shared plugins/items/plugin/lib/classes/sh_item.lua:406

On the server

Performs a menu action on the item on behalf of the player. Runs the 'PlayerCanUseItem' hook and then the hook of the action ('PlayerTakeItem', 'PlayerUseItem', 'PlayerDropItem' or 'PlayerDestroyItem'). Every action except 'on_use', 'on_take', 'on_drop' and 'on_destroy' also calls the item's method of the same name with the player and the extra arguments. Runs the 'PlayerUsedItem' hook when done.

-- Makes the player pick up the item into their hotbar.
item_obj:do_menu_action('on_take', actor, { inv_type = 'hotbar' })

Parameters

  • act String 'on_use', 'on_take', 'on_drop', 'on_destroy' or the callback of a custom button
  • actor Player the player performing the action
  • ... Vararg extra arguments that are passed to the hooks and to the item's method

On the client

Asks the server to perform a menu action on the item on behalf of the local player. The server ignores the actions that ItemBase:has_menu_action does not allow.

Parameters

  • act String 'on_use', 'on_take', 'on_drop', 'on_destroy' or the callback of a custom button
  • ... Vararg extra arguments to send to the server

Defined in

ItemBase:get_cancel_text()

client plugins/items/plugin/lib/classes/sh_item.lua:544

Returns the title of the 'cancel' option in the item's menu. Client-side only.

Returns

  • String language phrase

Returns the color of the item's model.

Returns

  • Color white if the item has no color set

ItemBase:get_data(id, default)

shared plugins/items/plugin/lib/classes/sh_item.lua:560

Returns a custom data value of the item.

Parameters

  • id String data key
  • default Any optional, defaults to nil what to return if the value is not set

Returns

  • Any the value, or the default if the value is not set

ItemBase:get_description()

shared plugins/items/plugin/lib/classes/sh_item.lua:157

Returns the description of the item.

Returns

  • String

ItemBase:get_destroy_text()

client plugins/items/plugin/lib/classes/sh_item.lua:538

Returns the title of the 'destroy' option in the item's menu. Client-side only.

Returns

  • String language phrase

ItemBase:get_drop_text()

client plugins/items/plugin/lib/classes/sh_item.lua:532

Returns the title of the 'drop' option in the item's menu. Client-side only.

Returns

  • String language phrase

ItemBase:get_entity_health()

shared plugins/items/plugin/lib/classes/sh_item.lua:210

Returns the health that the entity of the item gets when the item is put into the world: the entity_health of the item, or the 'item_entity_health' config if the item has none.

Returns

  • Number 0 if the item cannot be destroyed by damage

ItemBase:get_icon_data()

shared plugins/items/plugin/lib/classes/sh_item.lua:234

Returns the camera setup that is used to render the item's model in inventory slots.

Returns

  • Map table with origin (Vector), angles (Angle) and fov (Number) fields, or nil to position the camera automatically

ItemBase:get_icon_material()

shared plugins/items/plugin/lib/classes/sh_item.lua:240

Returns the material that is drawn in inventory slots instead of the item's model.

Returns

  • String path to the material, or nil if the model should be drawn

ItemBase:get_icon_model()

client plugins/items/plugin/lib/classes/sh_item.lua:551

Returns the model that is shown in inventory slots instead of the item's own model. Client-side only.

Returns

  • String path to the model, or nil if the item's own model should be shown

ItemBase:get_max_stack()

shared plugins/items/plugin/lib/classes/sh_item.lua:169

Returns how many items of this kind can be stacked in a single inventory slot.

Returns

  • Number

Returns the model of the item.

Returns

  • String path to the model

ItemBase:get_model_bodygroups()

shared plugins/items/plugin/lib/classes/sh_item.lua:202

Returns the bodygroups of the item's model, which the entity of the item is given when the item lies in the world.

ITEM.model_bodygroups = {
  [1] = 2,
  ['strap'] = 1
}

Returns

  • Map bodygroup id (Number) or bodygroup name (String) to its value, or nil if the item leaves the bodygroups of its model alone

See also

Returns the name of the item that is shown to players.

Returns

  • String print name of the item, or its name if there is none

Finds the player that has the item in one of their inventories, also inside of a container item there. Server-side only.

Returns

  • Player the player, or nil if no player has the item

ItemBase:get_real_name()

shared plugins/items/plugin/lib/classes/sh_item.lua:151

Returns the internal name of the item, ignoring its print name.

Returns

  • String

Returns the skin of the item's model.

Returns

  • Number

ItemBase:get_take_text()

client plugins/items/plugin/lib/classes/sh_item.lua:526

Returns the title of the 'take' option in the item's menu. Client-side only.

Returns

  • String language phrase

ItemBase:get_use_text()

client plugins/items/plugin/lib/classes/sh_item.lua:520

Returns the title of the 'use' option in the item's menu. Client-side only.

Returns

  • String language phrase

Returns the weight of the item.

Returns

  • Number

ItemBase:has_menu_action(act)

shared plugins/items/plugin/lib/classes/sh_item.lua:276

Checks whether clients may request a menu action of the item: 'on_take' and 'on_drop' always, 'on_use' if the item has an on_use callback, 'on_destroy' if the item is destroyable, and the callbacks of the item's custom buttons. The server ignores requests for anything else, so that a client cannot call arbitrary methods of the item.

Parameters

  • act String name of the action

Returns

  • Boolean

See also

Initializes a new item table. The 'item' pipeline creates one for every item file and exposes it as ITEM.

Parameters

  • id String item id; gets converted with string.to_id

Checks whether the item was based off the specified item base with ItemBase:base_off. Also available as ItemBase:based_off.

Parameters

  • base String/Map name of the base class ('Item' prefix may be omitted), or the class

Returns

  • Boolean

ItemBase:is_destroyable()

shared plugins/items/plugin/lib/classes/sh_item.lua:223

Checks whether players may destroy the item through its menu: the destroyable field of the item if it is set, and the 'item_destroy' config otherwise.

Returns

  • Boolean

ItemBase:on_destroy(_actor)

shared plugins/items/plugin/lib/classes/sh_item.lua:349

Called on the server when a player is about to destroy the item through its menu, after the 'PlayerCanDestroyItem' hook has allowed it. Returning nothing/nil lets the item be destroyed, returning false keeps it where it is.

Parameters

  • _actor Player

Returns

  • Boolean false to prevent the destruction
  • String error phrase to notify the player with; nothing otherwise

ItemBase:on_drop(_actor)

shared plugins/items/plugin/lib/classes/sh_item.lua:341

Called on the server by the 'CanPlayerDropItem' hook when a player is about to drop the item. Returning nothing/nil drops the item like normal, returning false prevents the item from appearing and doesn't remove it from the inventory.

Parameters

  • _actor Player

Returns

  • Boolean false to prevent the drop, nil otherwise

ItemBase:on_loadout(_owner)

shared plugins/items/plugin/lib/classes/sh_item.lua:354

Called on the server right after the player that has the item spawns with their character loaded. Override it to give the player whatever the item is supposed to provide.

Parameters

  • _owner Player

ItemBase:on_save(_owner)

shared plugins/items/plugin/lib/classes/sh_item.lua:359

Called on the server before the character of the player that has the item is saved. Override it to store the state of the item.

Parameters

  • _owner Player

ItemBase:play_sound(act, emitter)

shared plugins/items/plugin/lib/classes/sh_item.lua:320

Plays the sound of a menu action, if the item has one. The sound is emitted by the item's entity if it is in the world. Otherwise it is emitted by the player that has the item, which can only be looked up on the server.

Parameters

  • act String name of the action, e.g. 'on_drop'
  • emitter Entity optional, defaults to nil who emits the sound if the item is not in the world; the player that has the item if nil

See also

Registers the item as an item template under its id.

See also

ItemBase:set_action_sound(act, sound_path)

shared plugins/items/plugin/lib/classes/sh_item.lua:305

Sets the sound that is emitted when a menu action is performed on the item.

Parameters

  • act String name of the action, e.g. 'on_drop'
  • sound_path String path to the sound

See also

ItemBase:set_data(id, value)

server plugins/items/plugin/lib/classes/sh_item.lua:367

Sets a custom data value of the item. Server-side only. The data is sent to the clients that know the item and saved with the next save. The value has to be serializable, so no functions or entities.

Parameters

  • id String data key
  • value Any

ItemBase:set_entity(ent)

shared plugins/items/plugin/lib/classes/sh_item.lua:574

Ties the item to the entity that represents it in the world.

Parameters

  • ent Entity

Defined in