0 votes

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!")
asked Jul 23, 2019 in Engine by JulioYagami (603 points)
edited Jul 24, 2019 by JulioYagami

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

That's rewritten.

1 Answer

+1 vote
Best answer

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").

answered Jul 24, 2019 by BraindeadBZH (1,016 points)
selected Jul 27, 2019 by JulioYagami

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

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

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 How to use this Q&A? before posting your first questions.