CItem class
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
ITEM[itemID].CItem:GetVar lookups of one variable to another variable or a function.CItem:AddData, including its default and network flag.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:198Declares 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
nilSend changes of this field to clients
CItem:AddQueryProxy(var, replacement)
shared gamemodes/catwork/gamemode/core/libraries/sh_item.lua:172Redirects 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
ingredientstable 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
businessset
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
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
nilwhen not found
See also
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
nilValue returned when neither the data nor the field is set
Returns
- Any The value
See also
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
nilValue 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
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.
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
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:461Sends 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