Instantiated scene doesn't send signals
Asked Answered
L

6

0

I have a scene that is a button. If you click on it an instance of that scene (button) is added to that scene. So there are 2 buttons now. Pressing the 1 button creates more buttons. But pressing the instantiated buttons does nothing. What could be the problem? Something related to how signals are connected maybe?

The button's "pressed" signal is connected to the button's script.
func _on_button_pressed():
var child = preload("res://button.tscn").instantiate()
add_child(child)
X.children += 1 // global var to add buttons lower
child.position = Vector2 (0,40*X.children)

Lazar answered 22/1 at 18:40 Comment(0)
Q
0

Lazar Can you post your full script and node hierarchy.

Also, consider having a "container" node that will contain your buttons cause right now you are adding your buttons as childs of other buttons, I don't think it's a good idea.


Quarters answered 22/1 at 18:46 Comment(0)
L
0

Quarters There is no more code. And it is just a main node and a button scene as a child.

If I add children scenes by hand they work. Only instantiated in code ones don't.

Lazar answered 23/1 at 8:49 Comment(0)
S
0

Lazar If I add children scenes by hand they work. Only instantiated in code ones don't.

Do you have the signal connected through the inspector? Try to connect it in the code.

Signify answered 23/1 at 11:26 Comment(0)
L
0

Added to print X.children every second. Pressing the instantiated buttons doesn't add to X.children.
But if I add a process to move them in random directions they move. But they all move in the same direction. So they operate as the same node but only the manually placed ones have signals? I don't get it.

Lazar answered 23/1 at 11:33 Comment(0)
L
0

Saw an error " _parse_ext_resource: res://button.tscn:8 - Parse Error: [ext_resource] referenced non-existent resource at: res://Button.gd"
Replaced "preload("res://button.tscn").instantiate()" with "load("res://button.tscn").instantiate()"
Everything is working now.
Are you not supposed to preload scenes you want to instantiate?

Lazar answered 23/1 at 11:53 Comment(0)
Q
0

You preload at the beginning of your script with something like this :

@onready var button_scene = preload()

The point of preload is to not have to load the ressource while in gameplay (avoiding lag spikes)

Quarters answered 23/1 at 18:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.