Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Inventory Items: Inherited or composite?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 shinypixel   Members   -  Reputation: 226

Like
0Likes
Like

Posted 07 June 2014 - 02:39 PM

I'm not sure what I think on this topic.

 

I first preferred inheritance for something like this...

 

Item (base)

 - Cloth (inherited of class Item)

 - Weapon

 - Furnature

 - // etc. etc.

 

Then, I had a collection: m_items<Item>

 

I'd store everything into m_items: cloth, item, weapon, etc.

 

Then, I'd do a foreach on each Item. Well, they're now of type Item. I don't know what's what.

 

There is a way of determining the type by using OfType<DataType>() in a foreach, but after making 3-4 foreach'es, it defeats the purpose of something clean and elegant.

 

Being a little lost, I wanted to just do composites (without inheritance) thinking it would be cleaner. I'm a little annoyed with adding new functions, like AddCloth(), AddWeapon(), etc. with a linked list to each type, rather than just AddItem(Item). I'm also assuming this means I'm losing the data of the inherited type, since it's storing Item, not Cloth, Weapon, etc. with their own properties.

 

Of course, adding an item to an multidimensional cell of type linkedlist is easier with inheritance. Trying to search it and find out what inherited type Item is, and still retaining the inherited data, is probably my biggest issue I'm wondering on.

 

Is there a recommended way with adding and searching through items? I wonder if it's just a trade-off matter.

 


Edited by shinypixel, 07 June 2014 - 02:55 PM.


Sponsor:

#2 shinypixel   Members   -  Reputation: 226

Like
0Likes
Like

Posted 07 June 2014 - 03:01 PM

Well, I find myself favoring inheritance again.

 

Say I have this..

class Weapon : Item { }

 

Would I add it like:

 

AddItem(myWeapon);  // type item

or..

AddWeapon(myWeapon).  // type weapon

 

I'm assuming the first kills the inherited data?

 

or even..

myGameMap.get(0,0).AddItem(mySword);  // assuming this just stores the item data, not the sword inherited data?


Edited by shinypixel, 07 June 2014 - 03:05 PM.


#3 SeraphLance   Members   -  Reputation: 1750

Like
0Likes
Like

Posted 07 June 2014 - 04:02 PM

Every language that I've used lets you somehow 'generify' types.  In C, it's void*, and in Java it's Object.  To go past that, just carry along some metadata.  e.g. (pseudocodeish):

Enum ItemType { WEAPON, ARMOR, CONSUMABLE, KEY_ITEM }

class Item { string name; }

class Weapon : Item { int damage; uint accuracy; someOnAttackCallback onAttack; someOnEquipCallback onEquip; }

class InventoryItem 
{ 
   ItemType type; Item item; bool isEquippable; bool isConsumable; Inventory owner; 
   void equip() 
   { 
      if(isEquippable) 
      { 
         if(type == WEAPON) 
            inventory.equipPlayer<Weapon>(item); 
      } 
   }
}

//etc.

This is probably what I'd do as a first pass.



#4 shinypixel   Members   -  Reputation: 226

Like
0Likes
Like

Posted 07 June 2014 - 04:03 PM

I think I figured it out. I just compared items with base.ID while using inheritance.

 

Just saw your post. Thanks. It should help.


Edited by shinypixel, 07 June 2014 - 04:05 PM.


#5 SeanMiddleditch   Crossbones+   -  Reputation: 9901

Like
1Likes
Like

Posted 07 June 2014 - 04:31 PM

You might still consider composition. It makes it significantly easy to build more complex types of items, or even just moving to a data-driven model for items.

You can have item components like:

Carryable (weight, size, link to inventory UI object template)
Equippable (slot, min level, etc.)
Weapon (base damage info)
Effect (trigger event, what happens on event)
Droppable (link to in-game object template that represents item on the ground)
Armor (base armor info)
Charges (remaining charges)
Usable (interaction method like eat/drink/activate, event name, charges per use)

So you can make a longsword that is Carryable (4 lbs.), Equippable (hand, level 1+), Weapon (2d4 slash damage), Droppable (longsword.tpl).

Then you can "inherit" that template to make a Magic Flame Burst Longsword that adds a second Weapon component (1d8 fire damage), adds Effect (6d6 fireball on "flameburst"), Effect (disable second Weapon component on event "charges_depleted"), Charges (10), Usable (activate, event "flameburst", charges 1), and overrides the Carryable and Droppable properties to appropriate versions that visualize the fire nature of the sword. This gives you a longsword that deals extra fire damage so long as it has charges and lets the user activate the sword's ability to cast a fireball using a charge. All data-driven, no need for a designer to edit any code, all buildable in a nice UI.

Those are all just example components from a brain dump, of course. The actual specific components you'd want to use or need are going to require a bit more thought and tailoring to your specific game.

#6 DiegoSLTS   Members   -  Reputation: 2054

Like
0Likes
Like

Posted 07 June 2014 - 05:44 PM

Well, I find myself favoring inheritance again.

 

Say I have this..

class Weapon : Item { }

 

Would I add it like:

 

AddItem(myWeapon);  // type item

or..

AddWeapon(myWeapon).  // type weapon

 

I'm assuming the first kills the inherited data?

 

