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) ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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;
}
...
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 ...
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 ...
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:
...
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 ...
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. ...
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 ...
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:
...
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.
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 ...
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 ...
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 ...
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 ...
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 ...
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.
...
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 ...
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 ...
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 ...
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 ...
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, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
oop × 127architecture × 38
c++ × 30
c# × 19
unity × 18
design-patterns × 14
java × 12
software-engineering × 11
2d × 7
entity-system × 7
game-loop × 6
objects × 6
collision-detection × 5
libgdx × 5
sfml × 5
inheritance × 5
opengl × 4
xna × 4
game-design × 4
rendering × 4
data-structure × 4
component-based × 4
javascript × 3
python × 3
polymorphism × 3