Items are the objects that players carry, use, equip and drop. An item starts as an item class, also called a template: a file in the items/ folder of a plugin or of the schema that is loaded through the item pipeline. The pipeline creates an ItemBase object, exposes it as the ITEM global while the file runs and registers it under the id taken from the file name (sh_test_item.lua becomes test_item). An item class can build upon a base class from an items/bases/ folder with ItemBase:base_off; this plugin comes with ItemUsable, ItemConsumable, ItemAmmo, ItemEquipable, ItemWeapon, ItemThrowable and ItemWearable.

What players actually own are item instances, made out of a template by Item.create, each with its own numeric instance id and custom data. An instance either sits in an inventory (see the Inventory plugin) or lies in the world as an fl_item entity spawned by Item.spawn. Every instance is a row of the items table of the database; the server loads the instances when they are needed and writes back what changes (see lib/sv_storage.lua), and sends each client the instances it needs. Players act on an instance through its menu: the use, take, drop and destroy options and the custom buttons of the item all end up in ItemBase:do_menu_action on the server, which runs the PlayerCanUseItem, PlayerUseItem, PlayerTakeItem, PlayerDropItem, PlayerDestroyItem and PlayerUsedItem hooks.

The config keys of the plugin, all off by default, let players destroy the items in their inventories (item_destroy), give the items that lie in the world health so that they can be destroyed by damage (item_entity_health), keep players away from the items that another of their own characters has dropped (item_drop_ownership), make ammunition require a weapon that uses it (ammo_requires_weapon) and drop the equipped weapons of players who die (drop_weapons_on_death).

Once this plugin is loaded, every plugin and the schema get their items/bases/ and items/ folders included, so adding an item only takes a file:

ITEM:base_off 'ItemUsable'
ITEM.name = 'Bandage'
ITEM.description = 'A roll of clean cloth.'
ITEM.model = 'models/props_lab/box01a.mdl'

function ITEM:use(actor)
  actor:SetHealth(math.min(actor:Health() + 10, actor:GetMaxHealth()))
end

The plugin also registers the has_item and has_item_data conditions.

Summary

Hooks

Reserves the first block of item instance ids and loads the items of the map, if the map entities are there already.
Lets the on_drop callback of the item decide whether the player is able to drop it.
Drops the equipped weapon items of a player who is dying, if the 'drop_weapons_on_death' config is on.
Fills the circular progress indicator while the local player holds the use key on an item.
Loads the items of the map once the map entities have been created, if the database is ready already.
Keeps container items that still have something inside from being damaged while they lie in the world, so that their contents are not lost along with them.
Calls the on_loadout callback of the items of a character that have just been loaded, if the player has spawned and has not had them yet.
Runs the 'PlayerThrewGrenade' hook on the next tick after a frag grenade is created.
Calls the on_created callback of an item instance that has just been created.
Registers the 'items/bases' and 'items' plugin folders and includes the item bases and the items that come with this plugin.
Keeps players from destroying equipped items that cannot be unequipped, and container items that still have something inside.
Prevents menu actions of players who are dead or lie on the ground as a ragdoll, on items that the player does not have, and on items in the world that are too far away from the player, obstructed, not being looked at or already on their way out of the world.
Destroys an item that is in an inventory on request of the player: asks the 'PlayerCanDestroyItem' hook and the on_destroy callback of the item, takes the item out of its inventory, removes the instance and runs the 'PlayerDestroyedItem' hook.
Sends an item that is lying in the world again to the clients that know it, after a menu action has been performed on it.
Sends the item entity the player has used to them, along with its item, and tells their client to open the menu of the item.
Opens the menu of an item with its custom buttons and the use, take and drop options, and the destroy option if the item is destroyable and in an inventory.
Includes the items of a plugin when its 'items' folder is being included.
Calls the on_loadout callback of every item that the player has, once per spawn: on the next tick after they spawn with their character loaded, or once the items of the character have been loaded if that takes longer.
Writes the changes of the items to the database once everything else has been saved.
Draws a halo around the item entity that the local player holds the use key on.
Calls the on_save callback of every item that the player has before their character is saved, and writes the changes of the items along with the character.
Registers the 'has_item' and 'has_item_data' conditions.
Marks the items lying in the world that have moved, so that they are written with the next save.
Think()client
Aborts taking an item once it gets too far from the center of the local player's screen.

