Is it possible to override my own functions?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By André Andrade
:warning: Old Version Published before Godot 3 was released.

Hey! I’d like to know if I can override my own functions.

I have created a base script that contains the function “destroy()” that is called when an object is to be removed. At the root script it only runs “queue_remov()” but for some of the thing I’d like to play a sound and maybe a animation.

I’m guessing that if I just declare the function again on the child script it will not run because when it runs the parent code it will be removed before it gets to the extended part.

Am I right? How can I override the function as to re-write it? Is it possible? Should I just create a destroy_fancy() function and be done with it?

P.S.: I’m kinda new to godot. I tried searching to see if anything like this was answared and didn’t find anything (just about overriding the engine functions) so if this was already asked before, sorry!

:bust_in_silhouette: Reply From: aozasori

In most cases, you’ll have to manually call the parent script’s function if you override it. Done by placing a dot before the name.

func destroy():
    #additional code

Are you sure?

I thought that only declaring it as “func destroy():” doesn’t just extends it executing the child’s “destroy()” after executing the one from the parent? because I’m pretty sure that if it’s not the case my game shouldn’t be even working at all.

André Andrade | 2017-09-29 23:30

The implicit parent calls are only for engine callbacks (ready, process, etc.), other methods need to have an explicit “super” call.

eons | 2017-09-30 17:26

I wasted a lot of time thinking the parent function was being called in my overridden function by default, this information SHOULD be included in the documentation.


This information WAS included in the manual:
GDScript reference — Godot Engine (stable) documentation in English

TGMG | 2020-06-25 18:11

Yes, it is a final note in the Inheritance section.

eons | 2020-06-25 20:48