How to manage inventory system if there are too many classes?

#1
So, initially I was planning to create a base class, and some inherited classes like weapon/armour/etc, and each class will have an enum that specifies its type, and everything was going ok until I hit "usable items".
I ended up with creating UsableItem class, and tons of inherited classes, like Drink/Apple/SuperApple/MagickPotato/Potion/Landmine/(whatever that player can use) each with unique behaviour. I planned to store items in the SQLite database, but I discovered that there are not many ways of creating variables(pointers) with type determined at runtime (that preferably get their stats/model/icon/etc from DB). So, I think that I need to use some variation of the Factory pattern, but I have no idea how I should implement it for this particular case (giant switch/case :D).

It would be really nice if you guys can give me some advice on how I should manage this kind of problem or maybe how I should redesign the inventory.

Code: Select all

namespace Inventory
{
enum ItemType
{
Static,
Building,
Usable,
Weapon,
Armour
};

class InventoryItem
{
public:
virtual ~InventoryItem() = default;

virtual ItemType GetType() = 0;
virtual string GetName() = 0;
virtual string GetIcon() = 0;
virtual void Destroy()
{
//TODO: Notify inventory storage
delete this;
}
};

class UsableItem : public InventoryItem
{
public:
struct Usage
{
int Index;
string Use_Name;
};

virtual CryMT::vector<Usage> GetUsages() = 0;
virtual void UseItem(int usage) = 0;
};

class TestItem : public UsableItem
{
int Counter =0;

ItemType GetType() override
{
return ItemType::Usable;
}

string GetName() override
{
return "TestItem";
}

string GetIcon() override
{
return "NULL";
}

CryMT::vector<Usage> GetUsages() override
{
CryMT::vector<Usage> Usages;
Usages.push_back(Usage{1, "Dec"});
Usages.push_back(Usage{2,"Inc"});
Usages.push_back(Usage{3,"Show"});
return Usages;
}

void UseItem(int usage) override
{
CryMT::vector<Usage> uses = GetUsages();
switch (usage)
{
case 0:
for (int i =0; i<uses.size(); i++)
{
CryLog(uses[i].Use_Name);
}
break;
case 1:
Counter--;
CryLog("Dec");
CryLog("%d", Counter);
break;
case 2:
Counter++;
CryLog("Inc");
CryLog("%d", Counter);
break;
case 3:
CryLog("%d", Counter);
break;
default:
CryLog("WRONG INDEX");
break;
}
}
};
}
[/code]
Last edited by MarkusGod on Tue Sep 18, 2018 3:19 pm, edited 2 times in total.

Re: How to manage inventory system if there are too many classes?

#3
I would go with creating Item class that would have slot for model, all the functions like pickup etc, what all items have. Then I would go and create a few classes not particularly by their item names, but classes in general. For example.. CWeapon would derive from IItem interface. So I would go with.. CWeapon, CPotion, CFood, etc... Then I would go and create some folder in assets, let's say Items folder. Inside of it create .xml files for each item. Then factory would pick up all xml files that are there and register types by Xml file name, and all properties that you have inside of it.
Cryengine tutorial videos on my Youtube channel! Check it out !
https://www.youtube.com/user/MusicForLifePL20

Re: How to manage inventory system if there are too many classes?

#4
The data needs to come from somewhere. As Fury mentioned, using XML files is an option.
Generally though, if you are hard-coding item types into the system, using XML files would only be necessary if you would like the functionality of not having to recompile the engine for property changes.
This also is one of the reasons for a scripting system (LUA), as you can define the logic behind your entities by exposing certain methods to the LUA system.
You could also get away with Flow Graph, and using modules to define specific item behaviours.

Either way, this assumes that you want a modular functionality. If you are alone coding this and don't expect designers to modify the logic themselves then this level of detail may not be necessary. Generally, engineer your game to the point it is needed and not more, over engineering systems is one quick way to burn out in smaller teams.

Also, regarding the broken code block, we are investigating this, but for now, you can use the much better and new feature on the forum - Syntax Highlighting:
viewtopic.php?f=40&t=4376&p=10944&hilit ... ght#p10944
Uniflare
CRYENGINE Technical Community Manager
Here to help the community and social channels grow and thrive.

My personal belongings;
Beginner Guides | My GitHub | Splash Plugin

Who is online

Users browsing this forum: No registered users and 2 guests

cron