Godot Version
4.7
Question
Hello everyone. So I came from other engines and so far I’m loving the engine.
However there is something that is frustrating and I can’t seem to understand or fix in Godot.
Example code just for visualising.
public partial class Base : Node2D
{
[Export] public int SpawnChance = 100;
[ExportGroup("Random X")]
[Export] public int DifferentialX;
[Export] public int StepX;
[ExportGroup("Random Y")]
[Export] public int DifferentialY;
[Export] public int StepY;
}
public partial class Enemy : Base // This needs to be a CharacterBody2D
{
[Export] public IntRange Speed = new IntRange();
[Export] public double DecisionInterval = 1.0;
}
public partial class Sprite : Base // This needs to be a Sprite2D
{
[Export] public Texture2D[] Images;
[Export] public Sprite2D MySprite;
}
I want to have a Base class that is going to work as a Base object for everything. In this case it can be a Node2D.
Then I want to have a Tree for example that is Sprite2D and inherits from the Base.
Right here is the first problem. I can’t access directly the Sprite2D because Base is a Node2D. So I can change the Base to a Sprite2D (which later is not going to work) or I can attach the Base and the Tree script to 2 different nodes in the scene this way its going to work. However when I instantiate the Tree scene I don’t have access to the exported properties in the Base or Tree because it’s a child Node (yes I can’t activate to show the child, but I don’t want that it gets very confusing very fast).
And then I want to have an Enemy that also inherits from the Base and the Enemy is a CharacterBody2D.
So how can I inherit from the CharacterBody2D and the Base at the same time. In C# is not possible, I can use resources but they are instance shared and they make the code way messier. I can also you normal classes and instantiate them but then I need to export everything when I put it on a new “child” class which is more duplicated code.
I can also create an interface, however I duplicate the code again because when I add a property to the Base I need to add that to all the interfaces , so that’s not a solution.
I hope I could explain the problem. Can anyone explain me how to do this.
This is getting very frustrating since I’m making a procedural generated game this is essential.
Thank you