+2 votes

I was checking out the tutorial and one thing I noticed, I can use

get_node("Button").connect("pressed", self, "_on_button_pressed")

to connect a button's press to a function right? That's how to do it programmatically. Now I add another button but this time, use the GUI approach of clicking the plug icon to link it to the function. The function works without problems however, I don't see

get_node("Button 2").connect("pressed", self, "_on_button_2_pressed")

anywhere on the script file. So where is this being stored? Are the GUI and the code based connections separate? Why is that? I was expecting it to add the same connect code beneath the one i manually typed.

And vice versa, the code I manually typed doesn't seem to register on the button when I check it out via the plug icon approach.

in Engine by (141 points)

2 Answers

0 votes

Good question. We can easily test it. I have a scene with node directional light called light. I will connect signal visibility_changedto function _on_light_visibility_changed inside script testing_scene.gd. Let's save the scene to .tscn file, so we can see what's inside. And here we go. There's our signal:

[connection signal="visibility_changed" from="light" to="." method="_on_light_visibility_changed"]

The dot next to "to=" is the path to spatial node (it's a parent of light) with my script. Thus signal connections made in editor are saved inside your scene file. Why? Maybe, it would be hard to synchronize the content of the script with editor GUI? Don't know for sure.

by (820 points)

Thanks for the answer! Hopefully someone from the Godot team chimes in with word on the why and if it's being addressed in the future. I somehow had the hunch it was getting stored in the scene itself. I just didnt want to change to .TSCN because I read that .SCN is better performance wise on the forums I think.

It's actually easy to see why: the code and the scene are separate things.

The GUI cannot edit the code, it can not even tell in which script it'll be added (since where it's connected and the function to execute don't need to be in the same place). You can even connect things in a scene without scripts (e.g. a button that calls hide() on a popup when pressed, you don't need script for that).

If you do it by code, there's no way for the GUI to know. In code you can even connect dynamically, so the editor would have to run the code to see what's the connection.

As a note, while it's true that .scn has better performance, .tscn files are converted to .scn on export. And this is for loading only, since they're the same in memory after loaded.

0 votes

I think it's saved on the scene file. Using the tscn format, it's easier to note something like this:

[connection signal="pressed" from="Button 2" to="." method="_on_Button_2_pressed"]

I do prefer the programmtically way though, it's clearer to me :P

by (130 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 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.