Is it possible to have an abstract class that defined an Area3D Node and have the inherited classes use this Node?

Godot Version

4.3

Question

Suppose I have an abstract Enemy class that holds common attributes, common behaviors and an Area3D used for checking whether it got hit, is it possible to have the inherited class inherit the Area3D Node of the abstract class?

I have 10 enemy types, and they inherited from this abstract class, I see that they inherited the class and such, but not the Area3D node. Right now, each enemy type has its own Area3D and a Signal for whether a bullet entered its Area3D. While this works, this combination of ( body entered signal function, Area3D Node with Collision Shape 3D ) got repeated 10 times. And down the road, if I created more enemy types, this will also be repeating as well. And if I decide to change something here, this means I will have to modify this N times…

In Godot 4, what would be a better/preferable way to do this?

Your Enemy class can extend the Area3D class.
Unless it’s already inheriting any other class, then unfortunately it can’t inherit any more classes.
If you want your Enemy sub-classes to inherit an Area3D Node that is a child of the root Node in the Enemy scene - that’s not possible unfortunately. There is no concept of the “abstract” scene, only “abstract” classes. But class itself can’t hold Nodes, like a Node or a Scene would hold.

1 Like

There really isn’t abstract classes at all in GDScript.

But class itself can’t hold Nodes, like a Node or a Scene would hold.

Maybe you worded this too broadly? There is nothing stopping you from adding a node to an instance of a class.

2 Likes

In my opinion the best way to handle this is through composition.
There are a few GODOT specific tutorials in this regard including this one.

2 Likes