Jump to content
  • Advertisement
Sign in to follow this  
Butabee

Creating an item system

This topic is 3031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to think of a way to come up with an item system, and what I was thinking of is this...

I have a general item class and it has pointers to all sub-types of items.. weapons, armor, junk.

so if the item type is a weapon I use new to initialize the weapon pointer to the weapon item class which has all the weapon specific attributes.

How does that sound?

Share this post


Link to post
Share on other sites
Advertisement
Sounds like you're implementing polymorphic behavior when your language of choice probably allows you to use polymorphism directly. You could just create a base class Item and derive Weapon, Armor, etc from it. Read up on polymorphism in C++ here and C# here. If you're using some other language then you can just do a search for it yourself.

Share this post


Link to post
Share on other sites
Create an Item class, don't derive anything from it, and instead have its behavior defined in some sort of external script/data file/whatever.

Share this post


Link to post
Share on other sites
First of all, what are your requirements? Will items be pretty much unique, or will there be many instances of the same item type? How many item categories will you have, and how many types do you expect to have overall? Is it important to easily add or change item types? How will items be used in your game?

Share this post


Link to post
Share on other sites
virtual inheritance/polymorphism definitely sounds like the way to go.

Basically what I'd do is have your general item base class / interface with some protected members that all items might have like Name, Description, Encumbrance, value etc. depending on how your game is gonna be set up. some public methods like GetName, GetDescription, etc.

And then from there you'd have your general base classes for things like weapon and armor, so your armor class would inherit from the item class and then it can add things like armor type, armor defense, etc. and then maybe somewhere down the road if you wanted you could specialize it further, inheriting from your armor base class to more specialized scenarios.

Virtual inheritance is a beautiful thing.

Share this post


Link to post
Share on other sites
A simple hierarchy of base item types.


Item
|
|-Weapon
| |-Sword
| | |-Longsword
| | |-Broadsword
| | |-Shortsword
| |-Wand
| |-Staff
|
|-Armor
| |-Helmet
| |-Boots
| |-Gloves
| |-Helmet


Define specific items of these base types through a script.


name = "Shortsword of Healing"

baseType = WeaponType.ShortSword

minDamage = 6
maxDamage = 12

damageType = DamageType.Slashing

function onDamage(player)
player.hitpoints = player.hipoints + 12
end


This way you know how to treat items in the code (ie, you can only equip a helmet on your head) and the behavior of the items is easily extended from outside of the code. Also since the properties of a weapon are data, they can be randomized.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!