0 votes

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 gettree().changescene("res://GameSelection.tscn") in the script for the play button or the control node?

in Engine by (23 points)

1 Answer

+1 vote

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")
by (250 points)

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?

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)

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 buttonpressed():
gettree().changescene("res://GameSelection.tscn")
func button
pressed():
gettree().changescene("res://Options.tscn")
func button_pressed():
exit

does the button themselves require any scripting?

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.

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.