or even..

myGameMap.get(0,0).AddItem(mySword);  // assuming this just stores the item data, not the sword inherited data?

You're confused about inheritance and polimorfism. When you treat a derived class as a base class you don't "kill" anything, the whole object keeps everything from the derived class all the time. You can always cast the object back to the derived class and everything will be there.

 

What you lose is the ability to access directly to the added methods (if you call a virtual method, the derived implementation will be used, not the base one) or the added attributes of the derived class.

 

I'm not sure what I think on this topic.

 

I first preferred inheritance for something like this...

 

Item (base)

 - Cloth (inherited of class Item)

 - Weapon

 - Furnature

 - // etc. etc.

 

Then, I had a collection: m_items<Item>

 

I'd store everything into m_items: cloth, item, weapon, etc.

 

Then, I'd do a foreach on each Item. Well, they're now of type Item. I don't know what's what.

 

There is a way of determining the type by using OfType<DataType>() in a foreach, but after making 3-4 foreach'es, it defeats the purpose of something clean and elegant.

 

Being a little lost, I wanted to just do composites (without inheritance) thinking it would be cleaner. I'm a little annoyed with adding new functions, like AddCloth(), AddWeapon(), etc. with a linked list to each type, rather than just AddItem(Item). I'm also assuming this means I'm losing the data of the inherited type, since it's storing Item, not Cloth, Weapon, etc. with their own properties.

 

Of course, adding an item to an multidimensional cell of type linkedlist is easier with inheritance. Trying to search it and find out what inherited type Item is, and still retaining the inherited data, is probably my biggest issue I'm wondering on.

 

Is there a recommended way with adding and searching through items? I wonder if it's just a trade-off matter.

If you want to use inheritance I guess you can define a common class to every item that's enough for the inventory logic. You probably want to show stats of the item, so a "getStats()" method could be in the Item class. You probably want to have an icon for each item (getIcon()), a name, etc. An item could be equiped or used, so you can add methods like "isEquipable()" and "isUsed()" to the Item class too. Once you know if an item can be equiped you can cast it to, for example, an "EquipableItem" class that has more functionality, like a type that describes where can you equip that item; if you know it can be used you can cast it to "UsableItem" and call a "use()" method on it.

 

That way you're still doing something clean and extensible, you don't check for actual types, you check what the item is used for, and you only need to worry about Item, EquipableItem and UsableItem classes.

 

If you want to use composition you can have multiple "add" methods with different type for the parameter (overloaded method), so you keep a clean interface (you always call inventory.add()) and the compiler knows what "add" method to execute. Anyway, as you described you intent to use composition it sounds like you are still using inheritance, if in each "add" method you only store the item in a list of "Item" there's no composition and all those multiple "add" methods are useless, keeping a unique add(Item item) method would be enough but you'll be back again with the previous approach.

 

If you wan't to use composition you shouldn't have a base class called "Item" and a list of those items, you should have lists for Weapons, Clothes, etc, and add each item to the list it belongs. Then when managing the inventory you must use all the lists to handle everything.

 

I prefer the inheritance approach, but there's not a best way of doing it.


Edited by DiegoSLTS, 07 June 2014 - 05:46 PM.


#7 kauna   Crossbones+   -  Reputation: 2890

Like
1Likes
Like

Posted 07 June 2014 - 06:55 PM

There are uses for inheritance but I think that this isn't one of the cases. I'd go with the composition route, you could have an XML-file describing each item you'll need in the game. The data stored in the file would define the functionality. 

 

Instead of having classes such as "Weapon" or "Potion" or "Cloth" etc, you'll have just one item class and it's parameters define how it can be used, what effect is has when used, can it be weared or used as a weapon. Most of those things fall in the category "has the item a certain parameter?" so it is one function which can reveal many things. 

 

With the above function you'll avoid all the functions to related to certain kind of manipulation of the item (such as "isEquipable()"). 

 

Cheers!



#8 frob   Moderators   -  Reputation: 28168

Like
0Likes
Like

Posted 08 June 2014 - 04:30 AM

I could argue either way, it all depends on the engine.

If your engine is already designed around components, build it out of components. As was already mentioned, it enables designers to try out many new variations at a very low cost. You can build simple systems that designers build up in complex ways. There will still be some inheritance as components inherit bits of functionality, but the systems are designed around game objects that do nothing of themselves and are not even visible except a component gives it a mesh.

If your engine is already designed around code-centric models where game objects are full-featured and individually coded, go that route. It means design is less flexible and everything is handed over to the programmers up front except perhaps for some variables marked for designer adjustment. But it also can have many benefits if the programmers understand what they are doing and know how to take advantage of them.

Each direction has its own set of pros and cons. A bit of searching can find long lists of both.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#9 Ectara   Crossbones+   -  Reputation: 3089

Like
0Likes
Like

Posted 08 June 2014 - 07:58 AM


I'm assuming the first kills the inherited data?

or even..

myGameMap.get(0,0).AddItem(mySword); // assuming this just stores the item data, not the sword inherited data?

If it's a container of base class objects, then inserting a copy of the object would result in slicing. If you stored base class pointers, then pass in a pointer to an object, there's no slicing.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS