Items¶
How to read what an inventory holds and how to change it. All functions are in
re.Inventory on the server.
Reading¶
| Function | Returns |
|---|---|
GetItems(inventory) |
The stacks: InventoryEntry { Record, Seed, Counter, Quantity } |
GetItem(inventory, item) |
The full item as ItemInfo, or null if the inventory does not hold it |
GetItemData(inventory, item) |
The full item as bytes, for storing it; empty if not held |
GetEquipped(inventory) |
What a player has equipped (player inventories only) |
GetLinkedEntity(inventory) |
The entity a world inventory or your own inventory is linked to |
ItemInfo shows the item as data:
| Field | Meaning |
|---|---|
Record, Seed, Counter, Quantity |
Identity and amount |
LootRecord, LootSeed, PowerLevel |
How a looted item was rolled. Only meaningful when LootRecord is set; otherwise ignore PowerLevel (it may be 0 or float.MaxValue) |
Modifiers |
Stat modifiers saved on the item itself (StatModifier { Stat, Kind, Value }), e.g. an upgrade |
Parts |
Installed mods and attachments (ItemPart { Slot, Record, Seed, Depth, Modifiers }) |
ItemInfo shows constant modifiers only. GetItemData keeps everything, which
is why storing and restoring should use the bytes (see
Persistence).
Giving and taking¶
| Function | What it does |
|---|---|
AddItem(inventory, record, quantity) |
New items made from a TweakDB item record, with the record's default stats: quantity more on the record's stack for a stackable record, otherwise quantity unique items |
AddItemInfo(inventory, info) |
An item built from an ItemInfo, e.g. one you read with GetItem and changed |
AddItemData(inventory, data) |
An item restored exactly from GetItemData bytes |
RemoveItem(inventory, record, quantity) |
Removes that many of a record: from its stack, or that many of its unique items |
RemoveItemExact(inventory, item, quantity) |
Removes that many of exactly one item |
The three adds return an AddItemResult { Status, Items }. Status is Added,
or says why nothing was added: InventoryNotFound, InvalidItem (a record that
is no item of the game, an identity that doesn't fit the record, a quantity of
0), MalformedData (bytes AddItemData can't read), BodyPartItem or
InventoryFull. Items lists the added items as the inventory stores them, a
stack once. The removes return false and change nothing when the inventory is
unknown or holds fewer items than you remove.
An item you build for AddItemInfo needs the identity the game gives its
record: a stackable item seed 2 and nothing of its own (no modifiers, parts or
loot stats), a unique item a seed above 2 and quantity 1.
A unique item whose identity the inventory already holds gets the next free
counter when added, so adding the same GetItemData bytes twice gives two
items. Items carries the new counter: use those refs to equip or remove the
item, not the identity it came with.
var added = re.Inventory.AddItemData(inventory, data);
if (added.Status == re.AddItemStatus.Added)
{
re.Inventory.Equip(inventory, added.Items[0], 0);
}
On a player inventory¶
The player's game owns the items, so the server cannot just write them. Your
call sends the change to the client, the client carries it out and reports the
result. You see it shortly after as an Added or Removed change in
InventoryChanged, not right away in GetItems. These changes
carry the player as Client, not 65535.
An item you give with AddItem gets its rolls and stats from the player's game.
Its first report then arrives as a Generated change, which approval mode holds
(see Approval mode).
Body parts (heads, fists, underwear) can't be given to a player; the adds
return BodyPartItem for them.
- Equipment:
Equip(inventory, item, slot)andUnequip(inventory, area, slot)equip or unequip an item the player holds, including one you just gave.GetEquippedreads the loadout, and a new body (joining, a gender change) gets it equipped again. - Starter items: by default every new player body gets the game's starting
build (money, a pistol, ammo, clothing).
SetStarterItems(re.StarterItemsMode.Never)turns that off server-wide, so players own only what you give them;Alwaysturns it back on. Do this before players join if you restore inventories yourself.
Your own inventories¶
Inventory.Create() makes an empty inventory that only exists for your bundle's
purposes: a car trunk, a team locker, a reward chest. It lives until you call
Destroy, also if your bundle unloads.
To let a player use it, open it for them: Inventory.Open(client, inventory)
shows the game's storage screen with their own inventory on the left. Moves
between the two sides go through the server like any other move.
Link it to an entity with LinkToEntity(inventory, entity) to tie it to a
place: players can then only open it and move items within 5 m of the entity.
If the entity disappears, you get InventoryUnlinked and the
contents stay. Unlink removes the link again.
// A trunk for a car the server spawned
var trunk = re.Inventory.Create();
re.Inventory.LinkToEntity(trunk, car);
re.Inventory.AddItem(trunk, new TDBID("Items.Preset_Unity_Default"), 1);
// Later, when a player interacts with the car
re.Inventory.Open(client, trunk);
Player inventories, stashes and world inventories cannot be destroyed or
linked; they come and go with their player or entity. Open works on every
inventory except a player inventory: you can open another player's stash or a
container's inventory, e.g. for an admin tool.
Limits¶
An inventory holds at most 1024 stacks (a player's 4096), and a stack at most
4,294,967,295 items. Adds beyond that return InventoryFull and add nothing.