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

all()shared
Returns all the registered item templates.
Applies the bodygroups of an item's model to an entity, the way the item entity gets them.
Creates a new instance of an item template, with a new instance id.
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.
Finds an item by a loose query.
Finds all the loaded instances of a certain item template.
Finds an item template by its id.
Finds a loaded item instance by its instance id.
Finds a loaded item instance by its instance id.
Unregisters an instance, so that the server forgets it, and tells the clients that know it to forget it too.
Hands out a new item instance id.
Returns the icon of an item category.
Returns the item instances that are loaded.
Finds the outermost loaded container item that an instance is inside of, going from container item to container item through their parent_id fields.
Returns the players that have been sent an item instance and have not been told to forget it yet.
Includes every item file of a folder through the 'item' pipeline, registering the items.
Builds an instance of a template without registering it anywhere.
Checks whether the table is an item instance rather than an item template.
Checks whether the database is ready to load and save items.
Loads the items of a character from the database, along with the contents of its container items.
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.
Marks an instance as changed, so that it is written to the database with the next save if its row has changed.
Marks the items lying in the world that have moved since they were last written, so that the next save writes them.
Tells clients which item instance an item entity represents.
Tells the clients that know an item instance to forget it, and forgets that they know it.
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.
Sends the custom data of an item instance to clients.
Registers an item template, filling in defaults for every field that is not set.
Registers loaded instances as loaded, which makes them the ones 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.
Reserves the first block of instance ids, unless there is one already.
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.
Sends an item entity and its item instance to a player.
Sets the icon that represents an item category in the spawn menu.
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.
Spawns an item instance in the world as an fl_item entity.
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.
Builds the row of an instance as it is now.
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.

Functions

Returns all the registered item templates.

Returns

  • Map item templates, keyed by item id

Item.apply_bodygroups(entity, item_obj)

shared plugins/items/plugin/lib/sh_item.lua:337

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

Item.create(id, data)

server plugins/items/plugin/lib/sh_item.lua:402

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 nil fields 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

Item.delete_character_items(character_id)

server plugins/items/plugin/lib/sv_storage.lua:1080

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

Item.find_all_instances(id)

shared plugins/items/plugin/lib/sh_item.lua:229

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

Item.find_by_id(id)

shared plugins/items/plugin/lib/sh_item.lua:212

Finds an item template by its id.

Parameters

  • id String item id

Returns

  • Item the template, or nil if not found

Item.find_by_instance_id(instance_id)

shared plugins/items/plugin/lib/sh_item.lua:255

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

Item.find_instance_by_id(instance_id)

shared plugins/items/plugin/lib/sh_item.lua:246

Finds a loaded item instance by its instance id.

Parameters

  • instance_id Number

Returns

  • Item the instance, or nil if not found

Item.forget(item_obj, delete)

server plugins/items/plugin/lib/sv_storage.lua:708

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

Item.get_category_icon(category)

shared plugins/items/plugin/lib/sh_item.lua:369

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

Item.get_instances()

shared plugins/items/plugin/lib/sh_item.lua:52

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

Item.get_outermost(item_obj)

server plugins/items/plugin/lib/sv_storage.lua:376

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

Item.get_receivers(item_obj)

server plugins/items/plugin/lib/sh_item.lua:432

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

Item.include_items(directory)

shared plugins/items/plugin/lib/sh_item.lua:353

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:146

Builds 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 nil fields to override on the instance
  • data Map optional, defaults to nil custom data of the instance

Returns

  • Item the new instance

Item.is_instance(item_obj)

shared plugins/items/plugin/lib/sh_item.lua:325

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

Item.is_storage_ready()

server plugins/items/plugin/lib/sv_storage.lua:117

Checks whether the database is ready to load and save items. Server-side only.

Returns

  • Boolean

Item.load_character(character_id, callback)

server plugins/items/plugin/lib/sv_storage.lua:655

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

Item.load_map(callback)

server plugins/items/plugin/lib/sv_storage.lua:669

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 nil receives the List<Item> of the loaded instances

Item.mark_dirty(item_obj)

server plugins/items/plugin/lib/sv_storage.lua:364

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

Item.mark_world_dirty()

server plugins/items/plugin/lib/sv_storage.lua:1066

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.

Item.network_entity_data(target, ent)

server plugins/items/plugin/lib/sh_item.lua:532

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

Item.network_forget(item_obj)

server plugins/items/plugin/lib/sh_item.lua:477

Tells the clients that know an item instance to forget it, and forgets that they know it. Server-side only.

Parameters

  • item_obj Item

Item.network_item(target, instance_id)

server plugins/items/plugin/lib/sh_item.lua:514

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

Item.network_item_data(target, item_obj)

server plugins/items/plugin/lib/sh_item.lua:498

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

Item.register(id, data)

shared plugins/items/plugin/lib/sh_item.lua:60

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 nil item id; made out of data.name when omitted
  • data Item item table to store as the template

Item.register_loaded(items)

server plugins/items/plugin/lib/sv_storage.lua:629

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

Item.remove(instance_id)

shared plugins/items/plugin/lib/sh_item.lua:294

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.

Item.save(items, sync)

server plugins/items/plugin/lib/sv_storage.lua:969

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 nil the instances to write; all marked instances if nil
  • sync Boolean optional, defaults to false wait 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

Item.send_entity(target, ent)

server plugins/items/plugin/lib/sh_item.lua:545

Sends an item entity and its item instance to a player. Server-side only.

Parameters

  • target Player
  • ent Entity the fl_item entity

Item.set_category_icon(category, icon)

shared plugins/items/plugin/lib/sh_item.lua:362

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'

Item.set_holder(item_obj, inventory)

server plugins/items/plugin/lib/sv_storage.lua:347

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:570

Spawns 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 exact is set
  • angles Angle optional, defaults to nil
  • item_obj Item item instance; templates cannot be spawned
  • dropper Player/Map/Boolean optional, defaults to nil the 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 false put the entity exactly at the position, without raising it

Returns

  • Entity the item entity
  • Item the spawned item; nothing if the arguments are invalid

Item.to_networkable(item_obj)

shared plugins/items/plugin/lib/sh_item.lua:182

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

Item.to_record(item_obj)

server plugins/items/plugin/lib/sv_storage.lua:395

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>

Defined in