Methods

Asks the local player whether they really want to destroy an item, and requests the 'on_destroy' menu action from the server if they do.
Drops the equipped weapon items of a player into the world at the position of the player.
Loads the items of the current map once both the map entities and the database are there, which can happen in either order.

Hooks

Items:ActiveRecordReady()

server plugins/items/plugin/sv_hooks.lua:34

Implements hook ActiveRecordReady

Reserves the first block of item instance ids and loads the items of the map, if the map entities are there already.

Items:CanPlayerDropItem(actor, item_obj)

server plugins/items/plugin/sv_hooks.lua:177

Implements hook CanPlayerDropItem

Lets the on_drop callback of the item decide whether the player is able to drop it.

Parameters

  • actor Player
  • item_obj Item

Returns

  • Boolean false to prevent the drop, nil otherwise

Items:DoPlayerDeath(victim, _attacker, _damage_info)

server plugins/items/plugin/sv_hooks.lua:370

Drops the equipped weapon items of a player who is dying, if the 'drop_weapons_on_death' config is on. It is done here rather than in PlayerDeath so that the weapons are still there for the items to store their clips, and so that the items are gone from the inventory before the character is saved.

Parameters

  • victim Player
  • _attacker Entity
  • _damage_info CTakeDamageInfo

Items:HUDPaint()

client plugins/items/plugin/cl_hooks.lua:12

Fills the circular progress indicator while the local player holds the use key on an item.

Items:InitPostEntity()

server plugins/items/plugin/sv_hooks.lua:42

Loads the items of the map once the map entities have been created, if the database is ready already.

Items:ItemEntityTakeDamage(_entity, item_obj, _damage_info)

server plugins/items/plugin/sv_hooks.lua:288

Implements hook ItemEntityTakeDamage

Keeps container items that still have something inside from being damaged while they lie in the world, so that their contents are not lost along with them.

Parameters

  • _entity Entity the fl_item entity
  • item_obj Item
  • _damage_info CTakeDamageInfo

Returns

  • Boolean false to ignore the damage, nil otherwise

Items:OnCharacterItemsLoaded(owner, _character, _items)

server plugins/items/plugin/sv_hooks.lua:409

Implements hook OnCharacterItemsLoaded

Calls the on_loadout callback of the items of a character that have just been loaded, if the player has spawned and has not had them yet.

Parameters

  • owner Player
  • _character Character
  • _items List<Item>

Items:OnEntityCreated(entity)

server plugins/items/plugin/sv_hooks.lua:50

Runs the 'PlayerThrewGrenade' hook on the next tick after a frag grenade is created.

Parameters

  • entity Entity

Items:OnItemCreated(item_obj)

server plugins/items/plugin/sv_hooks.lua:437

Implements hook OnItemCreated

Calls the on_created callback of an item instance that has just been created.

Parameters

  • item_obj Item

Items:OnPluginLoaded()

shared plugins/items/plugin/sh_plugin.lua:50

Registers the 'items/bases' and 'items' plugin folders and includes the item bases and the items that come with this plugin.

Items:PlayerCanDestroyItem(actor, item_obj)

server plugins/items/plugin/sv_hooks.lua:271

Implements hook PlayerCanDestroyItem

Keeps players from destroying equipped items that cannot be unequipped, and container items that still have something inside.

Parameters

  • actor Player
  • item_obj Item

Returns

  • Boolean false to prevent the destruction
  • String error phrase; nothing to allow it

Items:PlayerCanUseItem(actor, item_obj, _action, ...)

server plugins/items/plugin/sv_hooks.lua:119

Implements hook PlayerCanUseItem

Prevents menu actions of players who are dead or lie on the ground as a ragdoll, on items that the player does not have, and on items in the world that are too far away from the player, obstructed, not being looked at or already on their way out of the world. With the 'item_drop_ownership' config on, it also keeps players away from the items in the world that another of their own characters has dropped.

Parameters

  • actor Player
  • item_obj Item
  • _action String name of the menu action
  • ... Vararg extra arguments of the action

Returns

  • Boolean false to prevent the action, nil otherwise

Items:PlayerDestroyItem(actor, item_obj)

server plugins/items/plugin/sv_hooks.lua:191

