Godot Version
v4.3.stable.official [77dcf97d8]
Question
Here’s an example:
I have a few scenes, and their inheritance is as follows:
Slime inherits from Actor
But now I want the Enemy to inherit from the Actor and Slime to inherit from the Enemy.
What should I do?
If Enemy and Slime have many subclasses each, many nodes in Actor, and their instantiated scenes are used in many places in the project, then choosing to modify the scene file is quite unrealistic.
This makes it difficult to guarantee the integrity of every node in the modified inheritance scenario.
It doesn’t sound like a missing feature, but rather an unusual way of trying to work with inheritance that doesn’t align with Godot’s design and best practices. If your game is not too far in development (eg: You don’t rely on this strange way of handling things with inheritance), then I’d recommend looking into composition over inheritance. Small scripts/nodes that can be used by any of your actors to share functionality without having to deal with Inheritance, which you can freely attach / detach or change in your scenes.
2 Likes
Actor
should contain all the behavior that ALL subclasses share.
Enemy
should be a subclass of Actor
if and only if Enemy is an Actor, and only if Enemy does all the things an Actor
does.
Slime
should be a subclass of Enemy
if and only if Slime
is always an Enemy
and always does all the things an Enemy
does.
I’m going to skip any discussion about whether that class hierarchy is a good one, and leave that decision up to you. However, your mentioning of many subclasses indicates that you may be confused about that very point.
Simply have Enemy
extend Actor
, and have Slime
extend Enemy
.
1 Like
I don’t think I’m overusing inheritance; my examples are just to show how tedious it can be to modify their inheritance relationships.
Although my focus is not on designing the project architecture, since you mentioned inheritance and composition, let’s take a look at the following example:
Slime still inherits from the Actor, and Slime needs all the members and child nodes in the Actor, so I think this inheritance is reasonable.
Also in the Slime scene, Slime has different behaviors than the Actor, such as jumping, so I added a node called JumpAbility to Slime. This is a functional node, also known as a component. It is responsible for handling the jumping behavior of its parent node.
If you have other ideas, please suggest your way to achieve; If you agree with me on this step, then let’s look at the following:
In the JumpAbility node (or component), there are some exported properties, which are the configuration of the jump behavior, such as the initial jump speed, the maximum jump height, and so on.
But now I want to change the inheritance relationship so that Slime inherits from Enemy, and Enemy inherits from Actor.
If you manually modify the scene file, these configuration items that have been modified in the inspector may be lost and returned to the default values.
This phenomenon can also occur on nodes inherited from Actor, and it is not limited to new nodes of subclasses.
That is, if the Actor has many nodes and their derived properties, then after modifying the inheritance relationship, it is difficult to guarantee that Slime inherits nodes and their derived properties from the Actor without losing data or other errors.
1 Like