The base class of every item definition and item instance.

Item files are loaded by item.IncludeItems, which creates a CItem named ITEM with item.New, runs the file and registers it. An item file sets fields on ITEM and defines callbacks:

ITEM.name = 'Ration'
ITEM.model = 'models/weapons/w_package.mdl'
ITEM.weight = 0.5
ITEM.category = 'Consumables'
ITEM.description = 'A small, sealed ration package.'
ITEM:AddData('Opened', false, true)

function ITEM:OnUse(player, itemEntity)
  player:SetHealth(math.min(player:Health() + 10, player:GetMaxHealth()))
end

function ITEM:OnDrop(player, position) end

Common fields: name, PrintName, description, model, skin, weight, space, cost, batch (how many are ordered at once), business (whether it can be ordered), category, baseItem (unique ID of the item to inherit from), isBaseItem, useSound, dropSound and destroySound (a sound path, a List of paths to pick from, or false for silence). Instances additionally have a non-zero itemID and per-instance data.

Callbacks used by the item library: OnSetup, OnInstantiated, OnUse (return false to cancel, true to keep the item), OnDrop, OnCreateDropEntity, OnDestroy (return false to cancel), OnLoaded and OnSaved (return false to skip the item), HasPlayerEquipped, and on the client GetClientSideModel, GetClientSideSkin, GetClientSideName, GetClientSideInfo and GetClientSideDescription.

Summary

Methods

Converts the item to a string of the form ITEM[itemID].
Declares a per-instance data field with a default value.
Redirects CItem:GetVar lookups of one variable to another variable or a function.
Adds a recipe of ingredients that ordering the item consumes.
Returns whether the item can be ordered from the business menu.
Returns whether a player can afford to order the item.
CItem()shared
Constructs a new item object; the base class does nothing here.
Takes the cost of an order from a player.
Returns the definition of an item by unique ID, usually one of this item's bases.
Returns a data field of the item, falling back to the item field of the same name.
Returns a variable of the item, preferring the instance's data over the definition's fields.
Returns whether the item is, or inherits from, the item with a unique ID.
Returns whether a data field is sent to clients when it changes.
Returns whether the item is an instance rather than a definition.
Returns whether another item is the same instance as this one.
Sends the item's queued data changes to its observers after one second.
Sets a field on the item.
Registers the item definition.
Removes a data field declared with CItem:AddData, including its default and network flag.
Sets a data field of an item instance and networks it when the field is networked.
Sends a menu option chosen for the item to the server over the MenuOption Cable message.

Methods

Converts the item to a string of the form ITEM[itemID].

Returns

  • String The string representation

CItem:AddData(dataName, value, bNetworked)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:198

Declares a per-instance data field with a default value.

Call it in an item file. The default is used when instances are created, values equal to it are left out when the inventory is saved, and networked fields are sent to clients when changed with CItem:SetData.

ITEM:AddData('Rounds', -1, true)

Parameters

  • dataName String Name of the data field
  • value Any Default value
  • bNetworked Boolean optional, defaults to nil Send changes of this field to clients

CItem:AddQueryProxy(var, replacement)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:172

Redirects CItem:GetVar lookups of one variable to another variable or a function.

ITEM:AddQueryProxy('weight', function(itemTable)
  return itemTable:GetData('Rounds') * 0.01
end)

Parameters

  • var String Name of the variable to redirect
  • replacement Any Name of the variable to read instead, or a function that receives the item and returns the value

Adds a recipe of ingredients that ordering the item consumes.

Arguments alternate between an ingredient's unique ID and the amount needed. When an item has recipes, ordering it requires the ingredients of a recipe the player has access to.

ITEM:AddRecipe('scrap_metal', 2, 'cloth', 1)

Parameters

  • ... Any Pairs of ingredient unique IDs and amounts

Returns

  • Map The recipe, with an ingredients table of amounts indexed by unique ID

Returns whether the item can be ordered from the business menu.

