Extend vs. don't extend

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

So, I realised some Godot games have some scripts where extends SomeNode doesn’t appear and I wanted to know what difference does it make when instancing those scripts (extend vs. doesn’t extend).

Now, another question related to that one: what if I use add_child() with a script that doesn’t extend anything?

Example:

a.gd:

extends Node

var b = preload("res://b.gd").new()
var c = preload("res://c.gd").new()

func _ready():
    add_child(b)
    b.somefunc()
    add_child(c)
    c.somefunc()

b.gd:

func somefunc():
    print("I'm b!")

c.gd:

extends Node

var somevar = 123

func somefunc():
    print("I'm c!")

Sorry, probably for you English is difficult, but your question is not clear. Can you rewrite it? With more details?

BraindeadBZH | 2019-07-23 15:14

That’s rewritten.

JulioYagami | 2019-07-24 14:35

:bust_in_silhouette: Reply From: BraindeadBZH

Thanks for the effort, it is way clearer now.

In Godot a script equal a class. When a script does not extend anything simply mean that it is a class which inherit from nothing.

To be able to add a script with add_child, the script has to extend Node (or one of the sublclasses, i.e. all the classes available when you click “Add Node”).

What if I instance a script and the node I instanced it from is freed?

JulioYagami | 2019-07-27 22:18

A node always destroy all its children. If you want your instance to stay across multiple scenes, look for singleton.

BraindeadBZH | 2019-07-28 09:58