+1 vote

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

func _on_Point_score():
(instance_scene)

Thanks in advance!

asked Aug 28, 2019 in Engine by EnderCreeper1st (169 points)

1 Answer

+1 vote

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

answered Aug 28, 2019 by BraindeadBZH (1,020 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.
someNode.add_child(yourNewSceneInstance)

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():
    self.add_child(scene.instance())
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.