Skip to main content
67 votes
Accepted

Should I avoid using object inheritance as possible to develop a game?

Favour composition over inheritance in your entity and inventory/item systems. This advice tends to apply to game logic structures, when the way in which you can assemble complex things (at runtime) ...
Engineer's user avatar
  • 30k
20 votes

Should I avoid using object inheritance as possible to develop a game?

You got a few nice answers already, but the huge elephant in the room in your question is this one: heard from someone that using inheritance must be avoided, and we should use interfaces instead ...
AnoE's user avatar
  • 540
14 votes

Should I avoid using object inheritance as possible to develop a game?

The idea that inheritance must be avoided is simply wrong. There exists a coding principle called Composition over Inheritance. It says that you can achieve the same things with composition, and it's ...
Linaith's user avatar
  • 536
8 votes
Accepted

Unity Hybrid ECS with "old way"

It is perfectly possible to mix both styles. The same GameObject can have some of its functionality implemented with the new ECS system and other functionality in classic MonoBehaviour events. What ...
Philipp's user avatar
  • 120k
7 votes
Accepted

In an object-oriented game engine, should there be seperate classes for objects with and without parents?

what should the parent of the root Instance in the hierarchy be? Nothing. Does Instances without parent make sense? If you, for example, might want a factory that ...
Theraot's user avatar
  • 27.2k
6 votes

Component oriented programming for rich RPG world

Taking the example of the guitar: (...) a player you can, for example, not only play on guitar as a Musical Instrument, but use it as a Weapon or, what is perhaps more unobvious, throw it right to ...
Theraot's user avatar
  • 27.2k
6 votes

Should I avoid using object inheritance as possible to develop a game?

The problem is that inheritance leads to coupling--your objects need to know more about each other. That's why the rule is "Always favor composition over inheritance". This doesn't mean NEVER use ...
Bill K's user avatar
  • 219
6 votes
Accepted

Designing PHP RPG game: Should I use multiple classes for each type of item or a single table?

A rule of thumb is that you use different classes when objects require different code and instances of the same class when the objects only require different values. So when ...
Philipp's user avatar
  • 120k
4 votes
Accepted

UPDATED: Deserialise Nested JSON with JSON Utility Unity 5.4

Your JSON firstly is faulty and secondly does not match the data model. To break down your JSON: ...
BackFromExile's user avatar
4 votes
Accepted

How to link my weapons with their corresponding ammo supply in the Unity Inspector?

You probably don't actually want your weapon to hold a reference to the instance representing your ammo stock of its type. That makes it very difficult to set up a weapon prefab for a pickup/powerup, ...
DMGregory's user avatar
  • 135k
3 votes

Should I avoid using object inheritance as possible to develop a game?

Contrary to the other answers, this has nothing to do with inheritance vs. composition. Inheritance vs. composition is a decision you make regarding how a class will be implemented. Interfaces vs. ...
StackOverthrow's user avatar
3 votes
Accepted

Three levels deep composition (player<-character<-spell), with preset character+skill sets

This looks like a job for the Flyweight Pattern! (Closely related to the Type Object pattern) Here you separate the concept of a character archetype from the instance of the character itself. You ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

OOP design principles that fit with Unity

Unity does cater for that sort of abstraction, however you have to do some additional work to leverage it. In my current project I have series of characters that can be controlled by the player or by ...
Droppy's user avatar
  • 214
3 votes
Accepted

Ideas/suggestions to implement an item combination system

When you handle all item combinations in the Inventory class, that class will potentially grow to an enormous size because it needs to implement the combination logic to handle every single item ...
Philipp's user avatar
  • 120k
3 votes

Passing a value to a stuct

you are copying in your ranged-for loop. You should use for (auto& path : Path) This will pass by reference and allow you to make changes to the variable ...
Summer's user avatar
  • 829
3 votes
Accepted

How to modularly call a script based on parameters within the script?

You can have just one interface, an IAction or similar. Make Lift implement it. Then, inside your main ...
Alex F's user avatar
  • 425
3 votes

How to modularly call a script based on parameters within the script?

Since Alex F already covered tackling the two with a single interface, I'll show you one other trick, using the Unity Events system: ...
DMGregory's user avatar
  • 135k
3 votes
Accepted

Mob generation design - is creating a class for each mob efficient?

Typically for a pure data change like this, we would not use separate classes. Instead, you could make your class non-abstract (say "BasicMob" to leave room if you need more complex mob classes in ...
DMGregory's user avatar
  • 135k
3 votes

Confused, maybe the mistake ik is the foreach loop

You have at least one obvious error at hand. foreach (RectangleShape obstacles in JumpAtObjectList) { MainPlayer.Jump(deltaTime, obstacles); break; } ...
Zibelas's user avatar
  • 4,346
3 votes
Accepted

How to use AddComponent to add an instance of the derived class, not the base class that defines the method?

You can accomplish this using the Curiously Recurring Template Pattern: ...
DMGregory's user avatar
  • 135k
3 votes

Name for groups of cards in a card game

When I did this a couple years ago, I called them all "piles", and a hand (fan) was just a special subcategory of pile that could hover off-table and have private visibility to one player.
DMGregory's user avatar
  • 135k
2 votes

Making unity inspector accept classes that inherit from a base class

I'm unable to reproduce the problem described in this question when using types that are derived from MonoBehaviour or ...
DMGregory's user avatar
  • 135k
2 votes

LIBGDX - group as child of an actor?

We don't know which things are containers and what they can contain. But let's assume your world work this way: Empires can contains Kingdoms, but also Cities (which are not contained by any ...
Sebastien Servouze's user avatar
2 votes
Accepted

How to create custom methods for sprite groups in pygame?

You can create a pygame.sprite.Group subclass and add the methods you need. If you want to customize and override the draw ...
skrx's user avatar
  • 335
2 votes

Should I avoid using object inheritance as possible to develop a game?

Whenever someone tells you that one specific approach is the best for all cases, it's the same as telling you that one and the same medicine cures all diseases. Inheritance vs composition is an is-a ...
Dragan Juric's user avatar
2 votes

How to use This Keyword and using iterators to call class methods?

These two lines in the constructor do not do what you think they do: GameObject::maxHp = _maxHp; GameObject::currentHp = _currentHp; In the constructor, as in ...
Patrick Hughes's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible