C# inheritance advice

Hello folks,

I have an inheritance system for the game I’m developing that is intended for single player. I now want to move it to multiplayer. The problem is that I have to extend NetworkBehaviour at some point, but I don’t want all of my hierarchy to be networked.


Here is my current system:

(abstract class) MeshedObject : MonoBehaviour //contains static methods for creating meshes of certain shapes. Contains a reference to the object which holds the mesh color scheme. Contains abstract methods for getting sorting layer and how to use the color scheme. Uses Start and Update to set initial values.

(abstract class) Unit : MeshedObject //the base class for all military units in the game, which are all made with simple meshes. Contains basic ideas such as health, contains virtual functions to take damage and die. Holds static prefabs for its child units and an init function to load those.

(abstract class) TargetingUnit : Unit //the base class for all units that use targeting. Contains a GameObject[] target variable. Contains a virtual targeting function and way of getting a random target, virtual layer mask calculation functions. Uses Start and Update. This should be networked.

(abstract class) MeshedEffect : MeshedObject //the base class for simple game effects which also use meshes. I do not want these networked. Contains overrides for some of the MeshedObject specifications. Contains a "constructor" for setting a parent and such.

//these are the only "root" classes, other classes which extend these not shown.

Here is a possible solution to the networking problem:

(static class) MeshHandler //contains the static mesh-getting functions
(interface) IMeshedObject //contains the "variables" previously in MeshedObject but as interface get/sets. 
(abstract class) Unit : NetworkBehaviour, IMeshedObject
(abstract class) TargetingUnit : Unit
(abstract class) MeshedEffect : MonoBehaviour, IMeshedObject

However, there is the problem that the IMeshedObject can’t use Start and Update to initialize the “variables” anymore, I have to trust the implementing classes to do that. Can this be solved?

I’m not an expert on C# inheritance and am looking for advice on how I should implement this.

Thanks for any input,

Nomenokes

That’s generally the issue with inheritance and why inheritanc is a straight jacket. You generally want to keep your inheritance chains short. Unity uses a component based object composition approach. Are you sure that a Unit needs to be a “MeshedObject” or is it enough to have a MeshedObject?

Something like targetting clearly should be a seperate component. Keep in mind that you can create the network layer as seperate classes.

I like the Interface option.

Start and Update CAN be used, when implemented on a monobehavior.

You can create a static extension function for the interface, that takes as parameters: the interface reference, and a monobehavior reference, and write the code to handle the initialization and update stuff in there.

Now your monobehavior’s that implement the interface need only call those functions during start and update.

Obviously, this still requires you to depend upon the monobehavior to call those functions.
The advantage is that their inner workings don’t need to be dealt with, in EACH monobehavior.