Both of the syntaxes that do not work seem to work fine on my end (just tested with a button from my game).
Your working code includes a null check using the ? operator, which makes me wonder what you mean by “working”: is the signal actually connected, or just the code not throwing any error? The ? sign will silently ignore the Connect call if _playButton is null, so the code may seem to work but the signal is actually not connected. I know you’ve mentionned checking for null and I’m probably explaining something you know already, but I don’t see any other reason for your code not working that _playButton being null.
Considering your initial not working codes, I guess you don’t want your button to be null anyway, so you could write something like this:
And that does not make sense anyway; you’re looking to connect a different signal based on the button being null or not, but how would the PushError be connected to the button if it is null?
Ternary operators are not always the way to go.
Thank you all for your quick replies!
The nullable operator was a remainder of a previous refactor and I was checking for a null value on the _playButton variable before.
I actually found the issue and it had nothing to do with the signal code. Instead I found the issue inside my scene management code:
if (_currentScene is not null)
{
_currentScene.QueueFree();
Logger.Info("Freed old main scene");
}
PackedScene scene = ResourceLoader.Load<PackedScene>(scenePath);
_currentScene = scene.Instantiate();
AddChild(_currentScene);
Logger.Info("Set new Main Scene.");
AddChild somehow triggered the _ExitTree callback inside my class and disconnected the signal. Now I’m connecting the signal in the _EnterTree method and everything seems to work fine.