BaseInventoryItemclass

Base for anything that can live in an `Sandbox.BaseInventoryComponent`. Handles the basics every item needs - a networked slot, editor metadata, and overridable equip/holster lifecycle - so deriving an item is almost no boilerplate. Usable as-is for a simple no-code pickup, or derive from it for weapons, tools, etc.

objectComponentBaseInventoryItem
Namespace
Sandbox
Assembly
Sandbox.Engine
Declaration
public class Sandbox.BaseInventoryItem : Sandbox.Component

Constructors1

Showing 1 constructors

Methods12

Showing 12 methods

public bool CanPickup(Sandbox.BaseInventoryComponent inventory)PUBLIC

True when the given inventory is allowed to pick this item up (see `Sandbox.BaseInventoryItem.OnCanPickup(Sandbox.BaseInventoryComponent)`). Queryable by UI - a pure check, nothing happens.

ParameterTypeDescription
inventoryBaseInventoryComponent
Returns:bool

public bool CanSwitchTo()PUBLIC

True when the inventory is allowed to make this the active item (see `Sandbox.BaseInventoryItem.OnCanSwitchTo`). Queryable by UI - a pure check, nothing happens.

Returns:bool

protected virtual void OnAdded(Sandbox.BaseInventoryComponent inventory)PROTECTEDVIRTUAL

Called on the host when added to an inventory. Base does nothing.

ParameterTypeDescription
inventoryBaseInventoryComponent
Returns:void

protected virtual bool OnAdding(Sandbox.BaseInventoryComponent inventory)PROTECTEDVIRTUAL

About to be added to `inventory`. Return false to refuse - optionally consuming this item instead (a duplicate weapon donates its ammo, a stackable merges into its stack). Runs on the host for every add - pickups, loadouts, code. Base allows it.

ParameterTypeDescription
inventoryBaseInventoryComponent
Returns:bool

protected virtual bool OnCanPickup(Sandbox.BaseInventoryComponent inventory)PROTECTEDVIRTUAL

Can the given inventory pick this item up out of the world? Both the Use prompt and Touch pickup consult it, so a refused item shows no prompt - role-locked weapons, class restrictions, quest gates. Base allows it.

ParameterTypeDescription
inventoryBaseInventoryComponent
Returns:bool

protected virtual bool OnCanSwitchTo()PROTECTEDVIRTUAL

Return false to stop the inventory making this item active.

Returns:bool

protected virtual void OnControl()PROTECTEDVIRTUAL

Called each frame on the owning client while this is the active item - read input and drive the item's behaviour here (firing, reloading, aiming, etc). Pumped by the inventory; see `Sandbox.BaseInventoryComponent.Pump` and `Sandbox.BaseInventoryComponent.ManualPumping`. Base does nothing.

Returns:void

protected virtual bool OnDrop()PROTECTEDVIRTUAL

Places this item in the world after being dropped. The default unparents it (keeping its world position), enables it, drops network ownership, and gives it a small toss if it has a `Sandbox.Rigidbody`. Return false to refuse the drop (e.g. bound items). Override to customise placement or behaviour.

Returns:bool

protected virtual void OnEquipped()PROTECTEDVIRTUAL

Called on every peer when this becomes the active item. Base does nothing.

Returns:void

protected virtual void OnHolstered()PROTECTEDVIRTUAL

Called on every peer when this stops being the active item. Base does nothing.

Returns:void

protected virtual bool OnHolstering(Sandbox.BaseInventoryItem next)PROTECTEDVIRTUAL

The inventory is about to switch away from this (the active) item - to `next`, or to nothing when it's null. Return false to refuse and stay deployed (stash `next` to finish a put-down and re-issue the switch), or handle it here - e.g. cancel an in-progress reload - and return true to allow the switch. Not called on forced holsters (death, removal, dropping). Base allows it.

ParameterTypeDescription
nextBaseInventoryItem
Returns:bool

protected virtual void OnRemoved(Sandbox.BaseInventoryComponent inventory)PROTECTEDVIRTUAL

Called on the host when removed from an inventory. Base does nothing.

ParameterTypeDescription
inventoryBaseInventoryComponent
Returns:void

Properties9

Showing 9 properties

public string Sandbox.BaseInventoryItem.DisplayName { get; set; }PUBLICGETSET

Display name shown in inventory UI.

Returns:string

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

The inventory this item currently belongs to, or null if it isn't in one. Derived from the hierarchy so it's correct on every peer. The nearest ancestor, never this item's own GameObject - an item that is itself an inventory (a backpack) belongs to its holder.

public bool Sandbox.BaseInventoryItem.IsActive { get; set; }PUBLICGETSET

True when this is the active (deployed) item in its inventory.

Returns:bool

public int Sandbox.BaseInventoryItem.PreferredSlot { get; set; }PUBLICGETSET

The slot this item wants when added without an explicit one (GMod's SWEP.Slot). In a hotbar inventory it falls back to the first empty slot when taken, with -1 for no preference; in a buckets inventory the preference always wins.

Returns:int

public virtual bool Sandbox.BaseInventoryItem.ShouldAvoid { get; set; }PUBLICVIRTUALGETSET

True when the inventory should avoid auto-switching to this item - e.g. a gun with nothing to fire. `Sandbox.BaseInventoryComponent.GetBestItem` falls back to avoided items only when there's nothing better.

Returns:bool

public int Sandbox.BaseInventoryItem.Slot { get; set; }PUBLICGETSET

The slot this item occupies, or -1 if unassigned. Set by the inventory.

Returns:int

public int Sandbox.BaseInventoryItem.SlotOrder { get; set; }PUBLICGETSET

Fixed sort position among items sharing a slot, lowest first (GMod's SWEP.SlotPos). Only matters in a buckets inventory - hotbar slots hold one item.

Returns:int

public int Sandbox.BaseInventoryItem.Value { get; set; }PUBLICGETSET

Relative worth of this item. Used as the priority when the inventory has to pick a "best" item to fall back to (e.g. after the active item is removed). Higher wins.

Returns:int

On this page