Alternative to multiple inheritance

Asked

Viewed 462 times

3

It is not possible to inherit more than one class in C#, so I come here looking for suggestions for my problem.

I am building a small game using Unity. In Unity the game objects (GameObject) inherit from a superclass called MonoBehaviour, as is the case in my class Player, and as is the case with my class Enemy.

So much Player as Enemy has the possibility to choose a target on the screen and run Attack(). However, in the case of player, your target may be both a Enemy as much as another Player. In innocence I quickly created a class Entidade, I put in her a method RecebeAtaque(Attack a), and did Player and Enemyinherit from Entidade. After that, I did the method Attack receive a parameter Entidade.

However, Player and Enemy already inherit from Monobehaviour, thus, this was not possible. I need them to inherit from MonoBehaviour, because this super class gives me ways to manipulate the GameObject in the game scene.

How could I implement this?

  • 1

    Do Entidade inherit from MonoBehavior.

  • Incredible as it may seem, this is exactly what I wanted. I tested the implementation here and it worked exactly as I expected. Thank you very much, it was such a simple thing, and I could not notice. If you want to post an answer, I mark as accepted :)

  • 1

    While we cannot have multiple inheritance, we use interfaces to, for example, add behaviors to an object. In the case of Unity, the class MonoBehavior serves exactly that. You can compose a complex object with several MonoBehavior, I mean, you add behaviors. In fact, I try to avoid gigantic classes with many responsibilities, I suggest trying to break into smaller classes, respecting SOLID. In case, you already managed to solve the problem, just wanted to give more details ;) good luck!!!

1 answer

3


Multiple inheritance is problematic and is rarely needed in fact. How one thing can be two things at once?. Besides the same inheritance gain is very small in almost all scenarios, mainly if one considers that the composition should be preferred.

In general we just want to add behavior, this can be done with interface, especially when it is possible to have code in the interface which gives in C++, Java 8 and C# 8 or languages they have traits.

When the intention is to actually use more than one class in almost all situations it is possible to do this by hierarchy, that is, one class inherits from the other and this class is available with the members of the two for other classes to inherit. It’s all about understanding what the class means, what its role, what it is in the project.

In this example it seems that the solution is Entidade inherit from MonoBehavior.

Alternative

But I don’t know what these classes do. I don’t like the way these Engines games are engineered. A Enemy is a MonoBehavior? No, right? It’s something weird. So shouldn’t have an inheritance there. Or should make a composition so that Enemy has the MonoBehavior or this should be just an interface to add behavior to the desired class.

And Entidade is what? It seems to me that a Enemy may be a Entidade, but since I didn’t see the implementation of it, I don’t know. It may not have any function or add anything to the object, there a interface is more than adequate, especially if it’s just one Marker, which may be the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.