# Sandbox.BaseInventoryComponent

A slot based inventory of [Sandbox.BaseInventoryItem](/api/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.

## Remarks

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](/api/Sandbox.BaseCombatWeapon).

- Kind: class
- Namespace: `Sandbox`
- Assembly: `Sandbox.Engine`
- Inherits: [Sandbox.Component](/api/Sandbox.Component)

## Constructors

- [`BaseInventoryComponent`](/api/Sandbox.BaseInventoryComponent/.ctor)

## Properties

- [`ActiveItem`](/api/Sandbox.BaseInventoryComponent/ActiveItem): The item that is currently active (deployed), or null when nothing is held. Setting this is host authoritative - use [Sandbox.BaseInventoryComponent.Switch](/api/Sandbox.BaseInventoryComponent/Switch).
- [`AutoSwitchOnEmpty`](/api/Sandbox.BaseInventoryComponent/AutoSwitchOnEmpty): Switch away from the active item when it's spent ([Sandbox.BaseInventoryItem.ShouldAvoid](/api/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.
- [`AutoSwitchOnPickup`](/api/Sandbox.BaseInventoryComponent/AutoSwitchOnPickup): Switch to a picked up item when it's better than the active one (see [Sandbox.BaseInventoryComponent.ShouldAutoSwitchTo](/api/Sandbox.BaseInventoryComponent/ShouldAutoSwitchTo)). Empty hands always deploy the pickup.
- [`Behaviour`](/api/Sandbox.BaseInventoryComponent/Behaviour): How this inventory assigns its slots - exclusive hotbar slots or shared buckets.
- [`GiveOnStart`](/api/Sandbox.BaseInventoryComponent/GiveOnStart): Grant the loadout automatically when the inventory starts. Turn off to decide when yourself (respawn logic, saved loadouts) and call [Sandbox.BaseInventoryComponent.GiveLoadout](/api/Sandbox.BaseInventoryComponent/GiveLoadout).
- [`Items`](/api/Sandbox.BaseInventoryComponent/Items): All items currently in the inventory, ordered by slot then [Sandbox.BaseInventoryItem.SlotOrder](/api/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.
- [`ManualPumping`](/api/Sandbox.BaseInventoryComponent/ManualPumping): 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](/api/Sandbox.BaseInventoryComponent/Pump) from wherever you want (e.g. alongside your player's own input handling).
- [`MaxSlots`](/api/Sandbox.BaseInventoryComponent/MaxSlots): How many slots this inventory has. Items occupy slots 0..MaxSlots-1.
- [`PickupMode`](/api/Sandbox.BaseInventoryComponent/PickupMode): How this inventory takes items lying in the world - dropped, or placed in the scene.
- [`PickupRadius`](/api/Sandbox.BaseInventoryComponent/PickupRadius): How close a world item has to be for Touch pickup, from the inventory's origin.
- [`StartingAmmo`](/api/Sandbox.BaseInventoryComponent/StartingAmmo): Reserve ammo granted by the loadout.
- [`StartingItems`](/api/Sandbox.BaseInventoryComponent/StartingItems): Item prefabs granted by the loadout, in order. Each needs a [Sandbox.BaseInventoryItem](/api/Sandbox.BaseInventoryItem).
- [`UsesLoadout`](/api/Sandbox.BaseInventoryComponent/UsesLoadout): Does this inventory start with a loadout?

## Methods

- [`Add`](/api/Sandbox.BaseInventoryComponent/Add): Adds an already-spawned item to the inventory. Slot -1 picks one: the item's [Sandbox.BaseInventoryItem.PreferredSlot](/api/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.
- [`Drop`](/api/Sandbox.BaseInventoryComponent/Drop): 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 `M:Sandbox.BaseInventoryItem.Drop`), then switches to the best remaining item. Routed through the host.
- [`FindEmptySlot`](/api/Sandbox.BaseInventoryComponent/FindEmptySlot): Returns the first empty slot index, or -1 if the inventory is full.
- [`ForceHolster`](/api/Sandbox.BaseInventoryComponent/ForceHolster): Holsters the active item without giving it a say ([Sandbox.BaseInventoryItem.OnHolstering](/api/Sandbox.BaseInventoryItem/OnHolstering) isn't consulted) - for host-decreed empty hands: arrests, vehicles, cutscenes. Host only.
- [`GetAmmo`](/api/Sandbox.BaseInventoryComponent/GetAmmo): How much reserve ammo of the given type this inventory holds. Null is 0.
- [`GetBestItem`](/api/Sandbox.BaseInventoryComponent/GetBestItem): Returns the highest [Sandbox.BaseInventoryItem.Value](/api/Sandbox.BaseInventoryItem/Value) item we're allowed to switch to, or null if there's nothing switchable. Items flagged [Sandbox.BaseInventoryItem.ShouldAvoid](/api/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.
- [`GetItem`](/api/Sandbox.BaseInventoryComponent/GetItem): Returns the first item of the given type in the inventory (lowest slot wins), or null.
- [`GetSlot`](/api/Sandbox.BaseInventoryComponent/GetSlot): 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](/api/Sandbox.BaseInventoryItem/SlotOrder) wins.
- [`GetSlotItems`](/api/Sandbox.BaseInventoryComponent/GetSlotItems): Returns every item in the given slot, ordered by [Sandbox.BaseInventoryItem.SlotOrder](/api/Sandbox.BaseInventoryItem/SlotOrder). One or none in a hotbar inventory; the bucket's contents in a buckets one.
- [`GiveAmmo`](/api/Sandbox.BaseInventoryComponent/GiveAmmo): Add reserve ammo of the given type, clamped to the type's [Sandbox.BaseAmmoResource.MaxReserve](/api/Sandbox.BaseAmmoResource/MaxReserve). Returns how much was actually added. Call this from host game logic (pickups) - it's authoritative on the host.
- [`GiveLoadout`](/api/Sandbox.BaseInventoryComponent/GiveLoadout): Grants the starting loadout - picks up every [Sandbox.BaseInventoryComponent.StartingItems](/api/Sandbox.BaseInventoryComponent/StartingItems) prefab, grants the [Sandbox.BaseInventoryComponent.StartingAmmo](/api/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.
- [`HasAmmo`](/api/Sandbox.BaseInventoryComponent/HasAmmo): Does this inventory hold at least `amount` reserve ammo of the given type?
- [`HasItem`](/api/Sandbox.BaseInventoryComponent/HasItem): Returns whether the inventory contains an item of the given type.
- [`MoveSlot`](/api/Sandbox.BaseInventoryComponent/MoveSlot): Moves the item in `fromSlot` to `toSlot`, swapping if the destination is occupied. Routed through the host.
- [`Pickup`](/api/Sandbox.BaseInventoryComponent/Pickup): Spawns an item from a prefab and adds it to the inventory in the given slot (-1 picks one, see [Sandbox.BaseInventoryComponent.Add](/api/Sandbox.BaseInventoryComponent/Add)). Host only. Returns the spawned item, or null if it couldn't be added (no room, no [Sandbox.BaseInventoryItem](/api/Sandbox.BaseInventoryItem) on the prefab, slot taken).
- [`PickupWorldItem`](/api/Sandbox.BaseInventoryComponent/PickupWorldItem): 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](/api/Sandbox.BaseInventoryComponent/CanPickupWorldItem) 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).
- [`Pump`](/api/Sandbox.BaseInventoryComponent/Pump): Drives the active item's per-frame control hook. The inventory calls this itself every frame unless [Sandbox.BaseInventoryComponent.ManualPumping](/api/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.
- [`Remove`](/api/Sandbox.BaseInventoryComponent/Remove): Removes an item from the inventory and destroys it, then switches to the best remaining item. Routed through the host.
- [`SetAmmo`](/api/Sandbox.BaseInventoryComponent/SetAmmo): 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.
- [`Switch`](/api/Sandbox.BaseInventoryComponent/Switch): Makes the given item active, holstering whatever was active. Pass null with `allowHolster` to holster everything. Routed through the host.
- [`SwitchToBest`](/api/Sandbox.BaseInventoryComponent/SwitchToBest): Switches to the best available item (see [Sandbox.BaseInventoryComponent.GetBestItem](/api/Sandbox.BaseInventoryComponent/GetBestItem)).
- [`TakeAmmo`](/api/Sandbox.BaseInventoryComponent/TakeAmmo): 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.
- [`Transfer`](/api/Sandbox.BaseInventoryComponent/Transfer): Moves an item from this inventory into another - no world drop, no destroy. The usual gates get a say ([Sandbox.BaseInventoryComponent.OnRemoving](/api/Sandbox.BaseInventoryComponent/OnRemoving) here, [Sandbox.BaseInventoryComponent.OnAdding](/api/Sandbox.BaseInventoryComponent/OnAdding) 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.
