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

Adds an item instance to an inventory table.
Returns the total space taken by the items in an inventory.
Returns the total weight of the items in an inventory.
Returns a copy of an inventory table.
Finds an item instance in an inventory.
Finds the first instance of an item in an inventory whose name matches, case insensitive.
Finds every instance of an item in an inventory whose name matches, case insensitive.
Returns every item instance in an inventory as a flat list.
Returns the local player's inventory.
Returns how many instances of an item an inventory has.
Returns the instances of an item in an inventory, indexed by item ID.
Returns the inventory menu panel.
Returns whether the local player has an item equipped.
Returns whether an inventory has at least one instance of an item.
Returns whether an inventory has at least an amount of instances of an item.
Returns whether an inventory contains a specific item instance.
Returns whether an inventory has no items.
Tells a player's client to rebuild its inventory panel on the next frame.
Removes an item instance from an inventory table.
Removes an instance of an item from an inventory table.
Sends every item in a player's inventory to that player.
Sends every instance of an item in a player's inventory to that player over the InvUpdate Cable message.
Sends an item instance in a player's inventory to that player over the InvUpdate Cable message.
Converts a saved inventory back into an inventory of item instances.
Converts an inventory into a table that can be saved.
Returns whether inventory space is limited as well as weight.

Methods

cw.inventory:AddInstance(inventory, itemTable, quantity)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:24

Adds 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 1 How many of the item to add

Returns

  • Item The instance added, or false when itemTable is nil or not an instance

cw.inventory:CalculateSpace(inventory)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:52

Returns 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:69

Returns 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:88

Returns 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:112

Finds 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 nil Item ID of the instance; any instance of the item when nil

Returns

  • Item The instance, or nil when the inventory has none

cw.inventory:FindItemByName(inventory, uniqueID, name)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:150

Finds 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 nil when none matches

cw.inventory:FindItemsByName(inventory, uniqueID, name)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:187

Finds 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 nil when the inventory has none of the item

cw.inventory:GetAsItemsList(inventory)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:208

Returns 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:222

Returns 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; 0 when the item does not exist

cw.inventory:GetItemsByID(inventory, uniqueID)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:170

Returns 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; nil when the inventory has none, an empty table when the item does not exist

Returns the inventory menu panel.

Returns

  • Panel The inventory panel, or nil when it has not been created

cw.inventory:HasEquipped(itemTable)

client gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:433

Returns whether the local player has an item equipped.

Parameters

  • itemTable Item The item instance

Returns

  • Boolean Whether the item's HasPlayerEquipped returns true for the local player

cw.inventory:HasItemByID(inventory, uniqueID)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:238

Returns 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; nil when it never had any

cw.inventory:HasItemCountByID(inventory, uniqueID, amount)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:249

Returns 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 amount of the item

cw.inventory:HasItemInstance(inventory, itemTable)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:263

Returns 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

Returns whether an inventory has no items.

Parameters

  • inventory Inventory The inventory table; nil counts 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 nil Rebuild even when the panel is not active

Defined in

cw.inventory:RemoveInstance(inventory, itemTable)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:290

Removes 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; false when itemTable is not an instance, nil when the inventory has none of the item

cw.inventory:RemoveUniqueID(inventory, uniqueID, itemID)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:307

Removes 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 nil Item ID of the instance to remove; the first instance when nil

Returns

  • Item The removed instance when no itemID was given; nothing otherwise

cw.inventory:SendUpdateAll(player)

server gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:539

Sends 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:551

Sends 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:528

Sends 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:332

Converts 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

Returns

  • Inventory The inventory of instances

cw.inventory:ToSaveable(inventory)

shared gamemodes/catwork/gamemode/core/libraries/sh_inventory.lua:371

Converts 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_system config

Defined in