Skip to content

GraphEdit

Inherits: Control < CanvasItem < Entity < Object

An editor for graph-like structures, using GraphNodes.

GraphEdit provides tools for creation, manipulation, and display of various graphs. Its main purpose in the engine is to power the visual programming systems, such as visual shaders, but it is also available for use in user projects.

GraphEdit by itself is only an empty container, representing an infinite grid where GraphNodes can be placed. Each GraphNode represents a node in the graph, a single unit of data in the connected scheme. GraphEdit, in turn, helps to control various interactions with nodes and between nodes. When the user attempts to connect, disconnect, or delete a GraphNode, a signal is emitted in the GraphEdit, but no action is taken by default. It is the responsibility of the programmer utilizing this control to implement the necessary logic to determine how each request should be handled.

Performance: It is greatly advised to enable low-processor usage mode (see OS.low_processor_usage_mode) when using GraphEdits.

Note: Keep in mind that Entity.get_children() will also return the connection layer node named _connection_layer due to technical limitations. This behavior may change in future releases.

Type Name Default
bool clip_contents true (overrides Control)
bool connection_lines_antialiased true
float connection_lines_curvature 0.5
float connection_lines_thickness 4.0
Array[Dictionary] connections []
Control.FocusMode focus_mode 2 (overrides Control)
GridPattern grid_pattern 0
bool minimap_enabled true
float minimap_opacity 0.65
Vector2 minimap_size Vector2(240, 160)
PanningScheme panning_scheme 0
bool right_disconnects false
Vector2 scroll_offset Vector2(0, 0)
bool show_arrange_button true
bool show_grid true
bool show_grid_buttons true
bool show_menu true
bool show_minimap_button true
bool show_zoom_buttons true
bool show_zoom_label false
int snapping_distance 20
bool snapping_enabled true
Dictionary type_names {}
float zoom 1.0
float zoom_max 2.0736003
float zoom_min 0.23256795
float zoom_step 1.2
Returns Method
PackedVector2Array _get_connection_line(from_position: Vector2, to_position: Vector2) virtual const
bool _is_in_input_hotzone(in_node: Object, in_port: int, mouse_position: Vector2) virtual
bool _is_in_output_hotzone(in_node: Object, in_port: int, mouse_position: Vector2) virtual
bool _is_node_hover_valid(from_node: StringName, from_port: int, to_node: StringName, to_port: int) virtual
void add_valid_connection_type(from_type: int, to_type: int)
void add_valid_left_disconnect_type(type: int)
void add_valid_right_disconnect_type(type: int)
void arrange_nodes()
void attach_graph_element_to_frame(element: StringName, frame: StringName)
void clear_connections()
Error connect_node(from_node: StringName, from_port: int, to_node: StringName, to_port: int, keep_alive: bool = false)
void detach_graph_element_from_frame(element: StringName)
void disconnect_node(from_node: StringName, from_port: int, to_node: StringName, to_port: int)
void force_connection_drag_end()
Array[StringName] get_attached_nodes_of_frame(frame: StringName)
Dictionary get_closest_connection_at_point(point: Vector2, max_distance: float = 4.0) const
int get_connection_count(from_node: StringName, from_port: int)
PackedVector2Array get_connection_line(from_node: Vector2, to_node: Vector2) const
Array[Dictionary] get_connection_list_from_node(node: StringName) const
Array[Dictionary] get_connections_intersecting_with_rect(rect: Rect2) const
GraphFrame get_element_frame(element: StringName)
HBoxContainer get_menu_hbox()
bool is_node_connected(from_node: StringName, from_port: int, to_node: StringName, to_port: int)
bool is_valid_connection_type(from_type: int, to_type: int) const
void remove_valid_connection_type(from_type: int, to_type: int)
void remove_valid_left_disconnect_type(type: int)
void remove_valid_right_disconnect_type(type: int)
void set_connection_activity(from_node: StringName, from_port: int, to_node: StringName, to_port: int, amount: float)
void set_selected(node: Entity)

begin_node_move

begin_node_move()

Emitted at the beginning of a GraphElement’s movement.

connection_drag_ended

connection_drag_ended()

Emitted at the end of a connection drag.

connection_drag_started

connection_drag_started(from_node: StringName, from_port: int, is_output: bool)

Emitted at the beginning of a connection drag.

connection_from_empty

connection_from_empty(to_node: StringName, to_port: int, release_position: Vector2)

Emitted when user drags a connection from an input port into the empty space of the graph.

connection_request

connection_request(from_node: StringName, from_port: int, to_node: StringName, to_port: int)

Emitted to the GraphEdit when the connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode is attempted to be created.

