+2 votes

I'm going to ask and answer this question for the points since I've answered it too many times.

A few times the question has mistakenly been "How Can I have 2 parents for a script/node?" This is obviously mistaken as a class/node CANNOT have two parents.

The better question I answered today was asked in Facebook which is the following:

Hey guys, a question about subclassing:
I'd like to be able to add script to an object on the scene, extending a class I wrote ( not one of built-in base classes ). I can't figure out where I can define them.

So how is this done?

in Engine by (223 points)

1 Answer

+2 votes
Best answer

The node system of Godot is basically complete OOP. Therefore, each node/script is considered an object, while scenes are collections of objects. You can make virtual scripts/ or children designed to be parents ealily by paying attention to the the first line with print. The one that starts with 'extends'

To make a virtual script you create a script extending the node of your choice and adding your code. For this example I'll use RigidBody2D we will call it myrigidbodies.gd:

extends RigidBody2D

add the rest of the code you would want every node that will use this

script as a parent to have 

Then, in the node you want to use the code you extended you would use the following:

extends "res://path/to/myrigidbodies.gd"

add more code specific to the individual here

by (223 points)
selected by
Also, note that you can use relative paths in the `extends` statement.
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.