duplicated preloaded scene with signals problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JeremiasDev

Hi, i have a problem, i have a button scene and a button script. The button is connected in the button script. I have a main scene with a main script and in the script i do:

onready var button = preload("res://button.tscn").instance()

func _ready():

    button.duplicate(1)
    button.global_position = # a random position (the position is not the problem)
    self.add_child(button)

the script works perfect it duplicate the button and set it on the perfect position but if i run the game i get a error the game dont stop but if i will press the button i get this error:

 E 0: 00: 04.882 emit_signal: Error calling method from signal 'pressed': 'Button (object.gd) :: _ on_Button_pressed ()': Method not found ..
   <C ++ source code> core / object.cpp: 1260 @ emit_signal ()

and i dont no what i can do.

please help me.

:bust_in_silhouette: Reply From: jgodfrey

Just guessing, but I assume you want to change the argument on your duplicate() call from 1 to 5. Looking at the docs here for those flags:

A value of 1 causes any attached signals to be duplicated in the new node. A value of 4 causes any attached scripts to be duplicated in the new node.

Since your original node has both a signal and an associated script, you want them both to be duplicated. To do that, just add the values together. So, 1 + 4 = 5.

So, try this:

button.duplicate(5)

Oh, I just noticed you’re not storing the result of your duplicate() call. That call returns a new node. So, you should be doing something like:

var b_copy = button.duplicate(5)
b_copy.global_position = # a random position (the position is not the problem)
self.add_child(b_copy)

jgodfrey | 2020-12-14 20:49

Thank you for your answer i think it works. Soo i thought duplicate (1 or 2) means how much I want to duplicate it. but thanks now i understand. Thank you very much

JeremiasDev | 2020-12-15 06:29