cw.inventory class
Defines the cw.inventory library, functions that work on inventory tables of item instances.
They add, remove, find and count instances, total up weight and space, and convert an inventory to and from its
saved form, without networking anything themselves. The client part holds the local player's inventory, kept up to
date by the InvGive, InvTake, InvUpdate and related messages, and the server part sends inventory updates to a
player.
Summary
Methods
InvUpdate Cable message.InvUpdate Cable message.Methods
cw.inventory:AddInstance(inventory, itemTable, quantity)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:24Adds an item instance to an inventory table.
Only changes the table; it does not network anything or touch a player. Use Player:GiveItem
to give a player an item. For quantities above one, new instances of the same item are created
and added as well.
cw.inventory:AddInstance(inventory, item.CreateInstance('ration'), 2)
Parameters
- inventory Inventory The inventory table
- itemTable Item The item instance
- quantity Number optional, defaults to
1How many of the item to add
Returns
- Item The instance added, or
falsewhenitemTableisnilor not an instance
cw.inventory:CalculateSpace(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:52Returns the total space taken by the items in an inventory.
Parameters
- inventory Inventory The inventory table
Returns
- Number The sum of the items'
space
cw.inventory:CalculateWeight(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:69Returns the total weight of the items in an inventory.
Parameters
- inventory Inventory The inventory table
Returns
- Number The sum of the items'
weight
cw.inventory:CreateDuplicate(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:88Returns a copy of an inventory table.
The tables of each unique ID are copied, but the item instances in them are shared.
Parameters
- inventory Inventory The inventory table
Returns
- Inventory The copy
cw.inventory:FindItemByID(inventory, uniqueID, itemID)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:112Finds an item instance in an inventory.
local itemTable = cw.inventory:FindItemByID(player:GetInventory(), 'ration')
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID - itemID Number optional, defaults to
nilItem ID of the instance; any instance of the item whennil
Returns
- Item The instance, or
nilwhen the inventory has none
cw.inventory:FindItemByName(inventory, uniqueID, name)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:150Finds the first instance of an item in an inventory whose name matches, case insensitive.
Both the instance's name and PrintName are compared.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID - name String The name to match
Returns
- Item The instance, or
nilwhen none matches
cw.inventory:FindItemsByName(inventory, uniqueID, name)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:187Finds every instance of an item in an inventory whose name matches, case insensitive.
Both the instance's name and PrintName are compared.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID - name String The name to match
Returns
- List<Item> The matching instances, or
nilwhen the inventory has none of the item
cw.inventory:GetAsItemsList(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:208Returns every item instance in an inventory as a flat list.
Parameters
- inventory Inventory The inventory table
Returns
- List<Item> The instances
Returns the local player's inventory.
Returns
- Inventory The local player's inventory
cw.inventory:GetItemCountByID(inventory, uniqueID)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:222Returns how many instances of an item an inventory has.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID
Returns
- Number The number of instances;
0when the item does not exist
cw.inventory:GetItemsByID(inventory, uniqueID)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:170Returns the instances of an item in an inventory, indexed by item ID.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID
Returns
- Map<Item> The instances indexed by item ID;
nilwhen the inventory has none, an empty table when the item does not exist
Returns the inventory menu panel.
Returns
- Panel The inventory panel, or
nilwhen it has not been created
cw.inventory:HasEquipped(itemTable)
client gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:433Returns whether the local player has an item equipped.
Parameters
- itemTable Item The item instance
Returns
- Boolean Whether the item's
HasPlayerEquippedreturnstruefor the local player
cw.inventory:HasItemByID(inventory, uniqueID)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:238Returns whether an inventory has at least one instance of an item.
The uniqueID is looked up in the inventory as given, so it should be the item's exact unique ID.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID of the item
Returns
- Boolean Whether the inventory has the item;
nilwhen it never had any
cw.inventory:HasItemCountByID(inventory, uniqueID, amount)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:249Returns whether an inventory has at least an amount of instances of an item.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID - amount Number The amount needed
Returns
- Boolean Whether the inventory has at least
amountof the item
cw.inventory:HasItemInstance(inventory, itemTable)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:263Returns whether an inventory contains a specific item instance.
Parameters
- inventory Inventory The inventory table
- itemTable Item The item instance
Returns
- Boolean Whether the instance is in the inventory
cw.inventory:IsEmpty(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:271Returns whether an inventory has no items.
Parameters
- inventory Inventory The inventory table;
nilcounts as empty
Returns
- Boolean Whether the inventory is empty
On the server
Tells a player's client to rebuild its inventory panel on the next frame.
Repeated calls in the same frame send a single InvRebuild message.
Parameters
- player Player The player
On the client
Rebuilds the inventory panel on the next frame.
Does nothing unless the inventory panel is the active menu panel or bForceRebuild is set.
Parameters
- bForceRebuild Boolean optional, defaults to
nilRebuild even when the panel is not active
Defined in
cw.inventory:RemoveInstance(inventory, itemTable)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:290Removes an item instance from an inventory table.
Only changes the table; use Player:TakeItem to take an item from a player.
Parameters
- inventory Inventory The inventory table
- itemTable Item The item instance
Returns
- Item The removed instance;
falsewhenitemTableis not an instance,nilwhen the inventory has none of the item
cw.inventory:RemoveUniqueID(inventory, uniqueID, itemID)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:307Removes an instance of an item from an inventory table.
Parameters
- inventory Inventory The inventory table
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID - itemID Number optional, defaults to
nilItem ID of the instance to remove; the first instance whennil
Returns
- Item The removed instance when no
itemIDwas given; nothing otherwise
cw.inventory:SendUpdateAll(player)
server gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:539Sends every item in a player's inventory to that player.
Parameters
- player Player The player to send to
See also
cw.inventory:SendUpdateByID(player, uniqueID)
server gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:551Sends every instance of an item in a player's inventory to that player over the InvUpdate
Cable message.
Parameters
- player Player The player to send to
- uniqueID String Unique ID, index or name of the item, as accepted by
item.FindByID
cw.inventory:SendUpdateByInstance(player, itemTable)
server gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:528Sends an item instance in a player's inventory to that player over the InvUpdate Cable message.
Parameters
- player Player The player to send to
- itemTable Item The item instance; nothing is sent when
nil
cw.inventory:ToLoadable(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:332Converts a saved inventory back into an inventory of item instances.
Creates an instance for every saved entry and keeps it unless its OnLoaded returns false.
Entries saved under a key that is not the item's current unique ID are dropped. An entry whose item ID is
already taken by an instance of another item is loaded under a new item ID.
Parameters
- inventory Map Saved data of each item ID, indexed by unique ID, as made by
cw.inventory:ToSaveable
Returns
- Inventory The inventory of instances
cw.inventory:ToSaveable(inventory)
shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:371Converts an inventory into a table that can be saved.
For each instance only the data fields that differ from the item's defaults are kept, indexed by
the item ID as a string. An instance is skipped when its OnSaved(newData) returns false.
Parameters
- inventory Inventory The inventory table
Returns
- Map The saved data of each item ID, indexed by unique ID
See also
Returns whether inventory space is limited as well as weight.
Returns
- Boolean The value of the
enable_space_systemconfig