#Inheritance

1 messages · Page 1 of 1 (latest)

hasty relic
#

I have heard inheritance isn't great and it shouldn't be used that much but does it apply to game dev aswell ? Let's say we have a Weapon class, Then Staff extends it and then FireStaff extends Staff or for example we have a Enemy class and then Zombie extends it

haughty swan
#

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

sacred prawn
haughty swan
#

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

maiden sleet
#

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.

hasty relic
# sacred prawn I don't think I've ever heard that inheritance is "bad". It's just another tool ...

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

▶ Play video
haughty swan
hasty relic
haughty swan
#

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

hasty relic
#

So for example having PlayerController, PlayerHealth, PlayerInteractions would be composition right ?

#

instead of Player

maiden sleet
#

That would just be a good implementation of single responsibility LUL

hasty relic
#

what would be composition then ?

hasty relic
haughty swan
#

It is hard to give answers for specific stuff in your game like that because I don't know the full context

haughty swan
# hasty relic what would be composition then ?

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.

hasty relic
haughty swan
#

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

rotund heart
#

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.

haughty swan
#

So, basically, "it depends" and you will have to experiment haha.

#

But that is basically all of programming

rotund heart
#

Tottaly agree with you

hasty relic
#

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