-
Notifications
You must be signed in to change notification settings - Fork 0
Inventory System
The Inventory System is one of the core gameplay systems in Adventure-Game. It is responsible for storing, organizing, and managing every item the player collects during gameplay.
Unlike a fixed-size inventory commonly found in simple console applications, Adventure-Game uses a dynamically resizable inventory, allowing players to collect an unlimited number of items without predefined storage limits. The inventory is designed to work with the project's object-oriented architecture by storing pointers to the abstract Item class, enabling it to manage multiple item types through a single interface.
This page explains how the inventory works, how it interacts with other systems, and how it is implemented internally.
- Overview
- Responsibilities
- Inventory Architecture
- Dynamic Storage
- Supported Item Types
- Core Functions
- Inventory Operations
- Memory Management
- Relationships with Other Systems
- Performance
- Future Improvements
- Summary
The inventory acts as the player's personal storage.
Every weapon, armor piece, and potion obtained during gameplay is stored inside the inventory until it is used, equipped, or removed.
The inventory system is responsible for:
- Storing collected items
- Managing dynamic storage
- Displaying item information
- Adding new items
- Removing existing items
- Assigning unique item IDs
- Supporting equipment management
Because every collectible object passes through the inventory, it serves as the primary connection between the player and the game's item system.
The MyInventory class has several responsibilities.
The inventory stores every collectible item owned by the player.
Examples include:
- Weapons
- Armor
- Potions
Players can:
- View items
- Add new items
- Remove unwanted items
- Equip weapons
- Equip armor
- Use consumable items
The inventory automatically expands when additional storage is required.
Players never need to manually increase inventory capacity.
Every stored item receives a unique sequential identifier.
This makes it easier to:
- Display inventory contents
- Remove items
- Locate specific objects
- Perform inventory operations
The inventory is implemented as its own independent class.
classDiagram
Character --> MyInventory
MyInventory --> Item
Item <|-- Weapon
Item <|-- Armory
Item <|-- Potion
The Character owns an inventory, while the inventory owns the items stored inside it.
This separation allows each system to focus on its own responsibilities.
Unlike a fixed-size array, Adventure-Game uses dynamic memory allocation to expand inventory capacity as needed.
When the current storage becomes full:
- A larger array is allocated.
- Existing items are copied.
- The old array is released.
- The new array becomes the active inventory.
This process happens automatically and is invisible to the player.
Inventory Full
│
▼
Allocate Larger Array
│
▼
Copy Existing Items
│
▼
Delete Old Array
│
▼
Continue Gameplay
This approach removes the need for a hard inventory limit while demonstrating dynamic memory management in C++.
Because every collectible object inherits from the abstract Item class, the inventory can store different object types in the same collection.
Current supported items include:
| Item Type | Description |
|---|---|
| Weapon | Offensive equipment that increases attack power. |
| Armory | Defensive equipment that improves defense. |
| Potion | Consumable item that modifies player statistics. |
Future versions may expand the inventory with additional item categories such as quest items, crafting materials, accessories, or magic artifacts.
The inventory provides several core operations.
Displays every stored item along with detailed information such as:
- Item ID
- Name
- Type
- Statistics
- Equipment status
Adds a new item to the inventory.
If the inventory reaches its capacity, storage is automatically expanded before inserting the new item.
Removes an item using its unique identifier.
After removal, remaining items are shifted to maintain a continuous collection.
Increases inventory capacity by allocating a larger block of memory and copying existing items.
This operation is performed automatically whenever necessary.
Removes every stored item and resets the inventory to its initial state.
The following diagram illustrates the typical lifecycle of an item inside the inventory.
flowchart TD
Chest --> AddItem
Potion --> AddItem
Weapon --> AddItem
Armory --> AddItem
AddItem --> Inventory
Inventory --> Display
Inventory --> Equip
Inventory --> Use
Inventory --> Delete
Equip --> Character
Use --> Character
Delete --> RemoveItem
Items may remain in the inventory until they are equipped, consumed, or discarded.
The inventory is responsible for managing the objects it stores.
Character
│
▼
MyInventory
│
▼
Item*
├── Weapon
├── Armory
└── Potion
This ownership model ensures that all collected items are managed through a single centralized system.
The inventory demonstrates manual dynamic memory management using pointers.
Current implementation includes:
- Dynamic allocation
- Pointer storage
- Automatic resizing
- Manual cleanup
Because the project is intended as a learning exercise, it showcases how dynamic arrays work internally without relying on higher-level containers.
In future versions, the inventory could be modernized using:
std::vectorstd::unique_ptrstd::shared_ptr
These alternatives would simplify ownership and improve memory safety while preserving the overall architecture.
The inventory communicates with several gameplay systems.
flowchart LR
Character --> Inventory
Inventory --> Weapon
Inventory --> Armory
Inventory --> Potion
Chest --> Inventory
Potion --> Character
Weapon --> Character
Armory --> Character
The inventory acts as the bridge between the Character and every collectible object in the game.
Most inventory operations are efficient enough for the current project.
| Operation | Complexity |
|---|---|
| Add Item | O(1)* |
| Remove Item | O(n) |
| Search by ID | O(n) |
| Display Inventory | O(n) |
| Resize Inventory | O(n) |
Note: Adding an item is typically O(1), but becomes O(n) when the inventory must be resized because all existing items need to be copied into a larger array.
Given the relatively small number of items expected in gameplay, this implementation provides good performance while remaining easy to understand.
Several architectural decisions influenced the inventory implementation.
Using an abstract base class allows the inventory to store different object types in a single collection while treating them through a common interface.
Dynamic resizing removes arbitrary inventory limits and demonstrates an important C++ programming concept: manual memory management.
Keeping the inventory in its own class improves modularity and keeps the Character class focused on player-related data rather than storage management.
The inventory system has been designed with expansion in mind.
Possible future features include:
- Item sorting
- Filtering by category
- Search functionality
- Stackable consumables
- Weight or capacity system
- Favorite items
- Equipment comparison
- Item rarity colors
- Item durability display improvements
- Drag-and-drop interface (GUI version)
- Automatic item organization
Because the inventory is implemented as an independent module, these features can be added with minimal changes to the rest of the project.
The Inventory System is the central storage component of Adventure-Game. It manages every collectible object, supports dynamic memory allocation, and provides a clean interface for interacting with weapons, armor, and consumable items.
Its modular architecture, automatic resizing, and object-oriented design make it both an effective gameplay system and a practical demonstration of C++ programming concepts such as dynamic memory management, inheritance, and encapsulation.