Item library
Part of Items System
The Item library keeps track of item templates and item instances.
Templates are the item classes registered from item files. The item pipeline, defined
at the end of this file, creates an ItemBase object for every file, runs the file with
that object as ITEM and passes it to Item.register. Item.create makes an instance
out of a template, with an instance id of its own, and Item.spawn puts an instance
into the world as an fl_item entity.
An instance only holds what is its own: its instance id, its custom data (data), the
fields that Item.create was told to override, and where it is. Everything else it
reads from its template through its metatable, so it costs little memory. When an item
file is registered again, after a Lua refresh for instance, the loaded instances of the
item read from the new template.
Instances live in the items table of the database, one row each, and the server keeps
in memory only those that are in use: the items of the characters that players have
loaded, and the items of the current map, in containers or lying in the world. How they
are loaded and written back is described in the Items plugin's lib/sv_storage.lua.
Item.remove deletes an instance for good.
Clients only get the instances they need: those in the inventories they receive, and
those lying in the world near them, which a client asks for once the entity of the item
reaches it. The server remembers which players know an instance, so that changes and
the removal of the instance reach exactly them (Item.network_item,
Item.network_item_data). Plugins can add fields to what is sent through the
PreItemNetwork hook.
An item entity remembers the character that dropped it: Item.spawn takes the dropper
as its last argument, which the Inventory plugin passes whenever a player drops an item
from an inventory.
Summary
Functions
parent_id fields.character_id, instance_id and container_id fields of the inventory name.Functions
Returns all the registered item templates.
Returns
- Map item templates, keyed by item id
Applies the bodygroups of an item's model to an entity, the way the item entity gets them. Bodygroups that are given by name and that the model of the entity does not have are skipped, so the model has to be set first.
Parameters
- entity Entity the entity to set the bodygroups on
- item_obj Item the item whose model bodygroups to apply
See also
Creates a new instance of an item template, with a new instance id. Server-side only. Runs the 'OnItemCreated' hook. The instance is written to the database once it is somewhere: in an inventory that is saved, or in the world. An instance that is still nowhere when the changes are written is removed, so put it where it belongs right away.
local item_obj = Item.create('test_item', { name = 'Some Item' })
if item_obj then
Item.spawn(actor:GetEyeTraceNoCursor().HitPos, nil, item_obj)
end
Parameters
- id String item id of the template
- data Map optional, defaults to
nilfields to override on the new instance; a data field is merged into its custom data. The overridden fields are saved with the instance
Returns
- Item the new instance, or nil if there is no such template or no instance id could be reserved
Deletes the items of a character from the database, along with what is inside of its container items, and forgets the instances that are loaded. Items that the character has dropped into the world no longer remember it. Server-side only.
Parameters
- character_id Number
Finds an item by a loose query. A number is treated as an instance id. A string is compared against the template ids and matched as a Lua pattern against the template names and print names.
Parameters
- name String/Number item id or (part of) item name, or an instance id
Returns
- Item first matching template, or the instance for a number; nil if not found
Finds all the loaded instances of a certain item template.
Parameters
- id String item id
Returns
- Map item instances keyed by instance id, or nil if there is no such template
Finds an item template by its id.
Parameters
- id String item id
Returns
- Item the template, or nil if not found
Finds a loaded item instance by its instance id. The same as Item.find_instance_by_id.
Parameters
- instance_id Number
Returns
- Item the instance, or nil if not found
Finds a loaded item instance by its instance id.
Parameters
- instance_id Number
Returns
- Item the instance, or nil if not found
Unregisters an instance, so that the server forgets it, and tells the clients that
know it to forget it too. With delete the row of the instance is deleted with the next
save. Changes that are not written yet are lost; use Item.unload to keep them.
Server-side only.
Parameters
- item_obj Item
- delete Boolean optional, defaults to
false
Hands out a new item instance id. No id is ever handed out twice, and a restart does not change that. Waits for the database only when the reserved ids have run out. Server-side only.
Returns
- Number the new instance id, or nil if the database is not ready
Returns the icon of an item category.
Parameters
- category String category id
Returns
- String path to the icon; 'icon16/bricks.png' if the category has none
Returns the item instances that are loaded. On the server those are the instances that are in use, see the top of this file; on a client those that the server has sent.
Returns
- Map item instances, keyed by instance id
Finds the outermost loaded container item that an instance is inside of, going from
container item to container item through their parent_id fields. Server-side only.
Parameters
- item_obj Item
Returns
- Item the outermost container item, or the instance itself if it is not inside of one; Number how many container items deep the instance is
Returns the players that have been sent an item instance and have not been told to forget it yet. Server-side only.
Parameters
- item_obj Item
Returns
- List<Player> connected players
Includes every item file of a folder through the 'item' pipeline, registering the items.
Parameters
- directory String path to the folder, e.g. plugin:get_folder()..'/items/'
Item.instantiate(template, instance_id, overrides, data)
shared plugins/items/plugin/lib/sh_item.lua:146Builds an instance of a template without registering it anywhere. The instance gets
its own copy of the default custom data of the template, with data merged on top.
Fields of overrides are set on the instance and kept in its overrides field, so that
they are saved with it; a data field among them is merged into the custom data instead.
Item.create and the database loader build instances with it.
Parameters
- template Item the item template
- instance_id Number
- overrides Map optional, defaults to
nilfields to override on the instance - data Map optional, defaults to
nilcustom data of the instance
Returns
- Item the new instance
Checks whether the table is an item instance rather than an item template.
Parameters
- item_obj Item
Returns
- Boolean nil if the argument is not a table
Checks whether the database is ready to load and save items. Server-side only.
Returns
- Boolean
Loads the items of a character from the database, along with the contents of its
container items. The loaded instances are not in use until they are passed to
Item.register_loaded, so that a load whose result is no longer wanted can simply be
dropped. Instances that are loaded already are passed as they are. Server-side only.
Parameters
- character_id Number
- callback Function receives the List<Item> of the instances
Returns
- Any what the callback returns if it runs right away, nil otherwise
Loads the items of the current map from the database: those lying in the world, those in the container props, and what is inside of their container items. Spawns the items that lie in the world and runs the 'OnMapItemsLoaded' hook. Server-side only.
Parameters
- callback Function optional, defaults to
nilreceives the List<Item> of the loaded instances
Marks an instance as changed, so that it is written to the database with the next save if its row has changed. Server-side only.
Parameters
- item_obj Item
Marks the items lying in the world that have moved since they were last written, so that the next save writes them. Server-side only.
Tells clients which item instance an item entity represents. Server-side only.
Parameters
- target Player/List<Player>/Nil who to send to; nil sends to the players that know the instance of the entity
- ent Entity the fl_item entity
Tells the clients that know an item instance to forget it, and forgets that they know it. Server-side only.
Parameters
- item_obj Item
Sends what a client needs to know about an item instance to clients, which build their own copies of the instance out of it, and remembers that they know the instance. Server-side only.
Parameters
- target Player/List<Player>/Nil who to send to; nil sends to the players that know the instance
- instance_id Number
Sends the custom data of an item instance to clients. Server-side only. Does nothing if the item is a template.
Parameters
- target Player/List<Player>/Nil who to send to; nil sends to the players that know the instance
- item_obj Item
Registers an item template, filling in defaults for every field that is not set. Item files normally get here through ItemBase:register, which the 'item' pipeline calls.
Parameters
- id String optional, defaults to
nilitem id; made out of data.name when omitted - data Item item table to store as the template
Registers loaded instances as loaded, which makes them the ones in use. An instance whose row is in use already by now is replaced by the one in use. Server-side only.
Parameters
- items List<Item> instances from Item.load_character
Returns
- List<Item> the instances in use
Removes an item instance along with its entity in the world, if it has one, and along with everything inside of it, if it is a container item. Does not take the item out of the inventory that holds it. On the server it also deletes the instance from the database and tells the clients that know the instance to forget it; the client only forgets the instance, since the entity is removed by the server.
Parameters
- instance_id Number/Item instance id, or the item instance itself
Reserves the first block of instance ids, unless there is one already. Called when the database is ready, while the adapter is in sync mode.
Writes the changes to the database: inserts the new instances, updates the changed rows and deletes the rows of the removed instances, all in one transaction. Instances whose own statement failed the last time are written on their own. Without a list, every marked instance is written. An instance that is held by nothing and lies nowhere is removed instead. Server-side only.
Parameters
- items List<Item> optional, defaults to
nilthe instances to write; all marked instances if nil - sync Boolean optional, defaults to
falsewait for the writes started before, then write and wait until the database has run the queries
Returns
- List<Item> in sync mode, the instances that could not be written
Sends an item entity and its item instance to a player. Server-side only.
Parameters
- target Player
- ent Entity the fl_item entity
Sets the icon that represents an item category in the spawn menu.
Parameters
- category String category id, e.g. 'item.category.weapon'
- icon String path to the icon, e.g. 'icon16/gun.png'
Makes an inventory the holder of an instance: the instance is saved as being in the
character, container item or container prop that the inventory belongs to, which the
character_id, instance_id and container_id fields of the inventory name. An
inventory without any of them holds the instance without saving it there. Without an
inventory the instance is held by nothing, which is how it is when it lies in the world
or has been taken out of its inventory. Marks the instance as changed.
Parameters
- item_obj Item
- inventory Inventory optional, defaults to
nil
Item.spawn(position, angles, item_obj, dropper, exact)
server plugins/items/plugin/lib/sh_item.lua:570Spawns an item instance in the world as an fl_item entity. Server-side only. The item stops being held by the inventory it was saved in, so take it out of its inventory first. The entity remembers the character of the dropper, and the on_entity_spawned callback of the item is called once the entity is in the world. Clients ask for the item once the entity reaches them.
local item_obj = Item.create('test_item')
local trace = actor:GetEyeTraceNoCursor()
local ent = Item.spawn(trace.HitPos, Angle(0, 0, 0), item_obj)
Parameters
- position Vector where to put the item; it is raised by the height of its bounds unless
exactis set - angles Angle optional, defaults to
nil - item_obj Item item instance; templates cannot be spawned
- dropper Player/Map/Boolean optional, defaults to
nilthe player who drops the item, or a table with the character_id and steam_id fields of an earlier drop; nil or false if nobody does - exact Boolean optional, defaults to
falseput the entity exactly at the position, without raising it
Returns
- Entity the item entity
- Item the spawned item; nothing if the arguments are invalid
Builds a plain table with what a client needs to know about an item instance: its template, its instance id, its custom data and its overridden fields. Runs the 'PreItemNetwork' hook so that plugins can add fields of their own.
Parameters
- item_obj Item
Returns
- Map networkable fields, or nil if no item was given
Builds the row of an instance as it is now.
Parameters
- item_obj Item
Returns
- Map the values of the columns, nil if the instance is in an inventory that is not saved, false if it is nowhere
Unloads instances because nothing uses them anymore: the clients forget them right away, their changes are written, and the server forgets them once that has worked. Server-side only.
Parameters
- items List<Item>