0 votes

Hello!

I created a scene to use it as a custom Popup dialog.
Its structure is:

-Panel
--TextureFrame
--Label1
--Label2
--Button1
--Button2

I would like to know (from the root scene that will load this custom scene) when and which of the buttons was clicked. How do I approach this, please?

Greetings

in Engine by (86 points)

1 Answer

+1 vote
Best answer

When you have to communicate with the parent scene, a nice approach is to use signals.
So you can have a script in your Panel that basically exposes signals this way (partial code):

signal choice1_selected
signal choice2_selected

func _on_Button1_pressed():
    emit_signal("choice1_selected")
    close()

func _on_Button2_pressed():
    emit_signal("choice2_selected")
    close()

Then in the scene that contains this panel, you can listen to those signals by connecting a function to them.

You could connect directly to the buttons, but it's as wrong as accessing private members of a class IMO, and it would break if you change or rename the buttons later.

by (27,952 points)
selected by

Just for possible future searches:
In the scene that contains the panel:

get_node("Panel").connect("choice1_selected", self, "method_to_call")

And yes Zylann, I asked this question because I thought it was a bit awful to connect the buttons directly.

Thank you very much! :)

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.
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 webmaster@godotengine.org with your username.