InventoryController - jimdroberts/FishMMO GitHub Wiki
Controls the character's inventory slots, handling item activation, slot manipulation, and network synchronization. Manages client-server broadcasts for inventory changes and slot management.
-
public override void OnAwake()
Initializes the inventory controller and adds 32 slots for items.
-
public override void ResetState(bool asServer)
Resets the state of the inventory controller, clearing all items and calling base reset logic. Parameters: - bool asServer: True if called on the server.
-
public override void OnStartCharacter()
Called when the character starts. Registers client broadcast handlers for inventory operations if the local player owns this inventory. Disables the controller for non-owners.
-
public override void OnStopCharacter()
Called when the character stops. Unregisters client broadcast handlers for inventory operations if the local player owns this inventory.
-
private void OnClientInventorySetItemBroadcastReceived(InventorySetItemBroadcast msg, Channel channel)
Handles a broadcast from the server to set a single inventory item. Updates the specified slot with the received item details. Parameters: - InventorySetItemBroadcast msg: The broadcast message containing item data. - Channel channel: The network channel used for the broadcast.
-
private void OnClientInventorySetMultipleItemsBroadcastReceived(InventorySetMultipleItemsBroadcast msg, Channel channel)
Handles a broadcast from the server to set multiple inventory items. Updates each specified slot with the received item details. Parameters: - InventorySetMultipleItemsBroadcast msg: The broadcast message containing multiple items. - Channel channel: The network channel used for the broadcast.
-
private void OnClientInventoryRemoveItemBroadcastReceived(InventoryRemoveItemBroadcast msg, Channel channel)
Handles a broadcast from the server to remove an item from an inventory slot. Removes the item from the specified slot with server authority. Parameters: - InventoryRemoveItemBroadcast msg: The broadcast message containing the slot to remove. - Channel channel: The network channel used for the broadcast.
-
private void OnClientInventorySwapItemSlotsBroadcastReceived(InventorySwapItemSlotsBroadcast msg, Channel channel)
Handles a broadcast from the server to swap item slots in the inventory or between inventories. Performs the swap operation based on the source inventory type. Parameters: - InventorySwapItemSlotsBroadcast msg: The broadcast message containing swap details. - Channel channel: The network channel used for the broadcast.
-
public override bool CanManipulate()
Determines if the inventory can be manipulated (e.g., items moved or swapped). Always returns true unless base logic restricts manipulation. Returns: - bool: True if manipulation is allowed, false otherwise.
-
public void Activate(int index)
Activates the item in the specified inventory slot, typically triggering its use effect. Only activates if the character is alive and the item exists in the slot. Parameters: - int index: The inventory slot index to activate.
-
public bool CanSwapItemSlots(int from, int to, InventoryType fromInventory)
Determines if two item slots can be swapped, preventing swaps within the same inventory slot. Parameters: - int from: The source slot index. - int to: The destination slot index. - InventoryType fromInventory: The inventory type of the source slot. Returns: - bool: True if the slots can be swapped, false otherwise.
- Ensure the character has an InventoryController attached and initialized.
- Register the necessary network broadcasts for inventory operations.
- Configure inventory slots and item templates as needed in the Inspector or via script.
- Initialize the controller during character startup.
// Example 1: Activating an item in an inventory slot
// Assume 'inventoryController' is a valid InventoryController instance.
inventoryController.Activate(0); // Activates the item in slot 0
// Example 2: Swapping two inventory slots
// Assume 'inventoryController' is a valid InventoryController instance.
bool canSwap = inventoryController.CanSwapItemSlots(0, 1, InventoryType.Inventory);
if (canSwap) {
// Perform swap logic
}
- Always check CanManipulate() before performing inventory operations.
- Register and unregister network broadcasts appropriately to avoid memory leaks or unexpected behavior.
- Validate slot indices and inventory types before swapping or activating items.
- Use clear and descriptive logging for inventory actions to aid debugging.
- Handle null checks for items and containers to prevent runtime exceptions.