Saving Signal connections programmatically

Godot Version

4.3

Question

If you create a Scene in the Editor, you can visually connect to methods in your script. When you open the corresponding .tscn file in a text editor, you can find those saved connections at the end of the file like for example:

[connection signal="focus_entered" from="." to="." method="_on_focus_entered"]
[connection signal="focus_exited" from="." to="." method="_on_focus_exited"]

Unfortunately, I can’t quite figure out how to do this programatically.

	var a = Control.new()
	var b = Control.new()
	a.add_child(b)
	b.focus_entered.connect(a.grab_focus) # Just connect some random 
	b.owner = a                           # method for test purposes
	
	var file = PackedScene.new()
	file.pack(a)
	
	ResourceSaver.save(file, "res://test.tscn" )

This does not save the connections. I added this little example to bring my point across, but I’ve also tried creating custom classes with custom signals. I can’t @export the signals though, so that doesn’t work either.

Any help would be appreciated.

Sorry, but if you have the signals connected by code, why do you want to also store them in the scene file? You already have them “stored” in your script.
I don’t see the point in programming the connection of the signals just so that they appear in the scene file. Wouldn’t it be much easier to do it through the editor?
I think I’m missing something.

I’m creating a plugin that can generate code and I wanna save/load whatever the user has created to/from a resource.

It would be useful to save the signals as well. I can also init the connections when the resource is loaded, but Godot seems to be capable of saving the signals. So why not?

Sorry, my English is not very good. Now I understand what you want.
Yes, there should be a way to do what you want.

Connect your signal with the CONNECT_PERSIST flag.

b.focus_entered.connect(a.grab_focus, CONNECT_PERSIST)
4 Likes

Thanks man,
I don’t know how I missed that.

1 Like