Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am making an inventory system. Different inventory item will have different uses so i am thinking about making a base class called InventoryItem and derive Wood, Gold, Weapons and so on. This way all InventoryItems will share some basic functionality and can have their unique as well.

My questions are:

  1. If instead i use a interface i cannot count on specific fields or variables as it only takes methods. Is there any reason to use interfaces?

  2. If i use derived classes and have a List with mixed items from different derived classes i would need some code that goes through each class and casts it to the proper class to use its unique abilities. How is this best handled?

share|improve this question

closed as off-topic by Sean Middleditch, Patrick Hughes, bummzack, Josh Petrie Nov 26 '13 at 16:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Sean Middleditch, Patrick Hughes, bummzack, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

    
A key benefit of both interfaces and inheritance is specifically to avoid (2); you need to rethink your design in this light. Any complex design is almost certain to use both extensively, as it is their appropriate combination that really drives O-O development. –  Pieter Geerkens Nov 25 '13 at 23:38
1  
I'd ask this on the general programming stack where you'll get much better results; this isn't directly related to game programming at all. –  Patrick Hughes Nov 26 '13 at 0:34
    
you should specify the programming language you are using as they behave differently. For example as the accepted answer states C# allows getters and setters in addition to methods. –  Benjamin Danger Johnson Nov 26 '13 at 23:11

1 Answer 1

up vote 0 down vote accepted
  1. If instead i use a interface i cannot count on specific fields or variables as it only takes methods. Is there any reason to use interfaces?

While this is basically true, interfaces may include getters and setters. So you can mimic variable members without actually using them.

Just a simple example:

interface IWeapon {
    unsigned int CurrentAmmo { get; set; }
    unsigned int MaxAmmo { get; }
    TimeSpan ShootInterval { get; }
    TimeSpan ReloadTime { get; }
    string Name { get; }
    void Reload();
}
  1. If i use derived classes and have a List with mixed items from different derived classes i would need some code that goes through each class and casts it to the proper class to use its unique abilities. How is this best handled?

That's exactly the reason for using polymorphism. If done right, your system will automatically call the apropriate member, without forcing casts or any special handling. Have a look at the virtual keyword.

Another short example:

class Drawable {
    virtual void Draw(RenderClass renderer) {
    }
}

// ...

foreach(Drawable d in Entities)
    d.Draw(myRenderTarget);

Derived classes can now do one of two things:

  • Don't implement Drawable::Draw which will essentially redirect all calls to the base class (i.e. default behavior).

  • Reimplement Drawable::Draw using the override keyword to overwrite the default behavior.

If a member is marked as virtual, you'll always call the most derived override being available.

Using an abstract base class is similar, but compared to interfaces this allows you to already implement parts, while leaving others unimplemented (they'll have to be implemented in derived classes).

share|improve this answer
    
Thank you! This did set me straight. I will design the baseclass so that there will not be any need to cast it. :) –  Lautaro Nov 26 '13 at 10:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.