π€ Character¶
Components that let a GameObject access and expose inventory containers β
players, NPCs, chests, vendors, or any other βinventory-holdingβ entity.
β Runtime-safe and authority-agnostic
β Works withSceneInventoryServiceout of the box
β Adds automatic cleanup when destroyed
Purpose¶
This folder provides the per-entity bridge between scene objects and the Inventory system.
It handles binding, lookup, and cleanup β while SceneInventoryService remains the actual owner
of all containers.
Character components never store inventory locally; they only expose containers managed by the service.
Contents¶
| Component | Purpose |
|---|---|
CharacterInventory |
Binds a specific container name (e.g. "Backpack") for this GameObject via the service. |
InventoryBinder |
Convenience component that binds a container to a UI or observer. |
InventoryOwnerHook |
Ensures the service clears containers when the owner is destroyed. |
π Quick Start¶
- Add
CharacterInventoryto your player prefab. - Defaults to a 24-slot
"Backpack"container (service-owned). -
Automatically adds
InventoryOwnerHook. -
(Optional) Add
CharacterEquipmentfor equippable gear. - (Optional) Add
InventoryBinderto wire UI or observer logic.
π CharacterInventory¶
Exposes one named container for a character (default: "Backpack").
The container is always created and owned by SceneInventoryService,
not by this component.
var cont = characterInventory.Container;
// Direct container access (authority bypass)
InvOpResult res = cont.TryAddAllResult(new ItemStack { def = potion, quantity = 3 });
cont.TryRemoveResult(potionGuid, 1);
int potions = cont.CountOf(potionGuid);
β οΈ Direct access bypasses authority.
Intended for local tools, UI, or inspectors.
UseSceneInventoryServicefor authoritative gameplay logic.
Events¶
OnChanged β fired when the bound containerβs contents change.
characterInventory.OnChanged += () => ui.Refresh();
Inspector Fields¶
| Field | Description |
|---|---|
backpackSize |
Initial slot count when no policy overrides it. |
inspectorContainer |
Container name to bind (case-insensitive; canonicalised). |
ownerGuid |
Stable ID for save/load systems (auto-generated). |
Binding Details¶
- Binding is lazy β resolved on first access or
OnEnable - If the service is unavailable, binding retries on the next frame
- Container names are canonicalised (trimmed + lowercase)
π InventoryBinder¶
Connects a container to UI or observer scripts.
public class MyUI : MonoBehaviour
{
public InventoryBinder binder;
void OnEnable()
{
// Binder auto-binds on enable
}
}
| Field | Description |
|---|---|
owner |
GameObject owning the container (defaults to Player-tag). |
containerName |
Case-insensitive container name. |
The binder subscribes to the containerβs OnChanged event and calls Refresh() internally.
πͺ InventoryOwnerHook¶
Ensures all containers for a GameObject are cleared when it is destroyed.
- Uses the active service if available
- Falls back to a cached service reference if teardown order changes (scene unload, playmode exit)
Automatically added by
CharacterInventory.
β οΈ Notes & Gotchas¶
CharacterInventorybinds one container per component- Service owns all containers β character components are views only
- Binding is lazy and retries safely
- Authority is enforced only by the service, never by character components
ownerGuidis auto-generated and stable for savesInventoryBindersilently no-ops if the owner cannot be resolved