ItemBase class
Part of Items System
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)andon_save(owner): the player that has the item has spawned, or their character is about to be saved.can_transfer(inventory, x, y)andcan_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 anfl_itementity, 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)andpaint_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
Methods
Fancy output if you do print(item_obj).
Returns
- String e.g. '#<Item:Test Item 12>'
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
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
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
Returns a custom data value of the item.
Parameters
- id String data key
- default Any optional, defaults to
nilwhat to return if the value is not set
Returns
- Any the value, or the default if the value is not set
Returns the description of the item.
Returns
- String
Returns the title of the 'destroy' option in the item's menu. Client-side only.
Returns
- String language phrase
Returns the title of the 'drop' option in the item's menu. Client-side only.
Returns
- String language phrase
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
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
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
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
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
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
Returns the internal name of the item, ignoring its print name.
Returns
- String
Returns the skin of the item's model.
Returns
- Number
Returns the title of the 'take' option in the item's menu. Client-side only.
Returns
- String language phrase
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
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
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
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
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
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
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
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
nilwho 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
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
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
Ties the item to the entity that represents it in the world.
Parameters
- ent Entity