Item system - design patterns

Started by
5 comments, last by LordRhys 11 years, 5 months ago
Hello,

I want to design an easily extensible item system for my game where I don't need to modify existing code much. I want to add items with new effects on the fly.

Traits my items have in common:

A name,
a description,
an npc sell price,
if it can be equipped,
required level,
if it is usable in battle,
if it is usable outside of battle,
a cooldown

So I can encapsulate this already into a class. But now there are item effects.

Example:
heals x health points,
heals x mana points,
removes debuff x,
adds buff x,
gives x stat points on equipping,
has x% chance to create some other effect when equipped,
etc.

And those can be combined like: heals x health and mana points. The first four are examples for effects a usable item can have while the last two are examples for effects equipable items can have.

The idea behind it is that I have this structure in a database as well and I can add a new item with a new effect combination in my database, my code reads this then and builds it together into a new fancy item, without me having to modify much. The only time I need to modify my code is when I add new effects obviously.

How would you put this together in design pattern(s)? I am writing my game in a language which has support for OO obviously.
Advertisement
I'd recommend an effect class that the item class has a pointer to. The effect class is the base class for all the different kinds of desired effects. You could have a list of effects in the item class instead of a single pointer to chain effects together, but that might complicate things. Either way, separate the effect from the item and make the interface for enabling the effect generic so it can be enabled in a consistent manner.

Cheers,

Bob

[size="3"]Halfway down the trail to Hell...
makeing one myself and as far as iv seen you will need an item object with a list of effects of each one.
the effects are generic and you dont even have to name them or make them all in advance thats the work of you item creator.

now the effects need nothing unless you adress them BUT you can make new effect any time you need.
Are you interested in generating them on the fly (I mean, randomly) or you're going to make the objects one by one (by a human)? Or perhaps use them as templates?
I worked on an RPG-oriented game system years ago with a friend for his MUD. I don't remember much but in the end we had to classify all the possible effects in advance to resolve cross-dependancies when generating.
So for example, if an object dealt fire damage, it couldn't roll a ice damage enchant (he decided fire would negate ice).
Similarly, we had to merge damage bonus such as +30%, +20% ends up being +50% and not (130*120=+56%).
Stacking was surprisingly involved as we needed to figure out if similar effect would stack linearly (+), negate each other, not stack, or combine by (*).

Besides generation, game play was relatively straightforward, we designed a set of signals/callbacks/events to be broadcast to each object. They took care of implementing the per-object requisite testing, checking against various conditions etc.
I have to admit the way we made combat work was pretty ugly. I'd probably make it nicer nowadays but at the time I was really unsatisfied by it, magic such as "regen" or anything else enabling lasting effects went through several holes in the design.

Previously "Krohm"

Separate item system and effect system, don't mix them up.

The effect system manages a collection of effects.
Each effect has a unique ID (the ID can be either string or integer, whatever), and implement an interface, e.g IEffect.
IEffect has a function, something like, void perform(Item * item);
When you add new effect, just implement IEffect and add the interface to effect system.

The item system manages a collection of items.
The items can be read from database, or added by code.
An item holds a collection of effect IDs.
When you want to do the effect, just ask the effect system with the effect ID, and the call IEffect::perform with the given item.

With this design, your item only knows an effect ID. It doesn't care what's the effect, or how effect works.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

When I was messing around with a magic the gathering clone I used a sortve hardcover + scripted solution. I boiled everything down to; cost - what is needed to use the ability. Target - what kind of objects can it effect. Effect - what the ability does.

Effects were hardcover as a series of functions. Ie DealDamage(target&, amountOfDamage). I had a separate functions that would validate cost, target, etc.

If every thing passed another function would parse the effect and pass its parameters and the desired target to the abilities corresponding function which would apply the actual effect.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
You could use a combination of what was mentioned above with EffectID but enumerate them with names like what is used in most common games now like Borderlands, and Diablo, etc.. where your Items have names like Fiery Vampiric Longsword of Flight. Then you have 3 effects Fiery, Vampiric, and Flight, which can fall into 3 different categories that have various levels like say: Hot, Flame, Fiery, Inferno, Hellfire for example and then you can have an exclusion list so you couldn't have a Fiery Blizzard Mace, etc...

This topic is closed to new replies.

Advertisement