Replication & Multiplayerr
Overview
InventoryComponent is fully replicated for multiplayer games.
Replicated Properties
- Items array: All inventory slots
- Max Slots: Capacity
- Max Weight: Weight limit
- Current Weight: Total weight
Server Authority
All inventory modifications go through the server:- Client calls
AddItem() - Server validates and adds item
- Server replicates change to all clients
- Clients receive updated inventory
RPC Functions
Server RPCs for authority:Client Prediction
For responsive gameplay, implement client-side prediction:Best Practices
- Use AddItem() for simplicity: When you just need to add items by type
- Use AddItemInstance() for custom items: When items have unique properties
- Handle overflow: Always check for overflow when adding items
- Validate before removing: Check if items exist before removing
- Use events for UI updates: Bind to OnSlotUpdated for reactive UI
- Enable auto-sort for convenience: Makes inventory management easier
- Set appropriate weight limits: Balance realism with gameplay
- Test multiplayer: Ensure all operations work with server authority
- Use container restrictions: Prevent players from breaking game logic
- Implement weight penalties: Make encumbrance meaningful
Common Patterns
Safe Item Addition
Transferring with Validation
Consuming Ingredients
Performance Considerations
- Slot count: More slots = more memory and replication cost
- Weight calculations: Cached and updated on changes only
- Sorting: O(n log n) operation - don’t sort every frame
- Replication: Only changed slots replicate (not entire inventory)
- UI updates: Use events to update only changed slots