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.

objectComponentBaseInventoryComponent
Namespace
Sandbox
Assembly
Sandbox.Engine
Declaration
public class Sandbox.BaseInventoryComponent : Sandbox.Component

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

Methods34

Showing 34 methods

public bool Add(Sandbox.BaseInventoryItem item, int slot = -1)PUBLIC

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.

ParameterTypeDescription
itemBaseInventoryItem
slot = -1int
Returns:bool

protected virtual bool CanPickupWorldItem(Sandbox.BaseInventoryItem item)PROTECTEDVIRTUAL

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.

ParameterTypeDescription
itemBaseInventoryItem
Returns:bool

public void Drop(Sandbox.BaseInventoryItem item)PUBLIC

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.

ParameterTypeDescription
itemBaseInventoryItem
Returns:void

public int FindEmptySlot()PUBLIC

Returns the first empty slot index, or -1 if the inventory is full.

Returns:int

public void ForceHolster()PUBLIC

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.

Returns:void

public int GetAmmo(Sandbox.BaseAmmoResource type)PUBLIC

How much reserve ammo of the given type this inventory holds. Null is 0.

ParameterTypeDescription
typeBaseAmmoResource
Returns:int

public virtual Sandbox.BaseInventoryItem GetBestItem()PUBLICVIRTUAL

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()PUBLIC

Returns the first item of the given type in the inventory (lowest slot wins), or null.

Returns:T

public Sandbox.BaseInventoryItem GetSlot(int slot)PUBLIC

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.

ParameterTypeDescription
slotint

public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> GetSlotItems(int slot)PUBLIC

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.

ParameterTypeDescription
slotint
Returns:IEnumerable<BaseInventoryItem>

public int GiveAmmo(Sandbox.BaseAmmoResource type, int amount)PUBLIC

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.

ParameterTypeDescription
typeBaseAmmoResource
amountint
Returns:int

public void GiveLoadout()PUBLIC

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.

Returns:void

public bool HasAmmo(Sandbox.BaseAmmoResource type, int amount = 1)PUBLIC

Does this inventory hold at least `amount` reserve ammo of the given type?

ParameterTypeDescription
typeBaseAmmoResource
amount = 1int
Returns:bool

public bool HasItem()PUBLIC

Returns whether the inventory contains an item of the given type.

Returns:bool

public void MoveSlot(int fromSlot, int toSlot)PUBLIC

Moves the item in `fromSlot` to `toSlot`, swapping if the destination is occupied. Routed through the host.

ParameterTypeDescription
fromSlotint
toSlotint
Returns:void

protected virtual bool OnAdding(Sandbox.BaseInventoryItem item, int slot)PROTECTEDVIRTUAL

An item is about to be added to `slot`. Return false to refuse.

ParameterTypeDescription
itemBaseInventoryItem
slotint
Returns:bool

protected virtual bool OnDropping(Sandbox.BaseInventoryItem item)PROTECTEDVIRTUAL

An item is about to be dropped into the world. Return false to refuse.

ParameterTypeDescription
itemBaseInventoryItem
Returns:bool

protected virtual void OnFixedUpdate()PROTECTEDVIRTUALFIXED TICK

Returns:void

protected virtual void OnItemAdded(Sandbox.BaseInventoryItem item)PROTECTEDVIRTUAL

An item was added to the inventory.

ParameterTypeDescription
itemBaseInventoryItem
Returns:void

protected virtual bool OnMovingSlot(int fromSlot, int toSlot)PROTECTEDVIRTUAL

The items in these slots are about to move/swap. Return false to refuse.

ParameterTypeDescription
fromSlotint
toSlotint
Returns:bool

protected virtual bool OnRemoving(Sandbox.BaseInventoryItem item)PROTECTEDVIRTUAL

An item is about to be removed and destroyed. Return false to refuse.

ParameterTypeDescription
itemBaseInventoryItem
Returns:bool

protected virtual void OnStart()PROTECTEDVIRTUALLIFECYCLE

