Remarks

This is deliberately animation agnostic and knows nothing about players. Driving hold-type animations belongs in layers built on top of this - see BaseWeapon.

build Constructors 1

data_object Properties 13

ActiveItem
BaseInventoryItem
The item that is currently active (deployed), or null when nothing is held. Setting this is host authoritative - use Sandbox.InventoryComponent.Switch(Sandbox.BaseInventoryItem,System.Boolean).
AutoSwitchOnEmpty
bool
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.
AutoSwitchOnPickup
bool
Switch to a picked up item when it's better than the active one (see Sandbox.InventoryComponent.ShouldAutoSwitchTo(Sandbox.BaseInventoryItem)). Empty hands always deploy the pickup.
Behaviour
InventoryBehaviour
How this inventory assigns its slots - exclusive hotbar slots or shared buckets.
GiveOnStart
bool
Grant the loadout automatically when the inventory starts. Turn off to decide when yourself (respawn logic, saved loadouts) and call Sandbox.InventoryComponent.GiveLoadout.
Items
IEnumerable`1
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.
ManualPumping
bool
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.InventoryComponent.Pump from wherever you want (e.g. alongside your player's own input handling).
MaxSlots
int
How many slots this inventory has. Items occupy slots 0..MaxSlots-1.
PickupMode
PickupBehaviour
How this inventory takes items lying in the world - dropped, or placed in the scene.
PickupRadius
float
How close a world item has to be for Touch pickup, from the inventory's origin.
StartingAmmo
List`1
Reserve ammo granted by the loadout.
StartingItems
List`1
Item prefabs granted by the loadout, in order. Each needs a Sandbox.BaseInventoryItem.
UsesLoadout
bool
Does this inventory start with a loadout?

functions Methods 33

Add (item, slot)
bool
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.
CanPickupWorldItem (item)
bool
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.InventoryComponent)), 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.
Drop (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.
FindEmptySlot ()
int
Returns the first empty slot index, or -1 if the inventory is full.
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.
GetAmmo (type)
int
How much reserve ammo of the given type this inventory holds. Null is 0.
GetBestItem ()
BaseInventoryItem
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.
GetItem ()
T
Returns the first item of the given type in the inventory (lowest slot wins), or null.
GetSlot (slot)
BaseInventoryItem
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.
GetSlotItems (slot)
IEnumerable`1
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.
GiveAmmo (type, amount)
int
Add reserve ammo of the given type, clamped to the type's Sandbox.AmmoResource.MaxReserve. Returns how much was actually added. Call this from host game logic (pickups) - it's authoritative on the host.
GiveLoadout ()
Grants the starting loadout - picks up every Sandbox.InventoryComponent.StartingItems prefab, grants the Sandbox.InventoryComponent.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 (type, amount)
bool
Does this inventory hold at least reserve ammo of the given type?
HasItem ()
bool
Returns whether the inventory contains an item of the given type.
MoveSlot (fromSlot, toSlot)
Moves the item in to , swapping if the destination is occupied. Routed through the host.
OnAdding (item, slot)
bool
An item is about to be added to . Return false to refuse.
OnDropping (item)
bool
An item is about to be dropped into the world. Return false to refuse.
OnFixedUpdate ()
OnItemAdded (item)
An item was added to the inventory.
OnMovingSlot (fromSlot, toSlot)
bool
The items in these slots are about to move/swap. Return false to refuse.
OnRemoving (item)
bool
An item is about to be removed and destroyed. Return false to refuse.
OnStart ()
OnUpdate ()
Pickup (prefab, slot)
BaseInventoryItem
Spawns an item from a prefab and adds it to the inventory in the given slot (-1 picks one, see Sandbox.InventoryComponent.Add(Sandbox.BaseInventoryItem,System.Int32)). Host only. Returns the spawned item, or null if it couldn't be added (no room, no Sandbox.BaseInventoryItem on the prefab, slot taken).
PickupWorldItem (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.InventoryComponent.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).
Pump ()
Drives the active item's per-frame control hook. The inventory calls this itself every frame unless Sandbox.InventoryComponent.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 (item)
Removes an item from the inventory and destroys it, then switches to the best remaining item. Routed through the host.
SetAmmo (type, 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.
ShouldAutoSwitchTo (item)
bool
Should picking this item up make it active? Base: Sandbox.InventoryComponent.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.
Switch (item, allowHolster)
Makes the given item active, holstering whatever was active. Pass null with to holster everything. Routed through the host.
SwitchToBest ()
Switches to the best available item (see Sandbox.InventoryComponent.GetBestItem).
TakeAmmo (type, amount)
int
Take up to 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 (item, to, slot)
bool
Moves an item from this inventory into another - no world drop, no destroy. The usual gates get a say (Sandbox.InventoryComponent.OnRemoving(Sandbox.BaseInventoryItem) here, Sandbox.InventoryComponent.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.

Inheritance

people
Log in to reply
You can't reply if you're not logged in. That would be crazy.