The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

I'm instancing a button in my scene and I want to be able to connect the _pressed() signal to the main scene it's being instanced in. How can I do this without a singleton?

I could have the button emit a signal in the _ready function and then connect it in the main script, but I have no way to access the button without making it a singleton. Is there a way to access it only after the button is instanced?

in Engine by (207 points)

2 Answers

+2 votes

Sure you can't connect it via

$MyButton.connect("pressed", self, "my_function")

like explained on this page?

by (86 points)

No, MyButton is not a child (until it's instanced) so using $MyButton wouldn't work

I did solve it however by using:

MyButton = button.instance

this works the same way as $MyButton, at least how I've been using it

Nice you solved it!
So you managed to get a reference to the button node.

Quick question how exactly did you use this? did you just replace the $MyButton with button.instance or is that a line of code you added before the .conect? as i think i am trying to do something similar with instanced pickups form mob drops.

You should be able to do sth. like:

myButton = button.instance()
addChild(myButton)
myButton.connect("pressed", self, "my_function")
+1 vote

in Godot 4, you can try this:

var bullet = bulletScene.instantiate()
bullet.connect('hit', _on_player_hit)

And your callable function:

func _on_player_hit():
  print('Hit!')
by (62 points)
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.