|Topic was automatically imported from the old Question2Answer platform.
My question is pretty straight forward : I have a parent class, I have a child class, I need to override getter and setter in the child class of a parameter defined in the parent class without changing the getter and setter of the parent class.
Here is an exemple, I need to create a
CustomCamera2D class for my project which inherit
Camera2D. I need to apply changes to parameters defined in
CustomCamera2D each time the zoom parameter defined in
Camera2D changes. Therefore my first thought was to override the zoom setter in my
CustomCamera2D class to apply the changes
Here is a short exemple code I wrote and tested : custom_camera_2d.gd
The problem is my code doesn’t work, I dont have any error or warning but
apply_change() method is never reached. Either the engine doesn’t allow to override child getters and setters or I am doing somthing wrong in my code
Note : I tested changing the zoom value in the inspector assuming the godot editor is calling the setter and getter of a parameter when editing values in the inspector
Edit : new pastebin link with the
tool keyword at the start of the file
If you want the setters and getters of exported variables to be called in the Editor, you need too make the script a tool script (by putting the keyword “tool” somewhere in it, generally at the top). In your case, you probably need both the parent and the child script to be tools.
Other than that, your reasoning seems sound. The parent defines a function to be called on set, and the child overrides that function.
If you are doing a lot of setget, you might want to look into Object._set() and _get() too. They are practical when you have many variables with similar setters and getters.
Sorry, I understood both parent and child where Scripts. It appears that overriding set_zoom does nothing, you can use _set instead:
func _set(property: String, value) -> bool:
if property == "zoom":
zoom = value
PS: Please include your code directly in your question instead of linking to it, when possible. You can either use the builtin buttons, or select the code in Godot, press tab to indent it, and paste with the extra indentation level, for markdown to recognize it as code.
Oh sorry I forgot the
tool keyword in my pastebin but it was there during my testing and it was not working.
I imagine this :
In your case, you probably need both the parent and the child script to be tools
is the reason why it doesn’t work, I don’t understand perfectly how the engine is working and I’ve never looked in the source files but my guess is
Camera2D doesn’t have the toolmode enabled