connection_to_empty

connection_to_empty(from_node: StringName, from_port: int, release_position: Vector2)

Emitted when user drags a connection from an output port into the empty space of the graph.

copy_nodes_request

copy_nodes_request()

Emitted when this GraphEdit captures a ui_copy action (Ctrl + C by default). In general, this signal indicates that the selected GraphElements should be copied.

cut_nodes_request

cut_nodes_request()

Emitted when this GraphEdit captures a ui_cut action (Ctrl + X by default). In general, this signal indicates that the selected GraphElements should be cut.

delete_nodes_request

delete_nodes_request(nodes: Array[StringName])

Emitted when this GraphEdit captures a ui_graph_delete action (Delete by default, or Backspace/Delete on macOS).

nodes is an array of node names that should be removed. These usually include all selected nodes.

disconnection_request

disconnection_request(from_node: StringName, from_port: int, to_node: StringName, to_port: int)

Emitted to the GraphEdit when the connection between from_port of from_node GraphNode and to_port of to_node GraphNode is attempted to be removed.

duplicate_nodes_request

duplicate_nodes_request()

Emitted when this GraphEdit captures a ui_graph_duplicate action (Ctrl + D by default). In general, this signal indicates that the selected GraphElements should be duplicated.

end_node_move

end_node_move()

Emitted at the end of a GraphElement’s movement.

frame_rect_changed

frame_rect_changed(frame: GraphFrame, new_rect: Rect2)

Emitted when the GraphFrame frame is resized to new_rect.

graph_elements_linked_to_frame_request

graph_elements_linked_to_frame_request(elements: Array, frame: StringName)

Emitted when one or more GraphElements are dropped onto the GraphFrame named frame, when they were not previously attached to any other one.

elements is an array of GraphElements to be attached.

node_deselected

node_deselected(node: Entity)

Emitted when the given GraphElement node is deselected.

node_selected

node_selected(node: Entity)

Emitted when the given GraphElement node is selected.

paste_nodes_request

paste_nodes_request()

Emitted when this GraphEdit captures a ui_paste action (Ctrl + V by default). In general, this signal indicates that previously copied GraphElements should be pasted.

popup_request

popup_request(at_position: Vector2)

Emitted when a popup is requested. Happens on right-clicking in the GraphEdit. at_position is the position of the mouse pointer when the signal is sent.

scroll_offset_changed

scroll_offset_changed(offset: Vector2)

Emitted when the scroll offset is changed by the user. It will not be emitted when changed in code.

PanningScheme

enum PanningScheme

SCROLL_ZOOMS = 0

Mouse Wheel will zoom, Ctrl + Mouse Wheel will move the view.

SCROLL_PANS = 1

Mouse Wheel will move the view, Ctrl + Mouse Wheel will zoom.

GridPattern

enum GridPattern

GRID_PATTERN_LINES = 0

Draw the grid using solid lines.

GRID_PATTERN_DOTS = 1

Draw the grid using dots.

activity

Color activity (color) = Color(1, 1, 1, 1)

connection_hover_tint_color

Color connection_hover_tint_color (color) = Color(0, 0, 0, 0.3)

connection_rim_color

Color connection_rim_color (color) = Color(0.1, 0.1, 0.1, 0.6)

connection_valid_target_tint_color

Color connection_valid_target_tint_color (color) = Color(1, 1, 1, 0.4)

grid_major

Color grid_major (color) = Color(1, 1, 1, 0.2)

grid_minor

Color grid_minor (color) = Color(1, 1, 1, 0.05)

selection_fill

Color selection_fill (color) = Color(1, 1, 1, 0.3)

selection_stroke

Color selection_stroke (color) = Color(1, 1, 1, 0.8)

connection_hover_thickness

int connection_hover_thickness (constant) = 0

port_hotzone_inner_extent

int port_hotzone_inner_extent (constant) = 22

port_hotzone_outer_extent

int port_hotzone_outer_extent (constant) = 26

grid_toggle

Texture2D grid_toggle (icon)

layout

Texture2D layout (icon)

minimap_toggle

Texture2D minimap_toggle (icon)

snapping_toggle

Texture2D snapping_toggle (icon)

zoom_in

Texture2D zoom_in (icon)

zoom_out

Texture2D zoom_out (icon)

zoom_reset

Texture2D zoom_reset (icon)

menu_panel

StyleBox menu_panel (style)

panel

StyleBox panel (style)

panel_focus

StyleBox panel_focus (style)

connection_lines_antialiased

bool connection_lines_antialiased = true

If true, the lines between nodes will use antialiasing.

connection_lines_curvature

float connection_lines_curvature = 0.5

