CharacterInventory

Character component that manages item slot selection, item spawning/despawning, and carry interactions for a character's hotbar-style inventory.

Definition

Namespace: Paragon.Townskeep.CharacterSystem Assembly: Townskeep.dll

[Serializable]
public class CharacterInventory : CharacterComponent

Inheritance: ParagonComponent<Character>CharacterComponentCharacterInventory

Remarks

CharacterInventory bridges the Inventory data system with the character's physical world. It manages a hotbar/toolbar paradigm where:

  • The character has an Inventory with multiple slots.

  • One slot is active at a time (CurrentIndex).

  • Switching slots physically despawns the current item and spawns the new one.

  • Spawned items are automatically picked up via CarryInteraction through the character's Interactor.

The slot-switching logic handles the full lifecycle: cancel current carry interaction → despawn current item → update index → spawn new item at character position → initiate carry interaction.

Quick Lookup

Goal
How

Get current slot data

characterInventory.GetCurrentSlot()

Set item in current slot

characterInventory.SetCurrentSlot(item, amount)

Clear current slot

characterInventory.ClearCurrentSlot()

Cycle to next slot

characterInventory.SwitchNextSlot()

Cycle to previous slot

characterInventory.SwitchPreviousSlot()

Jump to specific slot

characterInventory.SwitchToSlot(index)

Access underlying inventory

characterInventory.Inventory

Get held item reference

characterInventory.CurrentItem

Properties

Inventory

The underlying Inventory data container.

CurrentItem

The currently spawned Item in the world (the physical representation of the active slot). null if the active slot is empty.

CurrentIndex

The index of the currently active slot.

Events

CurrentSelectionChanged

Fires when the active slot index changes.

Parameter
Type
Description

index

int

The new active slot index

Methods

SetCurrentSlot

Sets the item and amount in the currently active slot.

Parameter
Type
Description

item

Item

The item to place in the slot

amount

int

Stack count (default: 1)

GetCurrentSlot

Returns the InventorySlot data for the currently active slot.

ClearCurrentSlot

Clears the current slot and nullifies the CurrentItem reference.

SwitchNextSlot

Cycles to the next slot (wraps around).

SwitchPreviousSlot

Cycles to the previous slot (wraps around).

SwitchToSlot

Switches to a specific slot index. Handles the full despawn → spawn → carry lifecycle.

Parameter
Type
Description

index

int

Target slot index

Slot switch flow:

  1. If holding an item with an active CarryInteraction, cancel the interaction (without despawn animation) and despawn the item.

  2. Update currentIndex and fire CurrentSelectionChanged.

  3. If the new slot has a valid ItemID, spawn the item via ItemSpawner, position it at the character, sync physics, and start a CarryInteraction.

Lifecycle

Phase
Behavior

OnInitialize()

Caches CharacterInteractor reference, sets index to 0, initializes the Inventory

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

See Also

Last updated