+1 vote

I noticed that if I have a gd script, which is then extended by another gd script where both define a _physics_process function, the extending script automatically calls the super version of _physics_process. Based on this GitHub issue, I would expect the super version to be called only when I explicitly call it with ._physics_process(). As this seems to not be the case, I have a few questions about this. Do all functions automatically call their super versions? If not, which functions do call their super versions? Why do they call their super versions? Lastly, is there a way not to call the super version?

in Engine by (112 points)

1 Answer

+2 votes
Best answer

In general, functions are called the way you would expect. However, some functions (such as _process or _physics_process are called by the engine using call_multilevel, which means it will call each method of the class hierarchy.
I am not sure what was the intent behind this, it's as if engine writers wanted to "simplify" the work of the script writers when inheritance is used, but personally I find it confusing because it is basically an exceptional and uncontrolled behavior.

by (29,090 points)
selected by

This is pretty close to what I expected. Do you happen to know if there's a list of functions that do this? And also if you can choose not to do this with these functions? I'm guessing you can't choose not to, as the reason these ones do preform this multilevel call is because it's a workaround to make them work, but if you happen to know otherwise, that would be great. Thank you!

After doing a quick search in the source code, I found these methods are called this way:

_notification
_enter_world
_exit_world
_process
_physics_process
_exit_tree
_gui_input

There is also _set, _get and _get_property_list mentionned in the doc: http://docs.godotengine.org/en/3.0/development/cpp/object_class.html?highlight=multilevel#binding-properties-using-set-get-get-property-list

Maybe also _init? Or even all special methods starting with _...

There was a discussion about this system a while ago, then the author closed the issue... even though more people added to the conversation afterwards ^^"
https://github.com/godotengine/godot/issues/6500

the same for _ready

In the Github thread, reduz commented on Sep 15, 2016:

callbacks get called at all levels, back when we had Squirrel
programmers used to forget calling the parent functions all the time
for callbacks they handled different and it was kind of a mess.

I've worked with an in-house game engine written in C++, and for some functions, forgetting to call the base implementation resulted in very bad behavior. Maybe the Godot engine devs made this design choice simply to avoid such situations?

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 Frequently asked questions and 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 [email protected] with your username.