Author Topic: Inventory  (Read 29548 times)

Fenrir

  • Rogueliker
  • ***
  • Posts: 473
  • Karma: +1/-2
  • The Monstrous Wolf
    • View Profile
Inventory
« on: September 12, 2009, 03:30:51 PM »
Using C++

My Weapon class inherits from my Item class. The player has an Item vector as his inventory. The way it's set up now, my 'wield' command has no way of knowing what is a weapon and what isn't. I certainly can't sort the inventory by item type either. How should I be doing this?

Nathan Stoddard

  • Newcomer
  • Posts: 9
  • Karma: +0/-0
    • View Profile
    • My website
    • Email
Re: Inventory
« Reply #1 on: September 12, 2009, 04:27:35 PM »
I would remove the Weapon, Armor, etc classes and have everything be an Item, and add a field for the type of item. Sometimes object-orientation is more trouble than it's worth. This method will require more space (every item will have "damage" and "armor_class" fields, no matter what type of item it is), and it isn't as safe, but it's simpler.

purestrain

  • Rogueliker
  • ***
  • Posts: 172
  • Karma: +0/-0
    • View Profile
Re: Inventory
« Reply #2 on: September 12, 2009, 04:33:38 PM »
Actually inheritance is not always the best choice of object orientation.

Use composition instead: have one item class (maybe even item is to specific.... what about entity?) in which you plug several components. Every think to pick could have a PhysicalComponent (with weight and size), every thing to eat contains a ConsumableComponent - and so on.

Fenrir

  • Rogueliker
  • ***
  • Posts: 473
  • Karma: +1/-2
  • The Monstrous Wolf
    • View Profile
Re: Inventory
« Reply #3 on: September 12, 2009, 06:12:30 PM »
Thanks for the help, guys.

Vanguard

  • Rogueliker
  • ***
  • Posts: 1112
  • Karma: +0/-0
    • View Profile
Re: Inventory
« Reply #4 on: September 14, 2009, 12:38:20 AM »
I have everything as subclasses of the Item class, but the subclasses only differ in a few ways.  For instance, going off of memory, the only line in the Weapon subclass constructor is the line "equipmentSlot = HANDS," meaning that when you try to 'w'ear/wield that item, the game checks what value is entered into equipmentSlot (HANDS is a static final integer), and puts it into the player's appropriate equipment slot.

Is this the best way to do it?  I don't know, but maybe it'll help you out, or give you an idea.

purestrain

  • Rogueliker
  • ***
  • Posts: 172
  • Karma: +0/-0
    • View Profile
Re: Inventory
« Reply #5 on: September 14, 2009, 06:14:51 AM »
Since i'm not in a restricted environment and do not think very detailled about memory consumption (hell, 512bytes per Entity allows me to have 2000items/drinks/creatures/doors in 1mb ram) i'm having a reference to a "draft" for each entity. These templates descripe the plugs and slots they have, and how the item works E.g:

Rifle -> Plug: LHand, RHand; Slots: Clip, SmallSight
RifleClip -> Plug: Clip
NightVision -> Plug: SmallSight

Humanoid creatures e.g. share the same Draft with LHand, RHand, Head and so on... For testing purposes i've also added the Slots of Humans in a limb-system, i'll have to see if it works.


Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Inventory
« Reply #6 on: September 14, 2009, 11:15:26 AM »
You could use data to determine what kind of item we are talking about. Something like this:

if (item_data[type].item_type==Weapon) //it's a weapon

rdc

  • Newcomer
  • Posts: 41
  • Karma: +0/-0
  • You hear a *bump* in the dark...
    • View Profile
    • Clark Productions
    • Email
Re: Inventory
« Reply #7 on: September 14, 2009, 09:52:18 PM »
Yet another option: don't use classes at all, use a set of structs for each item type, and a super-struct that contains a simple id flag, and a union of each struct type. The amount of memory you take up in the union will be the amount the of the largest struct.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Inventory
« Reply #8 on: September 15, 2009, 06:07:46 AM »
Yet another option: don't use classes at all, use a set of structs for each item type, and a super-struct that contains a simple id flag, and a union of each struct type.

