+1 vote

I want to know how to instance a scene when a signal was detected

func _on_Point_score():

Thanks in advance!

in Engine by (172 points)

1 Answer

+1 vote

First you use preload to get a PackedScene with which you can instantiate your scene.

by (1,045 points)
NOTE: a correction of this code is below  

onready var scene = preload("res://path/to/scene")    

    func _on_Point_score():
        return scene.instance()

Why do return the value?

You have to add your scene as a node with add_child.

It returns with an object because you have to add that object to a node as child.

It is in the official documentation.

You're correct, but why not do the add_child in the signal handler. Also due to the way signal are working, there is no way to store the return value, so your instance will actually never be used.

Edit: I just realized that none of the comments above where made by the original user, so I was kinda confused with the answers.

I would add the child in the signal handler :). I just wanted to show in the quickest way possible how to instance a scene. This is the correction:

onready var scene = preload("res://path/to/scene")    

func _on_Point_score():
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 [email protected] with your username.