Created
August 23, 2024 15:34
-
-
Save programaths/3cd73104bdbb25fa6e2577954734d143 to your computer and use it in GitHub Desktop.
Detect a cycle in a GraphEdit in Godot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Attach to GraphEdit | |
func creates_cycle(from_node: StringName, to_node: StringName): | |
var from:GraphNode=get_node(NodePath(from_node)) | |
var to:GraphNode=get_node(NodePath(to_node)) | |
var links:={from_node:{to_node:true}} | |
for con in get_connection_list(): | |
var list:Dictionary=links.get_or_add(con.from_node,{}) | |
list[con.to_node]=true | |
var stack=[] | |
var visited={} | |
stack.push_back(from_node) | |
while not stack.is_empty(): | |
var current=stack.pop_back() | |
visited[current]=true | |
var linked:Dictionary=links.get(current,{}) | |
for child in linked.keys(): | |
if visited.get(child,false): | |
return true | |
stack.push_back(child) | |
return false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment