godotengine / godotengine/godot
Building C# solution leads to errors if signals are connected in the editor
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
v4.3.stable.mono.official [77dcf97d8]
### System information
Godot v4.3.stable.mono - Windows 10.0.19045 - Vulkan (Forward+) - dedicated AMD Radeon RX 6700 XT (Advanced Micro Devices, Inc.; 32.0.11027.2001) - AMD Ryzen 5 7600X 6-Core Processor (12 Threads)
### Issue description
I have a CustomNode class that holds data in a CustomResource class. When a value of this Resource is changed in the editor, I want to execute code. However, subscribing to the Resource "changed" event, leads to errors if the C# solution is rebuild and the scene is closed.
### Steps to reproduce
**Setup**
Create a Node derived [Tool] script with a resource field. Create a Resource derived [Tool] class with a property that calls emit_changed() in the setter. In the CustomNode class, subscribe to the Resource's changed event in _ready. I'm using += and -= to connect and disconnect the signal.
**Reproducing the error**
- Open the scene (CustomNode subscribes to CustomResource's changed event)
- Rebuild C# solution
- Signal is unconnected after deserialization
- Close or reload scene -> error: Attempt to disconnect a nonexistent connection from ''. Signal: 'changed', callable: 'Delegate::Invoke'.
- Rebuild C# solution again -> error System.ObjectDisposedException: Cannot access a disposed object. Object name: 'CustomNode'. -> failed to unload assemblies (related issue https://github.com/godotengine/godot/issues/78513)
**Workaround**
It seems this error can be worked aroud by
- Using ISerializationListener to un- and resubscribe to the signal before and after serialization
- Using _Notification to unubscribe from the signal on the "predelete" notification
Note this workaround relies on the undocumented ISerializationListener which apparently should not be used: https://github.com/godotengine/godot-docs/issues/10615
**Variant without workaround**
The error also occurs when connecting to the signal in a CustomNode CustomResource property, as [suggested in the manual](https://docs.godotengine.org/en/stable/tutorials/plugins/running_code_in_the_editor.html). When using the property, the signal is automatically reconnected after the build as the setter is called. However, the "predelete" workaround somehow doesn't prevent the error when the scene is closed.
**Related Issues**
https://github.com/godotengine/godot/issues/84394 (possible duplicate)
https://github.com/godotengine/godot/issues/70026
https://github.com/godotengine/godot/issues/70414
Informative issue on the (de)serialization process: https://github.com/godotengine/godot-proposals/issues/9001
### Minimal reproduction project (MRP)
[signal-errors.zip](https://github.com/user-attachments/files/18672641/signal-errors.zip)
Contributor guide
Assessment
This issue has not been assessed yet.