I have a parent class named KeplerianOrbit2D wherein I declared a variable named true_anomaly. I am using this variable in multiple child classes, that is why I defined it here.
In one child class named CircularOrbit2D this variable is kept updated as the contents of the class changes, so directly accessing it always gives me an up to date value.
In another child class of KeplerianOrbit2D and a sibling class of CircularOrbit2D the variable true_anomaly is not updated to match the state of the orbit that the class represents. Thus whenever I want to access this parameter of the class, I need to update its value. I want to use a getter to do this, however all syntax documented for this requires re-declaring the variable, which is treated as an error:
The member “true_anomaly” already exists in parent class KeplerianOrbit2D.
In summary I have a variable x in two sibling classes A and B. The variable represents essentially the same thing, so it is defined in their parent. However I need a different getter function when accessing them. How can I achieve this?
You can only override virtual members.
In GDScript only functions can be virtual.
The easiest workaround is to decouple the getter/setter from the variable.
Unfortunately you can still access _myvar directly since there is no access modifiers in GDScript.
It is convention to add an underscore to the member variable to denote that it is private and should not be accessed directly
The problem with this solution is that this could silently fail if I mistype the function name. I could very well define a new function like so:
func get_my_varr() -> float:
return my_var * 12
it just doesn’t get called when accessing 'my_var´. It would be good if I had to explicitly use an ´override´ keyword.
I’ll still mark it as a solution as this is a good way to achieve my goal. I might raise a feature request for explicit override requirements, but I don’t know about programing paradigms that much to see if it is a bad or good idea.