Temple of The Roguelike Forums
Development => Programming => Topic started by: Fenrir 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?
-
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.
-
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.
-
Thanks for the help, guys.
-
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.
-
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.
-
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
-
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.
-
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.
-
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.
-
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 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:
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!
-
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?
-
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).
-
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.
-
This would allow to wield a snake and try to make it bite your oppenents ;D
I did that once, except with elves. That project died, though.
-
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
In Teemu you can pick up some monsters (grab them with 'g') and throw them, but you can't wield them as a weapon. I think you could actually grab the monster while it's fighting you, but I don't remember was it possible or not.. yeah, it's my game :)