You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot4无法从GraphEdit连接列表获取节点问题求助

Godot4 获取GraphNode内TextEdit值的解决方法

问题根源

你遇到的报错是因为Godot4对GraphEdit的API做了重构:get_connection_list()返回的连接字典中,"from"和"to"字段存储的是节点的整数ID,而非Godot3中的节点名称/路径。直接把ID传给get_node(),自然会触发"需要NodePath而非字符串"的错误。

可行解决方案

用Godot4新增的get_node_by_id()方法,通过节点ID获取对应的GraphNode实例,再访问内部的TextEdit:

func _on_compile_pressed():
    var graph = $GraphEdit
    var connection_list = graph.get_connection_list()
    
    for connection in connection_list:
        # 通过ID获取对应GraphNode
        var source_node = graph.get_node_by_id(connection["from"])
        # 检查节点是否存在,避免空引用报错
        if source_node:
            # 获取内部TextEdit(确保路径和你的节点结构一致)
            var text_edit = source_node.get_node("TextEdit")
            if text_edit:
                # 获取文本(用get_text()取全部内容,get_selected_text()取选中文本)
                var text_value = text_edit.get_text()
                print(source_node.name, " 的TextEdit内容:", text_value)

额外说明

  • 若要获取GraphNode的名称,拿到source_node后直接访问source_node.name即可。
  • 确保GraphNode内部确实有一个名为TextEdit的子节点,路径要和实际场景树结构匹配(比如TextEdit在容器内的话,路径可能是"VBoxContainer/TextEdit")。
  • 建议保留空值判断,避免因节点结构变化或连接异常导致运行时错误。

内容的提问来源于stack exchange,提问作者MitchW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:47:03