🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Inventory System: How to figure out which subclass item to instantiate without switching all of them!

Started by
7 comments, last by babaliaris 3 years, 5 months ago

I'm creating an inventory system C# and I'm worried about the following code

//===========================Item Type Enumeration===========================//
public enum ItemType
{
    NONE,
    UKNOWN,
    ROSE,
    SALT_BULLET,
    SHOTGUN,
    WELL_BUCKET,
    WELL_ROPE,
    WELL_PUMP,
    LIBRARY_BOOK,
    KEY
}
//===========================Item Type Enumeration===========================//



//===============================Game Item Base==============================//
public abstract class GameItemBase
{
    public ItemType  m_ItemType;    //What type of item is it?
    public int       m_MaxStacks;   //Maximum stacks.
    public bool      m_IsQuestItem; //Is a quest item?
    public bool      m_CanBeHold;   //Can the player hold it in his hands?
}
//===============================Game Item Base==============================//




//===============================Game Item None==============================//
public class GameItemNone : GameItemBase
{
    public GameItemNone()
    {
        m_ItemType      = ItemType.NONE; //What type of item is it?
        m_MaxStacks     = 0;             //Maximum stacks.
        m_IsQuestItem   = false;         //Is a quest item?
        m_CanBeHold     = false;         //Can the player hold it in his hands?
    }
}
//===============================Game Item None==============================//

But most importantly:

    public GameItemBase GenerateGameItem() 
    {

        switch (InventoryItemType)
        {
            case ItemType.NONE:
                return new GameItemNone();

            case ItemType.UKNOWN:
                return new GameItemUknown();

            case ItemType.ROSE:
                 return new GameItemRose();

            case ItemType.SALT_BULLET:
                return new GameItemSaltBullet();

            case ItemType.SHOTGUN:
                return new GameItemShotgun();

            case ItemType.WELL_BUCKET:
                return new GameItemWellBucket();

            case ItemType.WELL_ROPE:
                return new GameItemWellRope();

            case ItemType.WELL_PUMP:
                return new GameItemWellPump();

            default:
                return null;
        }
    }

In order to instantiate a GameItem, I have to switch through the entire ItemType enum.

Is there a way to automate this functionality? For Long Term Support purposes, this will get out of hand if I have lots of different game items, for example, if I'm making an RPG game.

Can I use templates to automate this somehow?


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Advertisement

So far, you have nothing inside a derived class except a few variable values. In other words, it looks like you can use GameItemBase for each one if it wasn't abstract. So instead of making a derived class for each, write a file with these magic values, and load it into eg a GameItemBase array indexed by enumeration (note sure if it's possible in C#, but you get the idea).

Alberth said:

So far, you have nothing inside a derived class except a few variable values. In other words, it looks like you can use GameItemBase for each one if it wasn't abstract. So instead of making a derived class for each, write a file with these magic values, and load it into eg a GameItemBase array indexed by enumeration (note sure if it's possible in C#, but you get the idea).

The reason I did it this way, it's because in the future I'm going to add more properties unique for each item. But maybe this can be separated from the inventory system.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Hey babaliaris,

I was wondering, what benefit are you hoping to get with making so many different item object classes? What's the use case, or usage code, for them?

Nobody said your variables must be POD (plain old data). You can also make eg a “lightProperty”, and stick an object in it that you query when you want something done with light. You could have different objects, eg one that works in a cave, or one that works under water, or one that lights up if an enemy is near.

Similarly have various “fightProperty” or “healProperty" objects. So while all objects have these properties, each object can have a different combination of them, or be configured stronger or weaker (new HealProperty(25) vs new HealProperty(10000) for example ) etc.

In other words, “compose" an item by composition rather than inheritance (or were you going to copy/paste 5 uses of the same property to 5 of your derived classes?).

Alberth said:

Nobody said your variables must be POD (plain old data). You can also make eg a “lightProperty”, and stick an object in it that you query when you want something done with light. You could have different objects, eg one that works in a cave, or one that works under water, or one that lights up if an enemy is near.

Similarly have various “fightProperty” or “healProperty" objects. So while all objects have these properties, each object can have a different combination of them, or be configured stronger or weaker (new HealProperty(25) vs new HealProperty(10000) for example ) etc.

In other words, “compose" an item by composition rather than inheritance (or were you going to copy/paste 5 uses of the same property to 5 of your derived classes?).

Thank you! I think I get it! My inventory system does not need to know about the specifics of an item, but the item itself can.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

If you have different instances which each have different properties, why not use any kind of ECS or in the C# case DynamicObject? Create an enum that defines your item types and use that to identify them, maybe in a base class that inherits from DynamicObject and everything else can be dynamic properties

Shaarigan said:

If you have different instances which each have different properties, why not use any kind of ECS or in the C# case DynamicObject? Create an enum that defines your item types and use that to identify them, maybe in a base class that inherits from DynamicObject and everything else can be dynamic properties

I'm making the game in Unity, so it's ECS driven already.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

This topic is closed to new replies.

Advertisement