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
Methods
Hooks
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.
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
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
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.
Items:ItemEntityTakeDamage(_entity, item_obj, _damage_info)
server plugins/items/plugin/sv_hooks.lua:288Implements 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:409Implements 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>
Runs the 'PlayerThrewGrenade' hook on the next tick after a frag grenade is created.
Parameters
- entity Entity
Implements hook OnItemCreated
Calls the on_created callback of an item instance that has just been created.
Parameters
- item_obj Item
Registers the 'items/bases' and 'items' plugin folders and includes the item bases and the items that come with this plugin.
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
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
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
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:103Implements 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
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
niltrue if the item is lying in the world, not in an inventory
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
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
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.
Draws a halo around the item entity that the local player holds the use key on.
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
Implements hook RegisterConditions
Registers the 'has_item' and 'has_item_data' conditions.
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
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
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
Loads the items of the current map once both the map entities and the database are there, which can happen in either order.