Advertisement
otorp2

Get input from an instanced scene

Jul 15th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1.  
  2. Get input from an instanced scene
  3. 0 votes
  4.  
  5. Hello!
  6.  
  7. I created a scene to use it as a custom Popup dialog.
  8. Its structure is:
  9.  
  10. -Panel
  11. --TextureFrame
  12. --Label1
  13. --Label2
  14. --Button1
  15. --Button2
  16.  
  17. 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?
  18.  
  19. Greetings
  20.  
  21. gdscript scenes instance nodes button
  22.  
  23. asked 3 hours ago in Engine by Not_a_Robot (47 points)
  24. 1 Answer
  25. +1 vote
  26. Best answer
  27.  
  28. When you have to communicate with the parent scene, a nice approach is to use signals.
  29. So you can have a script in your Panel that basically exposes signals this way (partial code):
  30.  
  31. signal choice1_selected
  32. signal choice2_selected
  33.  
  34. func _on_Button1_pressed():
  35. emit_signal("choice1_selected")
  36. close()
  37.  
  38. func _on_Button2_pressed():
  39. emit_signal("choice2_selected")
  40. close()
  41.  
  42. Then in the scene that contains this panel, you can listen to those signals by connecting a function to them.
  43.  
  44. 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.
  45. answered 3 hours ago by Zylann (1,566 points)
  46. selected 1 hour ago by Not_a_Robot
  47.  
  48. Just for possible future searches:
  49. In the scene that contains the panel:
  50.  
  51. get_node("Panel").connect("choice1_selected", self, "method_to_call")
  52.  
  53. And yes Zylann, I asked this question because I thought it was a bit awful to connect the buttons directly.
  54.  
  55. Thank you very much! :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement