BaseInventoryComponentclass
A slot based inventory of `Sandbox.BaseInventoryItem`s, modelled on the sandbox game's inventory. Items are stored as child GameObjects; the inventory tracks which one is active and enables/disables them as you switch. Host authoritative - clients request changes and the host applies them, replicating the result back down.
Note
This is deliberately animation agnostic and knows nothing about players. Driving hold-type animations belongs in layers built on top of this - see `Sandbox.BaseCombatWeapon`.
Constructors1
Showing 1 constructors
public BaseInventoryComponent()
No results match this filter.
Methods34
Showing 34 methods
public bool Add(Sandbox.BaseInventoryItem item, int slot = -1)
Adds an already-spawned item to the inventory. Slot -1 picks one: the item's `Sandbox.BaseInventoryItem.PreferredSlot` when free, otherwise the first empty slot (buckets always take the preference). Host only. The item is reparented under this inventory and disabled until switched to.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
| slot = -1 | int | — |
bool—protected virtual bool CanPickupWorldItem(Sandbox.BaseInventoryItem item)
Is this an item lying in the world that we could take right now? It's valid, it isn't in an inventory, it doesn't refuse us (`Sandbox.BaseInventoryItem.OnCanPickup(Sandbox.BaseInventoryComponent)`), and it isn't something we dropped a moment ago. Runs on the host for routed pickup requests, making it the place to validate them - the base deliberately has no range check (like shot claims, what's plausible is game policy), so override to add range or line-of-sight rules.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
bool—public void Drop(Sandbox.BaseInventoryItem item)
Drops an item out of the inventory and into the world. Holsters it first if it's active, asks the item to place itself (see `Sandbox.BaseInventoryItem.Drop`), then switches to the best remaining item. Routed through the host.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
void—public int FindEmptySlot()
Returns the first empty slot index, or -1 if the inventory is full.
int—public void ForceHolster()
Holsters the active item without giving it a say (`Sandbox.BaseInventoryItem.OnHolstering(Sandbox.BaseInventoryItem)` isn't consulted) - for host-decreed empty hands: arrests, vehicles, cutscenes. Host only.
void—public int GetAmmo(Sandbox.BaseAmmoResource type)
How much reserve ammo of the given type this inventory holds. Null is 0.
| Parameter | Type | Description |
|---|---|---|
| type | BaseAmmoResource | — |
int—public virtual Sandbox.BaseInventoryItem GetBestItem()
Returns the highest `Sandbox.BaseInventoryItem.Value` item we're allowed to switch to, or null if there's nothing switchable. Items flagged `Sandbox.BaseInventoryItem.ShouldAvoid` (e.g. empty guns) are only picked when nothing better exists. Override for game-specific priority - runs on the host for engine-driven switches (remove, drop, pickup, loadout), so per-player preference data must be host-available.
public T GetItem()
Returns the first item of the given type in the inventory (lowest slot wins), or null.
T—public Sandbox.BaseInventoryItem GetSlot(int slot)
Returns the item in the given slot, or null if the slot is empty. When items share the slot (a buckets inventory), the lowest `Sandbox.BaseInventoryItem.SlotOrder` wins.
| Parameter | Type | Description |
|---|---|---|
| slot | int | — |
public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> GetSlotItems(int slot)
Returns every item in the given slot, ordered by `Sandbox.BaseInventoryItem.SlotOrder`. One or none in a hotbar inventory; the bucket's contents in a buckets one.
| Parameter | Type | Description |
|---|---|---|
| slot | int | — |
public int GiveAmmo(Sandbox.BaseAmmoResource type, int amount)
Add reserve ammo of the given type, clamped to the type's `Sandbox.BaseAmmoResource.MaxReserve`. Returns how much was actually added. Call this from host game logic (pickups) - it's authoritative on the host.
| Parameter | Type | Description |
|---|---|---|
| type | BaseAmmoResource | — |
| amount | int | — |
int—public void GiveLoadout()
Grants the starting loadout - picks up every `Sandbox.BaseInventoryComponent.StartingItems` prefab, grants the `Sandbox.BaseInventoryComponent.StartingAmmo`, then switches to the best item if nothing is active. Host only. Grants unconditionally - it's the caller's job to only ask once per life.
void—public bool HasAmmo(Sandbox.BaseAmmoResource type, int amount = 1)
Does this inventory hold at least `amount` reserve ammo of the given type?
| Parameter | Type | Description |
|---|---|---|
| type | BaseAmmoResource | — |
| amount = 1 | int | — |
bool—public bool HasItem()
Returns whether the inventory contains an item of the given type.
bool—public void MoveSlot(int fromSlot, int toSlot)
Moves the item in `fromSlot` to `toSlot`, swapping if the destination is occupied. Routed through the host.
| Parameter | Type | Description |
|---|---|---|
| fromSlot | int | — |
| toSlot | int | — |
void—protected virtual bool OnAdding(Sandbox.BaseInventoryItem item, int slot)
An item is about to be added to `slot`. Return false to refuse.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
| slot | int | — |
bool—protected virtual bool OnDropping(Sandbox.BaseInventoryItem item)
An item is about to be dropped into the world. Return false to refuse.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
bool—protected virtual void OnFixedUpdate()
void—protected virtual void OnItemAdded(Sandbox.BaseInventoryItem item)
An item was added to the inventory.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
void—protected virtual bool OnMovingSlot(int fromSlot, int toSlot)
The items in these slots are about to move/swap. Return false to refuse.
| Parameter | Type | Description |
|---|---|---|
| fromSlot | int | — |
| toSlot | int | — |
bool—protected virtual bool OnRemoving(Sandbox.BaseInventoryItem item)
An item is about to be removed and destroyed. Return false to refuse.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
bool—protected virtual void OnStart()
void—protected virtual void OnUpdate()
void—public virtual void PickupWorldItem(Sandbox.BaseInventoryItem item)
Take an item lying in the world into this inventory - the path Touch and Use pickup share, and the one to call from game code in None mode. Routed through the host. Refuses anything `Sandbox.BaseInventoryComponent.CanPickupWorldItem(Sandbox.BaseInventoryItem)` does. The usual add hooks apply, and the item becomes active when nothing else is. Override to change what a pickup does (ammo from duplicates, notices).
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
void—public void Pump()
Drives the active item's per-frame control hook. The inventory calls this itself every frame unless `Sandbox.BaseInventoryComponent.ManualPumping` is set, in which case you call it yourself. Only does anything on the client that owns the inventory - control is input, which is owner only.
void—public void Remove(Sandbox.BaseInventoryItem item)
Removes an item from the inventory and destroys it, then switches to the best remaining item. Routed through the host.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
void—public void SetAmmo(Sandbox.BaseAmmoResource type, int amount)
Set the reserve ammo of the given type to an exact value (clamped to zero). Ignores the type's max - the escape hatch for game logic that wants to exceed it.
| Parameter | Type | Description |
|---|---|---|
| type | BaseAmmoResource | — |
| amount | int | — |
void—protected virtual bool ShouldAutoSwitchTo(Sandbox.BaseInventoryItem item)
Should picking this item up make it active? Base: `Sandbox.BaseInventoryComponent.AutoSwitchOnPickup` is on, the item is better (higher Value) than the active one, and it isn't avoided (an empty gun). The active item can still refuse the holster. Override for game policy.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
bool—public void Switch(Sandbox.BaseInventoryItem item, bool allowHolster = False)
Makes the given item active, holstering whatever was active. Pass null with `allowHolster` to holster everything. Routed through the host.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
| allowHolster = False | bool | — |
void—public void SwitchToBest()
Switches to the best available item (see `Sandbox.BaseInventoryComponent.GetBestItem`).
void—public int TakeAmmo(Sandbox.BaseAmmoResource type, int amount)
Take up to `amount` reserve ammo of the given type, returning how much was actually removed. The owning client spends locally and the spend is mirrored to the host, whose pool is the truth.
| Parameter | Type | Description |
|---|---|---|
| type | BaseAmmoResource | — |
| amount | int | — |
int—public bool Transfer(Sandbox.BaseInventoryItem item, Sandbox.BaseInventoryComponent to, int slot = -1)
Moves an item from this inventory into another - no world drop, no destroy. The usual gates get a say (`Sandbox.BaseInventoryComponent.OnRemoving(Sandbox.BaseInventoryItem)` here, `Sandbox.BaseInventoryComponent.OnAdding(Sandbox.BaseInventoryItem,System.Int32)` and the item's own say there); any refusal leaves everything as it was. Reserve ammo stays behind - the pool lives on the inventory, not the item. The destination doesn't auto-deploy (it may be a chest). Host only - who may move items between which inventories is game policy, so games route their own requests here. Returns whether the item moved.
| Parameter | Type | Description |
|---|---|---|
| item | BaseInventoryItem | — |
| to | BaseInventoryComponent | — |
| slot = -1 | int | — |
bool—No results match this filter.
Properties13
Showing 13 properties
public Sandbox.BaseInventoryItem Sandbox.BaseInventoryComponent.ActiveItem { get; set; }
The item that is currently active (deployed), or null when nothing is held. Setting this is host authoritative - use `Sandbox.BaseInventoryComponent.Switch(Sandbox.BaseInventoryItem,System.Boolean)`.
public bool Sandbox.BaseInventoryComponent.AutoSwitchOnEmpty { get; set; }
Switch away from the active item when it's spent (`Sandbox.BaseInventoryItem.ShouldAvoid`) and something that isn't is available - the classic arena-shooter auto-switch. Voluntary, so the item can still refuse the holster.
bool—public bool Sandbox.BaseInventoryComponent.AutoSwitchOnPickup { get; set; }
Switch to a picked up item when it's better than the active one (see `Sandbox.BaseInventoryComponent.ShouldAutoSwitchTo(Sandbox.BaseInventoryItem)`). Empty hands always deploy the pickup.
bool—public Sandbox.BaseInventoryComponent.InventoryBehaviour Sandbox.BaseInventoryComponent.Behaviour { get; set; }
How this inventory assigns its slots - exclusive hotbar slots or shared buckets.
public bool Sandbox.BaseInventoryComponent.GiveOnStart { get; set; }
Grant the loadout automatically when the inventory starts. Turn off to decide when yourself (respawn logic, saved loadouts) and call `Sandbox.BaseInventoryComponent.GiveLoadout`.
bool—public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> Sandbox.BaseInventoryComponent.Items { get; set; }
All items currently in the inventory, ordered by slot then `Sandbox.BaseInventoryItem.SlotOrder`. Includes disabled (inactive) items but not ones waiting to be destroyed - a removed item is gone immediately, even though its GameObject lives until the end of the frame. Items inside a nested inventory (a held backpack) belong to that inventory, not this one.
public bool Sandbox.BaseInventoryComponent.ManualPumping { get; set; }
By default the inventory pumps the active item's control every frame in its own update. Turn this on to take over that timing yourself - the inventory will stop pumping and you call `Sandbox.BaseInventoryComponent.Pump` from wherever you want (e.g. alongside your player's own input handling).
bool—public int Sandbox.BaseInventoryComponent.MaxSlots { get; set; }
How many slots this inventory has. Items occupy slots 0..MaxSlots-1.
int—public Sandbox.BaseInventoryComponent.PickupBehaviour Sandbox.BaseInventoryComponent.PickupMode { get; set; }
How this inventory takes items lying in the world - dropped, or placed in the scene.
public float Sandbox.BaseInventoryComponent.PickupRadius { get; set; }
How close a world item has to be for Touch pickup, from the inventory's origin.
float—public System.Collections.Generic.List`1<Sandbox.BaseInventoryComponent.AmmoGrant> Sandbox.BaseInventoryComponent.StartingAmmo { get; set; }
Reserve ammo granted by the loadout.
public System.Collections.Generic.List`1<Sandbox.GameObject> Sandbox.BaseInventoryComponent.StartingItems { get; set; }
Item prefabs granted by the loadout, in order. Each needs a `Sandbox.BaseInventoryItem`.
public bool Sandbox.BaseInventoryComponent.UsesLoadout { get; set; }
Does this inventory start with a loadout?
bool—No results match this filter.