Implements hook PlayerDestroyItem

Destroys an item that is in an inventory on request of the player: asks the 'PlayerCanDestroyItem' hook and the on_destroy callback of the item, takes the item out of its inventory, removes the instance and runs the 'PlayerDestroyedItem' hook.

Parameters

  • actor Player
  • item_obj Item

Returns

  • Boolean false if the item has not been destroyed, nil otherwise

Items:PlayerUsedItem(_actor, item_obj, _act, ...)

server plugins/items/plugin/sv_hooks.lua:166

Implements hook PlayerUsedItem

Sends an item that is lying in the world again to the clients that know it, after a menu action has been performed on it.

Parameters

  • _actor Player
  • item_obj Item
  • _act String name of the menu action
  • ... Vararg extra arguments of the action

Items:PlayerUseItemEntity(activator, entity, _item_obj)

server plugins/items/plugin/sv_hooks.lua:103

Implements hook PlayerUseItemEntity

Sends the item entity the player has used to them, along with its item, and tells their client to open the menu of the item.

Parameters

  • activator Player
  • entity Entity the fl_item entity
  • _item_obj Item

Items:PlayerUseItemMenu(instance_id, is_entity)

client plugins/items/plugin/cl_hooks.lua:83

Implements hook PlayerUseItemMenu

Opens the menu of an item with its custom buttons and the use, take and drop options, and the destroy option if the item is destroyable and in an inventory.

Parameters

  • instance_id Number instance id of the item
  • is_entity Boolean optional, defaults to nil true if the item is lying in the world, not in an inventory

Items:PluginIncludeFolder(extra, folder)

shared plugins/items/plugin/sh_plugin.lua:62

Implements hook PluginIncludeFolder

Includes the items of a plugin when its 'items' folder is being included.

Parameters

  • extra String name of the folder being included
  • folder String path to the plugin's folder

Returns

  • Boolean true if the folder was handled here, nil otherwise

Items:PostPlayerSpawn(actor)

server plugins/items/plugin/sv_hooks.lua:390

Implements hook PostPlayerSpawn

Calls the on_loadout callback of every item that the player has, once per spawn: on the next tick after they spawn with their character loaded, or once the items of the character have been loaded if that takes longer.

Parameters

  • actor Player

Items:PostSaveData()

server plugins/items/plugin/sv_hooks.lua:78

Implements hook PostSaveData

Writes the changes of the items to the database once everything else has been saved. While the server shuts down or changes the map, it waits until the database has run the queries, and reports the items that could not be written.

Items:PreDrawHalos()

client plugins/items/plugin/cl_hooks.lua:28

Draws a halo around the item entity that the local player holds the use key on.

Items:PreSaveCharacter(owner, _index)

server plugins/items/plugin/sv_hooks.lua:421

Implements hook PreSaveCharacter

Calls the on_save callback of every item that the player has before their character is saved, and writes the changes of the items along with the character.

Parameters

  • owner Player
  • _index Character the character that is being saved

Items:RegisterConditions()

shared plugins/items/plugin/sh_plugin.lua:71

Implements hook RegisterConditions

Registers the 'has_item' and 'has_item_data' conditions.

Items:SaveData()

server plugins/items/plugin/sv_hooks.lua:71

Implements hook SaveData

Marks the items lying in the world that have moved, so that they are written with the next save.

Aborts taking an item once it gets too far from the center of the local player's screen. The server is asked once per hold.

Methods

Items:confirm_destroy(item_obj)

client plugins/items/plugin/cl_hooks.lua:70

Asks the local player whether they really want to destroy an item, and requests the 'on_destroy' menu action from the server if they do.

Parameters

  • item_obj Item the item instance to destroy

Items:drop_weapons(victim)

server plugins/items/plugin/sv_hooks.lua:299

Drops the equipped weapon items of a player into the world at the position of the player. Each item goes through the 'PlayerCanDropWeaponOnDeath' and 'CanPlayerDropItem' hooks first; the 'PlayerDroppedWeaponOnDeath' hook is run for every item that drops.

Parameters

  • victim Player

Returns

  • List<Entity> the item entities that have been spawned

Items:load_map_items()

server plugins/items/plugin/sv_hooks.lua:24

Loads the items of the current map once both the map entities and the database are there, which can happen in either order.

Defined in