Where to add the script for changing scene

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

I want to change the scene when I press my buttons

for example when I press my play button, i want it to bring me to the game selection screen

for this to happen, do i add the code which is get_tree().change_scene(“res://GameSelection.tscn”) in the script for the play button or the control node?

:bust_in_silhouette: Reply From: Legorel

You don’t need to add a script to the button directly, you can connect the pressed signals of the button to a function in your main script (i suppose your control node):

var button = $Path/To/Button/Node

func _ready():
  button.connect("pressed", self, "button_pressed")

func button_pressed():
  gettree().changescene("res://GameSelection.tscn")

okay, so i go to my play button → node → pressed() and then the ‘connect a signal to a method’ screen pops up, what do i do?

i cant press connect (scene does not contain any script), and to what should i connect it to?

slyyy | 2021-04-24 19:41

If you connect the signal manually you can omit the first 4 lines in my answer
You can connect this signals to any script, i would recommend you to connect it to the main script (the one on your control node, which i think is the nearest to your scene’s root)

Legorel | 2021-04-24 19:57

so basically i just need to add a script for my control node, connect it manually and then add the code ?

func button_pressed():
gettree().changescene(“res://GameSelection.tscn”)

my menu has 3 buttons (play, options and exit), does this mean my control node script will have the following codes? its a bit weird
func button_pressed():
gettree().changescene(“res://GameSelection.tscn”)
func button_pressed():
gettree().changescene(“res://Options.tscn”)
func button_pressed():
exit

does the button themselves require any scripting?

slyyy | 2021-04-24 20:15

so basically i just need to add a script for my control node, connect it manually and then add the code ?

Yes

my menu has 3 buttons (play, options and exit), does this mean my
control node script will have the following codes? its a bit weird
func buttonpressed():
gettree().changescene(“res://GameSelection.tscn”)
func buttonpressed():
gettree().changescene(“res://Options.tscn”)
func button_pressed():
exit

You need all function to have different name, and each button connected to his corresponding function (for different behavior)

does the button themselves require any scripting?

No, the signal will be send automatically when the button is pressed.

Legorel | 2021-04-24 20:36