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!")
in Engine by (651 points)
edited by

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

by (1,035 points)
selected by

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.
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 webmaster@godotengine.org with your username.