• Godot HelpGUI
  • How to remove a node and it's connections in GraphEdit

So i want to be able to remove nodes. I listen for _delete_nodes_request. Then i iterate over all children of the GraphEdit. For all children which are of Type GraphNode, i check if it is selected. If the node is a selected one, i remove it with node.queue_free(). That works, but i am not able to remove the connections from and to this node. I can get all the connections with GraphEdits get_connection_list(). The problem is that the entries use a string to identify each node. I don't know to match the selected nodes with the nodes from this list. I checked the code of GraphEdit and GraphNode on github, but found nothing useful.

tldl; i want to be able to remove selected nodes and their connections. I don't know how to find the String values for disconnect_node()s from and to parameters.

Disclaimer: I have not dealt with the GraphEdit/-Nodes in a long while. So take this with a grain of salt, but worth experimenting at least.

You could try using the bool selected property to see which GraphNode is/are selected and store their string title in a dict/list perhaps? Then disconnect_node() the each in the list?

Or it might be just easier to get_connection_list() and store it, then clear_connections(), node.queue_free() the selected ones, and then rebuild all the remaining relevant nodes connections based on stored data/list.

@Megalomaniak said: You could try using the bool selected property to see which GraphNode is/are selected and store their string title in a dict/list perhaps? Then disconnect_node() the each in the list?

The problem is that disconnect_node does not need the nodes title. The title is just a lable set for each node. So in my case, i have different node types and each type has a unique title. But you can have multiple nodes of the same type. For example take acalculator. There are nodes for constants and nodes for adding, subtracting and co. The title would be for example "add". But in the connections, the node is encoded es GraphNode@32 or something like that. So i have no clue how to match the selected nodes, which are instances of GraphNode, against those string values.

@Megalomaniak said: Or it might be just easier to get_connection_list() and store it, then clear_connections(), node.queue_free() the selected ones, and then rebuild all the remaining relevant nodes connections based on stored data/list.

This wouldn't be that nice as a solution, but even with this, i would have the same problem. I can get the connections. So i know that some node XYZ is connected to some other node ABC and which slots are connected. But i don't know what node types they are. So i can not know if node XYZ is for example an "add"-node or a "sub"-node.

I could listen for mouse-click and mouse-release events on the nodes. Then i could save those nodes and if a new connection is made, i could save the connection-data with those two nodes by myself. With that i could make a mapping between those strange strings and the real node instances. But that would be a really dirty hack and i think this should be easier. The engine has some way to identify a node-instance by this strange string and it has some way to get this string-value our of such a node-instance. So it must be possible for me to get this working. I think there is some easy thing i am missing.

But thanks for your input.

So extend the nodes to give them some meaningful tags or data that you can use to identify their type with? If they are given that tag during generation/instantiation then that should solve your problem, no?

@Megalomaniak said: So extend the nodes to give them some meaningful tags or data that you can use to identify their type with? If they are given that tag during generation/instantiation then that should solve your problem, no?

You got me wrong. The problem is that the data i get from get_connection_list() doesn't help me to identify the nodes. For a simple setup, the data i get is this: [{from:@GraphNode@20, from_port:0, to:GraphNode, to_port:0}, {from:@GraphNode@22, from_port:0, to:GraphNode, to_port:1}, {from:@GraphNode@24, from_port:0, to:@GraphNode@28, to_port:0}, {from:GraphNode, from_port:0, to:@GraphNode@28, to_port:1}, {from:@GraphNode@28, from_port:0, to:@GraphNode@30, to_port:0}] But what is @GraphNode@20? How do i know what node-instance that is?

Ok, it was easy as i thought. The weird string i get there is the instances name. So i can just check if node.get_name() is equal to the connections from or to value. I am still ne wto godot and have alot to learn. Thanks for your patience :)

Right, I expected(guess I shouldn't have) it to be obvious that those are instance names, hence why I misunderstood you I suppose. Glad to hear you got it figured out. :)

4 years later