How would you deal with wanting to extend from 2 classes?

Godot Version

4.3

Question

I have something like this:
Unit is an abstract class
Tank is a unit.
Helicopter is a unit.

Unit is a scene.
Tank is also a scene.
Helicopter is also a scene.

The helicopter can simply freely move in any direction in 3D, so it does not need any terrain handling unlike the tank. I would like the tank to extend CharacterBody3D so that it has access to those physics functions and what not. But then, I also need the tank to extend from Unit as I would like to use polymorphism and access variables and functions from Unit.

In this case, how should this be handled in Godot?

You can definitely only extend one class.

You said Unit is an abstract class. Unit could extend Scene, and Tank could extend Unit. Would that work? Your Tank would then have methods of both Scene and Unit.

@pauldrewett I am not sure if I follow or maybe I do not understand this completely.

Unit could extend Scene, and Tank could extend Unit.

A scene is like a tree of nodes, and a class could be assigned to a node, right? How do you extend a scene? Would you mind point me to some examples?

Is the helicopter not a CharacterBody?
If so then the unit can be a CharacterBody.

If not then you have a great case for not shared inheritance. Clearly there would be two types of units, air and land.
When dealing with the limitation of single inheritance you are forced to stop at the greatest common denominator (so to speak). (This limitation is well regarded as it helps to prevent twisting up inheritance chains).

@sancho2 correct me if I am wrong as I am still learning the ropes in Godot. The reason that I avoided the helicopter to have CharacterBody3D is because I think it doesn’t need it. It only needs to fly around freely above the ground, so having CharacterBody3D is like giving it functions it does not need. I also have just seen some tutorials such as https://youtu.be/IuS-U3tDQ1c?si=MDzIwalFYfAISO9b so I felt unsure with the performance of CharacterBody3D. Or do you think having Helicopter extending CharacterBody3D is acceptable?

I am not a 3D guy so hopefully someone else will chime in.
But CharacterBody is meant to be used when a player controls the node movement.

I watched a bit of that video and in his case he has 100+ units in play.
If you are doing an RTS and are going to have 100+ helecopters then their might be some consideration for what he says there.
But then theoretically you are going to have 100+ Tank units as well and the issues he describes might crop up for them as well.
So you may have to consider not using CharacterBody or any of the bodies on them either.

What body type were you thinking for the helecopter? I think they are all going to come with some overhead so are you going with none of the body types?
In my opinion your choices are down to using the CharacterBody for all units or not using the CharacterBody for any units.

If this is an rts game all your units could extend Node3D. For tanks and other vehicles you might need to align them to the ground when moving up hills etc.
But do helicopters crash on the ground?

I once tested the performance difference for nav agents with node3d vs character body. Node3D performed ~2x better in my case.

Just thinking:
Having a character body and as a child the unit (node3d)? Sounds weird, I guess this is something I will test.

1 Like

@nines
Sorry, I chimed in here but perhaps, on re-reading your question, I may have got the wrong end of the stick. I will quietly bow out :slight_smile:

I would only add that in my very limited experience I have found a mix of composition and inheritance works very well. You could use composition for ground_movement and air_movement whilst extending unit for common things like health, speed etc.