Skip to main content
The 2025 Annual Developer Survey is live — take the Survey today!
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
  • 30.4k
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 As ...
AnoE's user avatar
  • 525
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
  • 123k
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
  • 28k
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
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
  • 139k
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,672
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
  • 139k
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

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
  • 139k
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

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
  • 139k
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
  • 139k
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
  • 139k
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
  • 139k
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 Kingdom ...
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

How to use OOP to handle different requirements

You don't necessarily need to supply the position of the player to the Missile. You can make the position of the player available to everyone, and let your game ...
Vaillancourt's user avatar
  • 16.4k
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
2 votes

How to update the value of all owners of shared_ptr?

Update Object in place. *cache["foo"] = Object(stuff); Or add a method that will reload the entire object. ...
ratchet freak's user avatar
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
Accepted

Extra constructor call when using inheritance in Godot

Updated version of the initialization process (old version): Node instancing If the Node is an scene instance, do Scene instancing for the corresponding scene, and ...
Theraot's user avatar
  • 28k
2 votes
Accepted

How to include cross cutting concerns like sound/graphics effects in an ECS?

I'll just answer from the perspective of sound integration here. Not quite sure what all would be included in the graphics side of your cross-cutting concerns. I've seen these types of cross-cutting ...
aransley's user avatar
  • 124
2 votes
Accepted

Name for groups of cards in a card game

In Magic the Gathering, the different places that cards or other objects can exist in are called zones. Your hand is a zone, your deck is a zone, your discard pile is a zone, etc. Zones have different ...
Nuclear Hoagie's user avatar
2 votes

Is there any reason not to use classes in C++?

Good question. First what role do classes serve? No seriously, why do we use classes? Some might say thtat it is to bundle related bits of data together... Such as a rectangle has a location, a width, ...
Questor's user avatar
  • 171

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

X