Returns:void

protected virtual void OnUpdate()PROTECTEDVIRTUALPER-FRAME

Returns:void

public virtual void PickupWorldItem(Sandbox.BaseInventoryItem item)PUBLICVIRTUAL

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).

ParameterTypeDescription
itemBaseInventoryItem
Returns:void

public void Pump()PUBLICPER-FRAME

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.

Returns:void

public void Remove(Sandbox.BaseInventoryItem item)PUBLIC

Removes an item from the inventory and destroys it, then switches to the best remaining item. Routed through the host.

ParameterTypeDescription
itemBaseInventoryItem
Returns:void

public void SetAmmo(Sandbox.BaseAmmoResource type, int amount)PUBLIC

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.

ParameterTypeDescription
typeBaseAmmoResource
amountint
Returns:void

protected virtual bool ShouldAutoSwitchTo(Sandbox.BaseInventoryItem item)PROTECTEDVIRTUAL

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.

ParameterTypeDescription
itemBaseInventoryItem
Returns:bool

public void Switch(Sandbox.BaseInventoryItem item, bool allowHolster = False)PUBLIC

Makes the given item active, holstering whatever was active. Pass null with `allowHolster` to holster everything. Routed through the host.

ParameterTypeDescription
itemBaseInventoryItem
allowHolster = Falsebool
Returns:void

public void SwitchToBest()PUBLIC

Switches to the best available item (see `Sandbox.BaseInventoryComponent.GetBestItem`).

Returns:void

public int TakeAmmo(Sandbox.BaseAmmoResource type, int amount)PUBLIC

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.

ParameterTypeDescription
typeBaseAmmoResource
amountint
Returns:int

public bool Transfer(Sandbox.BaseInventoryItem item, Sandbox.BaseInventoryComponent to, int slot = -1)PUBLIC

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.

ParameterTypeDescription
itemBaseInventoryItem
toBaseInventoryComponent
slot = -1int
Returns:bool

Properties13

Showing 13 properties

public Sandbox.BaseInventoryItem Sandbox.BaseInventoryComponent.ActiveItem { get; set; }PUBLICGETSET

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; }PUBLICGETSET

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.

Returns:bool

public bool Sandbox.BaseInventoryComponent.AutoSwitchOnPickup { get; set; }PUBLICGETSET

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.

Returns:bool

public bool Sandbox.BaseInventoryComponent.GiveOnStart { get; set; }PUBLICGETSET

Grant the loadout automatically when the inventory starts. Turn off to decide when yourself (respawn logic, saved loadouts) and call `Sandbox.BaseInventoryComponent.GiveLoadout`.

Returns:bool

public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> Sandbox.BaseInventoryComponent.Items { get; set; }PUBLICGETSET

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.

Returns:IEnumerable<BaseInventoryItem>

public bool Sandbox.BaseInventoryComponent.ManualPumping { get; set; }PUBLICGETSET

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).

Returns:bool

public int Sandbox.BaseInventoryComponent.MaxSlots { get; set; }PUBLICGETSET

How many slots this inventory has. Items occupy slots 0..MaxSlots-1.

Returns:int

public float Sandbox.BaseInventoryComponent.PickupRadius { get; set; }PUBLICGETSET

How close a world item has to be for Touch pickup, from the inventory's origin.

Returns:float

public System.Collections.Generic.List`1<Sandbox.GameObject> Sandbox.BaseInventoryComponent.StartingItems { get; set; }PUBLICGETSET

Item prefabs granted by the loadout, in order. Each needs a `Sandbox.BaseInventoryItem`.

Returns:List<GameObject>

public bool Sandbox.BaseInventoryComponent.UsesLoadout { get; set; }PUBLICGETSET

Does this inventory start with a loadout?

Returns:bool

On this page

Constructorspublic BaseInventoryComponent()Methodspublic System.Boolean Add(Sandbox.BaseInventoryItem item, System.Int32 slot = -1)protected virtual System.Boolean CanPickupWorldItem(Sandbox.BaseInventoryItem item)public System.Void Drop(Sandbox.BaseInventoryItem item)public System.Int32 FindEmptySlot()public System.Void ForceHolster()public System.Int32 GetAmmo(Sandbox.BaseAmmoResource type)public virtual Sandbox.BaseInventoryItem GetBestItem()public T GetItem()public Sandbox.BaseInventoryItem GetSlot(System.Int32 slot)public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> GetSlotItems(System.Int32 slot)public System.Int32 GiveAmmo(Sandbox.BaseAmmoResource type, System.Int32 amount)public System.Void GiveLoadout()public System.Boolean HasAmmo(Sandbox.BaseAmmoResource type, System.Int32 amount = 1)public System.Boolean HasItem()public System.Void MoveSlot(System.Int32 fromSlot, System.Int32 toSlot)protected virtual System.Boolean OnAdding(Sandbox.BaseInventoryItem item, System.Int32 slot)protected virtual System.Boolean OnDropping(Sandbox.BaseInventoryItem item)protected virtual System.Void OnFixedUpdate()protected virtual System.Void OnItemAdded(Sandbox.BaseInventoryItem item)protected virtual System.Boolean OnMovingSlot(System.Int32 fromSlot, System.Int32 toSlot)protected virtual System.Boolean OnRemoving(Sandbox.BaseInventoryItem item)protected virtual System.Void OnStart()protected virtual System.Void OnUpdate()public Sandbox.BaseInventoryItem Pickup(Sandbox.GameObject prefab, System.Int32 slot = -1)public Sandbox.BaseInventoryItem Pickup(System.String prefabPath, System.Int32 slot = -1)public virtual System.Void PickupWorldItem(Sandbox.BaseInventoryItem item)public System.Void Pump()public System.Void Remove(Sandbox.BaseInventoryItem item)public System.Void SetAmmo(Sandbox.BaseAmmoResource type, System.Int32 amount)protected virtual System.Boolean ShouldAutoSwitchTo(Sandbox.BaseInventoryItem item)public System.Void Switch(Sandbox.BaseInventoryItem item, System.Boolean allowHolster = False)public System.Void SwitchToBest()public System.Int32 TakeAmmo(Sandbox.BaseAmmoResource type, System.Int32 amount)public System.Boolean Transfer(Sandbox.BaseInventoryItem item, Sandbox.BaseInventoryComponent to, System.Int32 slot = -1)Propertiespublic Sandbox.BaseInventoryItem Sandbox.BaseInventoryComponent.ActiveItem { get; set; }public System.Boolean Sandbox.BaseInventoryComponent.AutoSwitchOnEmpty { get; set; }public System.Boolean Sandbox.BaseInventoryComponent.AutoSwitchOnPickup { get; set; }public Sandbox.BaseInventoryComponent.InventoryBehaviour Sandbox.BaseInventoryComponent.Behaviour { get; set; }public System.Boolean Sandbox.BaseInventoryComponent.GiveOnStart { get; set; }public System.Collections.Generic.IEnumerable`1<Sandbox.BaseInventoryItem> Sandbox.BaseInventoryComponent.Items { get; set; }public System.Boolean Sandbox.BaseInventoryComponent.ManualPumping { get; set; }public System.Int32 Sandbox.BaseInventoryComponent.MaxSlots { get; set; }public Sandbox.BaseInventoryComponent.PickupBehaviour Sandbox.BaseInventoryComponent.PickupMode { get; set; }public System.Single Sandbox.BaseInventoryComponent.PickupRadius { get; set; }public System.Collections.Generic.List`1<Sandbox.BaseInventoryComponent.AmmoGrant> Sandbox.BaseInventoryComponent.StartingAmmo { get; set; }public System.Collections.Generic.List`1<Sandbox.GameObject> Sandbox.BaseInventoryComponent.StartingItems { get; set; }public System.Boolean Sandbox.BaseInventoryComponent.UsesLoadout { get; set; }Metadata