Inventories¶
The platform has a built-in inventory sync that syncs player inventories, player stashes,
the contents of containers and drops in the world, and inventories your bundles creates.
Server code can reads and changes them through the re.Inventory APIs and gets an event
for every change, so it can log, store or police what players own.
This page is an overview, these pages below go into detail:
| Page | What it covers |
|---|---|
| Items | Reading items, giving and taking them, players' equipment, your own inventories |
| Events | What your bundles receive when an inventory changes |
| Approval mode | Checking changes before they are replicated / stored, sanity checks, controlling loot |
| Persistence | Saving inventories to a database and restoring them |
The kinds of inventories¶
| Kind | What it is | Exists | Changed by |
|---|---|---|---|
| Player inventory | What a player carries | From shortly after joining (PlayerInventoryCreated) until they leave |
The player's game, moves, your API calls |
| Player stash | The stash in V's apartments, one per player, shown at every stash | From joining until they leave | Moves in the stash screen, your API calls |
| World inventory | A loot container or a drop in the world | From the first client near it (WorldInventoryCreated) until the entity is gone |
Takes and puts of any player within 5 m, your API calls |
| Your own inventory | Anything you want it to be. A car trunk, a shared locker, a shop | From Inventory.Create() until Inventory.Destroy() |
Moves of players you Open it for, your API calls |
Every inventory has a re.InventoryId. Get a player's with
re.Inventory.GetPlayerInventory(client) and re.Inventory.GetPlayerStash(client).
Everything lives in memory. When a player leaves, their inventory and stash
are destroyed right after the ClientDisconnecting handlers ran, and nothing
survives a server restart. If you want items to persist, see
Persistence.
What you can do with this API¶
Who decides what¶
There are two ways an inventory changes, and they differ in how far you can trust them:
Moves go through the server. When a player takes something out of a container, puts something into their stash or drops an item, the client only asks. The server checks that the item is really there, that the player is close enough and allowed to, applies the move and then tells the client. A modified client cannot fake a move.
The game's own changes to a player's items happen on the client first. This is unavoidable based on how the game expects inventory operations to work. Buying from a vendor, crafting, quest rewards, using up ammo, upgrading a weapon, installing a mod. The client applies them and reports the result. A modified client can report anything here, including items it never got or changed stats on items it has.
By default the server trusts these reports. With approval mode on, it holds every report (and every move) until your bundle approves it, so you can reject what looks wrong.
Whatever the player does, the items that can leave their inventory (to a container, a stash, another player) are always the server's copy. A client cannot move out something the server never accepted.
Items in short¶
An item is identified by an re.ItemRef. Its TweakDB record, a seed
and a unique counter. Inventory.GetItems lists an inventory's stacks as
re.InventoryEntry (the same three plus the quantity). entry.ToRef() gives
the ItemRef the other functions take.
- Stackable items (ammo, money, crafting materials and such) have seed 2, and every inventory holds one stack per record.
- Unique items (weapons, clothing, cyberware) each have their own identity and a quantity of 1. The seed decides their rolls, so two Lexingtons with different seeds are different items.
Which records are stackable is taken directly from the games TweakDB records, which the server ships, but stats are rolled by the first client interacting with the item and require validation by bundle authors.
To see everything about an item (loot stats, stat modifiers, installed mods)
use Inventory.GetItem, which returns an re.ItemInfo. To store an item and
restore it exactly, use Inventory.GetItemData and Inventory.AddItemData.
Quick start¶
Give every player a pistol and some ammo when they join, and print what they carry when they leave:
re.Events.On(
"PlayerInventoryCreated",
(re.PlayerInventoryCreated x) =>
{
re.Inventory.AddItem(x.Inventory, new TDBID("Items.Preset_Lexington_Default"), 1);
re.Inventory.AddItem(x.Inventory, new TDBID("Ammo.HandgunAmmo"), 120);
}
);
re.Events.On(
"ClientDisconnecting",
(re.ClientDisconnecting x) =>
{
var inventory = re.Inventory.GetPlayerInventory(x.Id);
foreach (var entry in re.Inventory.GetItems(inventory))
{
Console.WriteLine($"{entry.Record.Get():X} x{entry.Quantity}");
}
}
);
The full list of functions is in the scripting API reference under re.Inventory.