This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+6 votes

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!

in Engine by (18 points)

1 Answer

+6 votes

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
    .destroy()
by (470 points)

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.

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

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.

Edited:

This information WAS included in the manual:
https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/gdscript_basics.html

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

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.