Virtual Scripts /(mistakenly 2 parents)?

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

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?

:bust_in_silhouette: Reply From: dc1a0

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 my_rigid_bodies.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/my_rigid_bodies.gd"

#add more code specific to the individual here

Also, note that you can use relative paths in the extends statement.

Bojidar Marinov | 2016-02-27 12:40