The curvature of the lines between the nodes. 0 results in straight lines.

connection_lines_thickness

float connection_lines_thickness = 4.0

The thickness of the lines between the nodes.

connections

Array[Dictionary] connections = []

The connections between GraphNodes.

A connection is represented as a Dictionary in the form of:

Connections with keep_alive set to false may be deleted automatically if invalid during a redraw.

grid_pattern

GridPattern grid_pattern = 0

The pattern used for drawing the grid.

minimap_enabled

bool minimap_enabled = true

If true, the minimap is visible.

minimap_opacity

float minimap_opacity = 0.65

The opacity of the minimap rectangle.

minimap_size

Vector2 minimap_size = Vector2(240, 160)

The size of the minimap rectangle. The map itself is based on the size of the grid area and is scaled to fit this rectangle.

panning_scheme

PanningScheme panning_scheme = 0

Defines the control scheme for panning with mouse wheel.

right_disconnects

bool right_disconnects = false

If true, enables disconnection of existing connections in the GraphEdit by dragging the right end.

scroll_offset

Vector2 scroll_offset = Vector2(0, 0)

The scroll offset.

show_arrange_button

bool show_arrange_button = true

If true, the button to automatically arrange graph nodes is visible.

show_grid

bool show_grid = true

If true, the grid is visible.

show_grid_buttons

bool show_grid_buttons = true

If true, buttons that allow to configure grid and snapping options are visible.

show_menu

bool show_menu = true

If true, the menu toolbar is visible.

show_minimap_button

bool show_minimap_button = true

If true, the button to toggle the minimap is visible.

show_zoom_buttons

bool show_zoom_buttons = true

If true, buttons that allow to change and reset the zoom level are visible.

show_zoom_label

bool show_zoom_label = false

If true, the label with the current zoom level is visible. The zoom level is displayed in percents.

snapping_distance

int snapping_distance = 20

The snapping distance in pixels, also determines the grid line distance.

snapping_enabled

bool snapping_enabled = true

If true, enables snapping.

type_names

Dictionary type_names = {}

Dictionary of human-readable port type names.

zoom

float zoom = 1.0

The current zoom value.

zoom_max

float zoom_max = 2.0736003

The upper zoom limit.

zoom_min

float zoom_min = 0.23256795

The lower zoom limit.

zoom_step

float zoom_step = 1.2

The step of each zoom level.

_get_connection_line

PackedVector2Array _get_connection_line(from_position: Vector2, to_position: Vector2) virtual const

Virtual method which can be overridden to customize how connections are drawn.

_is_in_input_hotzone

bool _is_in_input_hotzone(in_node: Object, in_port: int, mouse_position: Vector2) virtual

Returns whether the mouse_position is in the input hot zone.

By default, a hot zone is a Rect2 positioned such that its center is at in_node.GraphNode.get_input_port_position()(in_port) (For output’s case, call GraphNode.get_output_port_position() instead). The hot zone’s width is twice the Theme Property port_grab_distance_horizontal, and its height is twice the port_grab_distance_vertical.

Below is a sample code to help get started:

_is_in_output_hotzone

bool _is_in_output_hotzone(in_node: Object, in_port: int, mouse_position: Vector2) virtual

Returns whether the mouse_position is in the output hot zone. For more information on hot zones, see _is_in_input_hotzone().

Below is a sample code to help get started:

_is_node_hover_valid

bool _is_node_hover_valid(from_node: StringName, from_port: int, to_node: StringName, to_port: int) virtual

This virtual method can be used to insert additional error detection while the user is dragging a connection over a valid port.

Return true if the connection is indeed valid or return false if the connection is impossible. If the connection is impossible, no snapping to the port and thus no connection request to that port will happen.

In this example a connection to same node is suppressed:

public override bool _IsNodeHoverValid(StringName fromNode, int fromPort, StringName toNode, int toPort)
{
return fromNode != toNode;
}

add_valid_connection_type

void add_valid_connection_type(from_type: int, to_type: int)

Allows the connection between two different port types. The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot() method.

See also is_valid_connection_type() and remove_valid_connection_type().

add_valid_left_disconnect_type

void add_valid_left_disconnect_type(type: int)

Allows to disconnect nodes when dragging from the left port of the GraphNode’s slot if it has the specified type. See also remove_valid_left_disconnect_type().

add_valid_right_disconnect_type

void add_valid_right_disconnect_type(type: int)

Allows to disconnect nodes when dragging from the right port of the GraphNode’s slot if it has the specified type. See also remove_valid_right_disconnect_type().

arrange_nodes

void arrange_nodes()

