Godot Version
4.7.2
Question
Alongside our game project I’m authoring some editor tools to help the development process. I’m building an interaction system through signals. Having used many level editors for game modding over the years, I wanted to recreate a cool feature that could prove useful for our development. So the goal is to select one object in the 3D viewport, Ctrl-Alt-Click another object and thus connect a specific signal this way.
I pick the object to connect via ray cast from the mouse position into the 3D viewport. Disconnecting already existing signals is rather simple:
Node targetInteractable = hit.Owner;
// if already connected, disconnect
var connection = selectedNode.GetSignalConnectionList("InteractionFinished")
.FirstOrDefault(d =>
{
var callable = d["callable"].AsCallable();
return callable.Target == targetInteractable;
});
if (connection != null)
{
var signal = connection["signal"].AsSignal();
var callable = connection["callable"].AsCallable();
selectedNode.Disconnect(signal.Name, callable);
EditorInterface.Singleton.GetSelection().RemoveNode(selectedNode); //update inspector
EditorInterface.Singleton.GetSelection().AddNode(selectedNode);
return (int)AfterGuiInput.Stop;
}
This works and signals are properly updated in the inspector.
Now I’m not quite sure how to connect them. The signal/event names are always the same, but trying to call
selectedNode.Connect("InteractionFinished", new Callable(targetInteractable, "InteractionReceivedEvent"));
does… “something”. It connects the node somehow, as shown through my visual debugging, but the inspector doesn’t show that the signal I want to connect is actually connected. It’s not a refresh issue either because the action in-game doesn’t work.
One thing I notices is that I can’t really cast nodes to their script class via editor plugin. Getting the script itself via GetScript() on the node returns the correct C# class, just casting won’t work. Maybe that’s also the reason why signal connection doesn’t quite work the way I hope it would?
Anyone have any ideas on this?