+1 vote

So,we have this part of code:

var player=load("res://MovingIcon.tscn")
func ready():
var inst=player.instance()
add
child(inst)
pass

I can understand the part of : ( player=load("res://MovingIcon.tscn") ) and the part of
( var inst=player.instance() ), OR we can say also:

var player=load("res://MovingIcon.tscn")
func ready():
add
child(player.instance())
pass

But, what excactly does the instance() method?

Thank you.

in Engine by (219 points)

1 Answer

+1 vote
Best answer

If I'm not wrong, it creates a new object from the "template" loaded. Its equivalent is .new(), in exemple var alpha = Line2D.new(). Line2D is the template, and you create a new object from that.

by (86 points)
selected by

Hmmm...The new object is not created with the add_child?

Definitely not, add_child() just renders it on screen. You can try to make an object, add_child() it on the screen, modify it in some way, then call the function remove_child() and add_child() again. You will see that it's the same object, same reference, and the modifications are still here.
Even if the object isn't a child of the scene, it exists in the memory.
That's why you can change its name (or other properties) before adding it to the scene, but not before instancing it.

Oh i see...Now its clear!Thank you very much!

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