#Inheritance
1 messages · Page 1 of 1 (latest)
the reason people say it isn't great is because it is a very rigid way of working, as in, it is difficult to make large changes to your game if you lock yourself into a certain inheritance hierarchy. It is also convoluted as much of the logic is hidden away in base classes that you have to hope there's no bugs in
that said, many developers still use it, so it's not like it is taboo
you might as well try it and figure out your own opinions on it
I don't think I've ever heard that inheritance is "bad". It's just another tool that can be used in cases where it should be used.
I'm not really sure why anyone would fight against using inheritance in the example you gave here, it's perfectly fine for that
It can be quite a nuisance later in development if your problem space changes at all. I do not recommend long hierarchy chains
Doesn't sound like you have looked very hard if you can't find instances of people warning against that exact style provided in the OP's example. Most experienced programmers argue for composition and data driven design for scenarios where you want modular behavior like this
But great for prototyping and just quickly getting stuff into the game. Works for games with smaller scopes, and when you are working alone on something relatively simple
Inheritance and composition are not mutually exclusive though.
In fact, I would say that composition most of the time relies on inheritance.
Not to mention that the latter is a great way of abstracting functionality.
Let's discuss the tradeoffs between Inheritance and Composition
Access to code examples, discord, song names and more at https://www.patreon.com/codeaesthetic
0:00 Introduction
0:25 Inheritance
3:32 Composition
5:22 Abstracting with Inheritance
6:52 Abstracting with Interfaces
8:20 When to use Inheritance
yes, although usually it does not look like that example (starting to create long inheritance trees), but good point
What would be the alternative way to approach this problem instead of using Inheritence ?
Well you could still use it, but you would have to be cautious of not creating too many base classes. The problem is once you want to start intermingling behavior for a specific item or enemy, but you realize that you can't because of the way you have set up your base classes.
You would also try to put common behavior into additional scripts you can attach to your objects instead of putting everything into a base class, that way simple behavior can be reused without needing to derive from anything in particular
Also trying to use interfaces wherever possible instead of base classes
So for example having PlayerController, PlayerHealth, PlayerInteractions would be composition right ?
instead of Player
That would just be a good implementation of single responsibility 
what would be composition then ?
The only reason i had a Enemy base class was to have the enemy follow the target. A better approach would be having a seprate script called FollowTarget.cs ?
It is hard to give answers for specific stuff in your game like that because I don't know the full context
Well, composition would usually involve figuring out what pieces can be reused, and simplifying them. Like, can you make the Health be reused for enemies too? Perhaps Health is just a value and has some events.
I would say an Entity Component System is basically composition, as the behavior of each Entity is only determined by the small components it has attached to it.
so "Reusable Scripts" or "Generic Scripts"
It's like, separating things out into smaller pieces and then putting them together to create the final "thing"
and if you do this well, you usually find you can reuse stuff
But like Zenvin said, it is not black and white. You can blend inheritance with composition based on your needs
In your example, using inheritance for game development can be a reasonable approach. Creating a base class like "Weapon," then extending it with subclasses like "Staff" and "FireStaff" can help organize and reuse code for common functionalities while allowing for specific variations. Similarly, having an "Enemy" base class and extending it with subclasses like "Zombie" can help define shared properties and behaviors across different types of enemies.
However, you must be aware of the potential downsides of using inheritance. Overreliance on inheritance can lead to complex class hierarchies. Changes made to the base class may affect all its subclasses.To mitigate these issues, consider using Composition. For example, it can be a powerful alternative to inheritance, where objects are composed of other objects rather than solely relying on inheritance hierarchies.
So, basically, "it depends" and you will have to experiment haha.
But that is basically all of programming
Tottaly agree with you
Thanks for everyone's input. I will try to consider these things for my refactor i am gonna do so my future projects won't suffer lol