+1 vote

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 : customcamera2d.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

Godot version 3.2.3
in Engine by (23 points)
edited by

1 Answer

0 votes

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.

Edit:

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
        apply_changes()
        return true
    return false

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.

by (2,408 points)
edited by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.