Returns

  • Boolean Whether the item is not a base item and has business set

CItem:CanPlayerAfford(player)

server gamemodes/catwork/gamemode/core/libraries/sh_item.lua:397

Returns whether a player can afford to order the item.

The player must have cost * batch cash and, when the item has recipes, every ingredient of a recipe they have access to.

Parameters

  • player Player The player ordering the item

Returns

  • Boolean Whether the player can afford the order

Constructs a new item object; the base class does nothing here.

item.New sets up the per-item tables after construction.

Takes the cost of an order from a player.

Takes the ingredients of the first recipe the player has access to and owns enough of, then takes cost * batch cash and logs the order. Call CItem:CanPlayerAfford first.

Parameters

  • player Player The player ordering the item

CItem:GetBaseClass(uniqueID)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:262

Returns the definition of an item by unique ID, usually one of this item's bases.

Parameters

  • uniqueID String Unique ID of the item

Returns

  • Item The item definition, or nil when not found

See also

CItem:GetData(dataName, default)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:279

Returns a data field of the item, falling back to the item field of the same name.

Falsy data values (false, nil) fall through to the item field and then to default.

Parameters

  • dataName String Name of the data field
  • default Any optional, defaults to nil Value returned when neither the data nor the field is set

Returns

  • Any The value

See also

CItem:GetVar(varName, failSafe)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:127

Returns a variable of the item, preferring the instance's data over the definition's fields.

The lookup order is the item's data, then any query proxy added with CItem:AddQueryProxy, then the field on the item itself. On the client, name is read from PrintName when it is set, and string values are translated with cw.lang:TranslateText.

local weight = itemTable:GetVar('weight', 1)

Parameters

  • varName String Name of the variable
  • failSafe Any optional, defaults to nil Value returned when the variable is not set

Returns

  • Any The variable's value, or failSafe

Returns whether the item is, or inherits from, the item with a unique ID.

Follows the baseItem chain through every ancestor.

if itemTable:IsBasedFrom('weapon_base') then
  print(itemTable('name')..' is a weapon.')
end

Parameters

  • uniqueID String Unique ID of the base item

Returns

  • Boolean Whether the item is based on that item

Returns whether a data field is sent to clients when it changes.

Parameters

  • key String Name of the data field

Returns

  • Boolean Whether the field was declared networked with CItem:AddData

Returns whether the item is an instance rather than a definition.

Returns

  • Boolean Whether the item has a non-zero itemID

CItem:IsTheSameAs(itemTable)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:317

Returns whether another item is the same instance as this one.

Parameters

  • itemTable Item The item to compare; may be nil

Returns

  • Boolean Whether both have the same unique ID and item ID

Sends the item's queued data changes to its observers after one second.

Does nothing when a send is already scheduled; changes made in the meantime are sent with it.

CItem:Override(varName, value)

shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:181

Sets a field on the item.

Equivalent to assigning the field directly; provided to pair with CItem:GetVar.

Parameters

  • varName String Name of the field
  • value Any The new value

Registers the item definition.

See also

Removes a data field declared with CItem:AddData, including its default and network flag.

Parameters

  • dataName String Name of the data field

CItem:SetData(dataName, value)

server gamemodes/catwork/gamemode/core/libraries/sh_item.lua:425

Sets a data field of an item instance and networks it when the field is networked.

Only changes fields declared with CItem:AddData (whose current value is not nil) on instances. Networked changes are queued and sent to observers one second later with item.SendUpdate.

Parameters

  • dataName String Name of the data field
  • value Any The new value

See also

CItem:SubmitOption(option, data, entity)

client gamemodes/catwork/gamemode/core/libraries/sh_item.lua:461

Sends a menu option chosen for the item to the server over the MenuOption Cable message.

Parameters

  • option String The option chosen
  • data Any Extra data for the option
  • entity Entity The item entity the option was chosen on, if any

Defined in