Layers of inheritance in a game

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
spheroidal_defence
Red Slime
Posts: 25
Joined: Mon Aug 04, 2014 10:13 pm
Location: CCCP

Layers of inheritance in a game

Post by spheroidal_defence »

I'm trying to make a RTS game in C++ and I'm sort of worried I'm going to have issues later if I inherit too deeply. I'm sort of new to OOP, and I was wondering if anyone has more experience in this area and could tell me if I'm going overboard with my inheritance. This is currently how my game is laid out:

Code: Select all

Object (For holding instances in my game engine)
    Unit
        Peasant
            Native
            Colonist
        Soldier
            Spearman
            Musketeer
    Building
        House
            Tent
            Shack
This isn't the full list, but the rest looks the same.
Last edited by spheroidal_defence on Thu Jul 09, 2015 5:55 pm, edited 2 times in total.
reinterpret_cast<ClassB>(a);
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7660
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post by Bob the Hamster »

That looks like perfectly reasonable inheritance to me.

I have never heard of problems from inheriting too deeply (unless we are talking about problems with poorly planned inheritance trees, or inheritance that doesn't serve any actual purpose)

If you do want to simplify your implementation, you might try

Code: Select all

    Unit
        Peasant
        Native
        Colonist
        Soldier
        Spearman
        Musketeer
    Building
        House
        Tent
        Shack 
Whether or not this is better mostly depend on, for example, how much Soldier code is shared by Spearman and Musketeer, compared to how much Spearman and Musketeer differ from each other.
User avatar
spheroidal_defence
Red Slime
Posts: 25
Joined: Mon Aug 04, 2014 10:13 pm
Location: CCCP

Post by spheroidal_defence »

Thanks, I think that Soldier units and peasant units are similar enough to be put together. I was just worried that things might get confusing if I had too much inheritance. I guess it will be alright as long as I don't start using MI and that kind of crazy stuff.
reinterpret_cast<ClassB>(a);
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

I think that having extra levels of inheritance is a more harmless example of overengineered code. Try not to add levels of abstraction or indirection before you actually find that they will help you. And don't waste too much time thinking about things like this, it's crippling!
Post Reply