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

I have multiple nodes, which implement and use identical methods.

So I would like to write them once in one script, and let all nodes, who need them "inherit?" from the script.

Is this possible? Or is there another solution for my problem?

in Engine by (107 points)

1 Answer

+14 votes
Best answer

That's possible. Replace the node inheritance by your script.

extends "res://your_script.gd"
by (548 points)
selected by

Thats great! But will I then be able to call functions like getnode("MyNode"), getpos() etc.? I mean I thought the extends Node2D part is for accessing these basic functions?

In my example if "yourscript.gd" extends from Node2D you have access to all Node2D functions in this script and also the functions you created in "yourscript.gd".
In the editor the "Classes" button shows the inheritance tree.

Thank you so much DriNeo!! :)

what happens if you override the _ready() or _init() functions. Is there a way to call the super class's function? Something like:

func _ready():
    set_fixed_process(true)
    super._ready()

edit: never mind! I found the answer! just call ._ready()to call the superclass' method

so:

func _ready():
        set_fixed_process(true)
        ._ready()

@batmanasb you don't have to call it (it would be called by default). Though, this might change in 3.0...

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.