• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
shinypixel

Inventory Items: Inherited or composite?

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!

1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0