DEV Community

Sunder Iyer
Sunder Iyer

Posted on

TIL Godot GraphEdit Gaffes

2021-03-11

The GraphEdit and GraphNodes sure have their...idiosyncrasies.

I got stumped for a bit because I wanted to remove all the GraphNodes in a GraphEdit to perform a clear so I did a children-loop remove.

# self is the graphedit control
for child in self.get_children():
    self.remove_child(child)
    child.queue_free()
Enter fullscreen mode Exit fullscreen mode

Yeah, don't do this. This is because the GraphEdit comes with children already - a GraphEditFilter and Control; you get hard crashes when you remove those and mess with the control gg ez.

I found this works better.

for child in self.get_children():
    if (child is GraphNode):
        self.remove_child(child)
        child.queue_free()
Enter fullscreen mode Exit fullscreen mode

Gonna make it a two-fer-one since I slept before writing an entry yesterday!
This one was a bit tricky but I learned you cannot set the position of children GraphNodes in a GraphEdit. Basically, I setup an auto-connecting GraphNode and wanted to position them nicely around the node it was connected to.
Intended Goal

Aboved: what I wanted (and eventually got)

In order to accomplish this, the offset property needed to be used and not position. This is because the GraphEdit sets the actual position of the GraphNode in order to set the proper scaled/scrolled position of it. The following C++ code shows this in action.

void GraphEdit::_update_scroll_offset() {
    // . . .
    Point2 pos = gn->get_position_offset() * zoom;
    pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
    gn->set_position(pos);
Enter fullscreen mode Exit fullscreen mode

What made this very trippy was setting the position worked(!) at one point but I suspect that removing all children in the GraphEdit allowed this to happen. Once I fixed that issue, the placement issue popped up. It got resolved but there's still more work to be done! Stay tuned!!

Top comments (0)