Sounds complicated for no reason. I think it's just inheritance from item to subclasses (like weapon) that is too fine-grained. "Don't use classes" Yeah, if you want to go back to old school C and unmaintainable source code.

rdc

  • Newcomer
  • Posts: 41
  • Karma: +0/-0
  • You hear a *bump* in the dark...
    • View Profile
    • Clark Productions
    • Email
Re: Inventory
« Reply #9 on: September 15, 2009, 12:47:46 PM »
You know I am sick of your ignorant bullshit Krice. All you seem to do is to put other people down and inflate your own questionable skills. The truth is, you don't know what the fuck you are talking about. Try learning some skills before you make bullshit comments that make you look even more like an asshole.

Don't bother replying. I am done with this place.

Perdurabo

  • Rogueliker
  • ***
  • Posts: 99
  • Karma: +0/-0
    • View Profile
    • Email
Re: Inventory
« Reply #10 on: September 15, 2009, 03:01:57 PM »
Yet another option: don't use classes at all, use a set of structs for each item type, and a super-struct that contains a simple id flag, and a union of each struct type. The amount of memory you take up in the union will be the amount the of the largest struct.

I use classes, but effectively that's what I use in Kharne.

TItem subclasses from TItemArchetype

with the added restriction that TItem is actually an instantiation of TItemArchetype.

Fenrir

  • Rogueliker
  • ***
  • Posts: 473
  • Karma: +1/-2
  • The Monstrous Wolf
    • View Profile
Re: Inventory
« Reply #11 on: September 15, 2009, 03:50:56 PM »
Fenrir watches in puzzled astonishment as rdc storms out. The monstrous wolf winces as the sound of the slamming door strikes his canine ears.

Wow, that seemed a little.... much. You eat his kitten or something, Krice?

Fenrir's eyes close in fond remembrance as he begins to drool.

Mmmmm.... cat tallow stew.


Anyway, from http://msdn.microsoft.com/en-us/library/64973255%28VS.80%29.aspx:
Quote
In C++, a structure is the same as a class except that its members are public by default.
So, does it make a difference if I use classes or structs, or is there more to it?

At any rate, I've been given more than enough solutions to get the job done. Thanks, everyone!
« Last Edit: September 15, 2009, 03:52:56 PM by Fenrir »

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Inventory
« Reply #12 on: September 15, 2009, 07:48:54 PM »
The truth is, you don't know what the fuck you are talking about.

Of course I know. You can check out what happens when you don't use classes as in Nethack. The result is source code that is very hard to maintain, fix and extend. It's the way C works, I'm sorry to say that. The benefits of class based programming in large scale applications are well known fact. Why don't you study the facts before bursting out in rage?

Z

  • Rogueliker
  • ***
  • Posts: 905
  • Karma: +0/-0
    • View Profile
    • Z's Roguelike Stuff
Re: Inventory
« Reply #13 on: September 18, 2009, 04:51:23 PM »
OO is useful in programming large scale applications and should be used in roguelikes for general programming, but this does not mean that basing your item list as a tree of inherited classes in C++ is a good idea. There are some problems with it, for example if you want to allow using any item as a weapon, as any Hacklike should, it makes no big sense to create a Weapon class. Another things hard to do with this approach include serialization (in C++, in Java it is easy), morphing (one item/monster changing into another), and unclassifiable monsters/items (for example, having properties typical to several of your classes).

Etinarg

  • Rogueliker
  • ***
  • Posts: 424
  • Karma: +1/-1
  • Idea archivist and game tinkerer.
    • View Profile
    • Gedankenweber Blog (German)
Re: Inventory
« Reply #14 on: October 06, 2009, 01:17:38 PM »
Long ago there was an argument of merging monster and items into one structure/class even. This would allow to wield a snake and try to make it bite your oppenents  ;D

Also, it allowed to put monsters into containers, which was a funny bug in a former project of mine.