How can I make two graph nodes automatically linked to eachother?

Godot Version

4.6

Question

Hello

I am trying to create an undirected graph, that I can give neighbours in the Godot editor

Here is the relevant code for a vertex :

@tool
class_name node_map
extends Area2D


@export var node_neighbours: Array[node_map] = []

I want, when clicking on the “Add Element” and setting another node to that element, to have the current node added in the neighbour of the selected one

To use the example from Wikipedia, imagine if I add to the node 6 the node 4 as neighbour, the opposite happens ; the 4 adds 6 as neighbour

Note that I don’t want to have only graphic changes. The neighbours have a role in the gameplay

Thank you in advance

Typically you would use a setter function, but with array types it may not trigger. If it doesn’t then you can create an @export_tool_button to update the neighbours of your neighbours. Or handle a NOTIFICATION_EDITOR_PRE_SAVE notification via func _notification

You can connect to inspector’s property_edited signal. It should emit when an array is altered.

Ok, I didn’t know about EditorInspector, and it was a bit confusing for me, but I think I understand it

So, it’s only about the engine (and not a possible node), and adding an element in the array would trigger the signal, where the only thing I have to do now would be to do the connections when the signal is received. Am I right ?

Yes, you’d need to rebuild the graph connections in the signal handler.

Many thanks !