Rearranges selected nodes in a layout with minimum crossings between connections and uniform horizontal and vertical gap between nodes.

attach_graph_element_to_frame

void attach_graph_element_to_frame(element: StringName, frame: StringName)

Attaches the element GraphElement to the frame GraphFrame.

clear_connections

void clear_connections()

Removes all connections between nodes.

connect_node

Error connect_node(from_node: StringName, from_port: int, to_node: StringName, to_port: int, keep_alive: bool = false)

Create a connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode. If the connection already exists, no connection is created.

Connections with keep_alive set to false may be deleted automatically if invalid during a redraw.

detach_graph_element_from_frame

void detach_graph_element_from_frame(element: StringName)

Detaches the element GraphElement from the GraphFrame it is currently attached to.

disconnect_node

void disconnect_node(from_node: StringName, from_port: int, to_node: StringName, to_port: int)

Removes the connection between the from_port of the from_node GraphNode and the to_port of the to_node GraphNode. If the connection does not exist, no connection is removed.

force_connection_drag_end

void force_connection_drag_end()

Ends the creation of the current connection. In other words, if you are dragging a connection you can use this method to abort the process and remove the line that followed your cursor.

This is best used together with connection_drag_started and connection_drag_ended to add custom behavior like node addition through shortcuts.

Note: This method suppresses any other connection request signals apart from connection_drag_ended.

get_attached_nodes_of_frame

Array[StringName] get_attached_nodes_of_frame(frame: StringName)

Returns an array of node names that are attached to the GraphFrame with the given name.

get_closest_connection_at_point

Dictionary get_closest_connection_at_point(point: Vector2, max_distance: float = 4.0) const

Returns the closest connection to the given point in screen space. If no connection is found within max_distance pixels, an empty Dictionary is returned.

A connection is represented as a Dictionary in the form of:

For example, getting a connection at a given mouse position can be achieved like this:

get_connection_count

int get_connection_count(from_node: StringName, from_port: int)

Returns the number of connections from from_port of from_node.

get_connection_line

PackedVector2Array get_connection_line(from_node: Vector2, to_node: Vector2) const

Returns the points which would make up a connection between from_node and to_node.

get_connection_list_from_node

Array[Dictionary] get_connection_list_from_node(node: StringName) const

Returns an Array containing a list of all connections for node.

A connection is represented as a Dictionary in the form of:

Example: Get all connections on a specific port:

get_connections_intersecting_with_rect

Array[Dictionary] get_connections_intersecting_with_rect(rect: Rect2) const

Returns an Array containing the list of connections that intersect with the given Rect2.

A connection is represented as a Dictionary in the form of:

get_element_frame

GraphFrame get_element_frame(element: StringName)

Returns the GraphFrame that contains the GraphElement with the given name.

get_menu_hbox

HBoxContainer get_menu_hbox()

Gets the HBoxContainer that contains the zooming and grid snap controls in the top left of the graph. You can use this method to reposition the toolbar or to add your own custom controls to it.

Warning: This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their CanvasItem.visible property.

is_node_connected

bool is_node_connected(from_node: StringName, from_port: int, to_node: StringName, to_port: int)

Returns true if the from_port of the from_node GraphNode is connected to the to_port of the to_node GraphNode.

is_valid_connection_type

bool is_valid_connection_type(from_type: int, to_type: int) const

Returns whether it’s possible to make a connection between two different port types. The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot() method.

See also add_valid_connection_type() and remove_valid_connection_type().

remove_valid_connection_type

void remove_valid_connection_type(from_type: int, to_type: int)

Disallows the connection between two different port types previously allowed by add_valid_connection_type(). The port type is defined individually for the left and the right port of each slot with the GraphNode.set_slot() method.

See also is_valid_connection_type().

remove_valid_left_disconnect_type

void remove_valid_left_disconnect_type(type: int)

Disallows to disconnect nodes when dragging from the left port of the GraphNode’s slot if it has the specified type. Use this to disable a disconnection previously allowed with add_valid_left_disconnect_type().

remove_valid_right_disconnect_type

void remove_valid_right_disconnect_type(type: int)

Disallows to disconnect nodes when dragging from the right port of the GraphNode’s slot if it has the specified type. Use this to disable a disconnection previously allowed with add_valid_right_disconnect_type().

set_connection_activity

void set_connection_activity(from_node: StringName, from_port: int, to_node: StringName, to_port: int, amount: float)

Sets the coloration of the connection between from_node’s from_port and to_node’s to_port with the color provided in the activity theme property. The color is linearly interpolated between the connection color and the activity color using amount as weight.

set_selected

void set_selected(node: Entity)

Sets the specified node as the one selected.