Skip to content

Inventory System

Vladyslav Vytrykush edited this page Jul 14, 2026 · 1 revision

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.


Table of Contents


Overview

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.


Responsibilities

The MyInventory class has several responsibilities.

Item Storage

The inventory stores every collectible item owned by the player.

Examples include:

  • Weapons
  • Armor
  • Potions

Item Management

Players can:

  • View items
  • Add new items
  • Remove unwanted items
  • Equip weapons
  • Equip armor
  • Use consumable items

Capacity Management

The inventory automatically expands when additional storage is required.

Players never need to manually increase inventory capacity.


Item Identification

Every stored item receives a unique sequential identifier.

This makes it easier to:

  • Display inventory contents
  • Remove items
  • Locate specific objects
  • Perform inventory operations

Inventory Architecture

The inventory is implemented as its own independent class.

classDiagram

Character --> MyInventory

MyInventory --> Item

Item <|-- Weapon
Item <|-- Armory
Item <|-- Potion
Loading

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.


Dynamic Storage

Unlike a fixed-size array, Adventure-Game uses dynamic memory allocation to expand inventory capacity as needed.

When the current storage becomes full:

  1. A larger array is allocated.
  2. Existing items are copied.
  3. The old array is released.
  4. 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++.


Supported Item Types

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.


Core Functions

The inventory provides several core operations.

DisplayInventory()

Displays every stored item along with detailed information such as:

  • Item ID
  • Name
  • Type
  • Statistics
  • Equipment status

AddItem()

Adds a new item to the inventory.

If the inventory reaches its capacity, storage is automatically expanded before inserting the new item.


DelItem()

Removes an item using its unique identifier.

After removal, remaining items are shifted to maintain a continuous collection.


ResizeInventoryBig()

Increases inventory capacity by allocating a larger block of memory and copying existing items.

This operation is performed automatically whenever necessary.


Clear()

Removes every stored item and resets the inventory to its initial state.


Inventory Operations

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
Loading

Items may remain in the inventory until they are equipped, consumed, or discarded.


Inventory Ownership

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.


Memory Management

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::vector
  • std::unique_ptr
  • std::shared_ptr

These alternatives would simplify ownership and improve memory safety while preserving the overall architecture.


Relationships with Other Systems

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
Loading

The inventory acts as the bridge between the Character and every collectible object in the game.


Performance

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.


Design Decisions

Several architectural decisions influenced the inventory implementation.

Why Use an Abstract Item Class?

Using an abstract base class allows the inventory to store different object types in a single collection while treating them through a common interface.


Why Use Dynamic Resizing?

Dynamic resizing removes arbitrary inventory limits and demonstrates an important C++ programming concept: manual memory management.


Why Separate Inventory from Character?

Keeping the inventory in its own class improves modularity and keeps the Character class focused on player-related data rather than storage management.


Future Improvements

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.


Summary

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.

Clone this wiki locally