Hey guy I'm wondering if there is a tutorial or example code for a C++ RPG inventory system. I've surfed the website and I only discovered to make two classes, item and inventory and to use each of them for item's detail and holding the items in a linked list.

I'm hoping my long search for such a tutorial can end..

Thank you.

5 Years
Discussion Span
Last Post by Ketsuekiame

That may be because a game inventory system seems both too simple and too game-specific for a general purpose tutorial to be valuable. I imagine it might either be an std::vector of items or an std::map with items as keys and the amount of each item as values. It doesn't matter how you do it; I don't think there is a wrong way. Surely no game would have such a large inventory that it could be computationally expensive no matter what data structure you choose, but every game would probably have its own distinctive needs.

Perhaps if you explained more about your requirements and what exactly you are finding difficult. Are you having trouble with the UI that allows the player to interact with the inventory system? Are you having trouble populating the inventory system with items, such as reading items from a file generated by some sort of item-editing tool? Or is it something else entirely?

Edited by bguild


First of all I want to say thank you for replying. The program I'm trying to write is for a hw assignment. Unfortunately, I wasn't able to make it to a lecture that spoke specifically about this project. Anyways, I'm supposed to write a program that doesn't require UI for the player. Instead, it's simply printed but the professor wants to see the details of an inventory system.

This is what I have so far...

using namespace std;

int maxWeight;

class inventory {  //manages the entire inventory
    inventory::inventory(int defaultWeight = maxWeight);

    int maxWeight = 100;


class item {  //holds the details about a particular item
    item(char* name, double weight);    
    char* name;
    double weight;

The first steps are to create the "item: class with a constructor that will take in a char* name and a double weight but I don't understand how this will help in the later coding. I mean.. I understand that constructors are used to set a default value but i don't know how it would be used to describe the name of an item.

The next class I'm supposed to build is "inventory." This class is supposed to have a linked list that will carry all the items.

I have tried searching and learning these topics, but I can't manage to construct a code that meet my professor's specifications.

I do hope you can help.

Thank you.


I wouldn't say that constructors set default values; I think the term "initial values" is more appropriate, especially in a case like this where the value never changes. The constructor of an object controls the value of your object at the beginning of its life; there's nothing "default" about that. I expect that an item will never change its name or weight, so your constructor is the only way that you will ever set those values.

You will also probably want member functions for getting the name of an item and the weight of an item. Other than that, it seems like your item class doesn't need to do anything.

As for putting a linked list in your inventory class, I'm sure you can find more than you could ever want about linked lists on the web. The trickiest part will probably be deciding what happens if someone tries to put items into the inventory that cause it to exceed the maxWeight.


Instead of defining the name attribute as of type char *, I'd define it using the std::string type instead.

Edited by mvmalderen


There I was typing up all the benefits of std:vector until I re-read your prof. requires you to write a Linked List.

Linked lists are quite simple. They are classes that hold the data and the position to the next class in the list, they may also hold a link to the previous object.

An example of a linked list is:

#include <string>
#include <iostream>

class MyObject
    MyObject(int age, std::string name)
        Age = age;
        Name = name;
    int Age;
    std::string Name;
    MyObject* m_pPreviousObject;
    MyObject* m_pNextObject;

class ListManager
    MyObject* Root;

void PrintAllPeople(MyObject* rootNode);

int main(int argc, char** argv)
    ListManager root;
    root.Root = NULL;

    MyObject tom(46, "Tom");
    MyObject sarah(16, "Sarah");

    tom.m_pNextObject = &sarah;
    tom.m_pPreviousObject = root.Root;

    sarah.m_pNextObject = NULL;
    sarah.m_pPreviousObject = &tom;

    root.Root = &tom;


    return 0;

void PrintAllPeople(MyObject* rootNode)
    MyObject* currentNode = rootNode;
    while(currentNode != NULL)
        std::cout << "Name: " << currentNode->Name << std::endl;
        std::cout << "Age: " << currentNode->Age << std::endl;

        currentNode = currentNode->m_pNextObject;

This isn't a particularly good example, I wrote it off the top of my head, but should give you an idea of what to do next. (Your inventory would be your root node, your items would need links to the next item etc.)

I should also add, that contrary to what I wrote here, your ListManager should maintain your links through "Add" and "Remove" code.

Edited by Ketsuekiame: Missed part of example out

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.