用途
本技能提供 Godot 4.x 的调试工作流与常见问题解决方案,帮助开发者在编辑器与运行时定位脚本错误、资源加载失败、信号连接异常、性能瓶颈以及导出/构建错误。
主要能力
输出方法
print():通用输出,可在编辑器输出窗口过滤。printf():格式化输出,例如printf("Player %s has %d health", player_name, health)。push_error():以红色显示,包含文件与行号,例如push_error("Failed to load resource: ", resource_path)。push_warning():以黄色显示,用于非致命问题,例如push_warning("Missing animation for state: ", state)。load()失败时返回 null,使用前需检查:
var data = load(risky_path)
if data == null:
push_error("Could not load resource at: %s" % risky_path)
- 也可使用
ResourceLoader获取错误码:
var err, data = ResourceLoader.load_interactive(risky_path)
if err != OK:
push_error("Failed to load: %s (error code %d)" % [risky_path, err])
编辑器输出窗口
通过 Window > Debugger 或 Debug > Start Debugging 访问。输出面板显示:标准 print 输出(灰色)、警告(黄色)、错误(红色,含文件/行信息)、自定义日志消息。
调试器面板
内置调试器功能包括:
- Scene Tree — 当前场景的可视化树,可检查属性。
- Resources — 已加载资源列表,用于检查泄漏。
- Profiler — CPU 与 GPU 使用分解。
- Remote Transform — 在运行中的场景里编辑节点属性。
- Network Profiler — 监控多人游戏流量。
远程调试:以调试模式(F6)运行时,编辑器会自动连接。可通过 get_node(path) 访问远程节点。
性能分析工作流
CPU Profiler:在编辑器中选择 Profile > Start Profiling,然后查看 Profile > CPU Usage 或 Profile > Frame Time Graph。关键指标包括:帧时间(60fps 应低于 16.67ms)、物理 FPS(通常应为 60)、脚本时间(GDScript/C# 代码耗时)、绘制调用数(每帧 GPU draw call 数量)。
Memory Profiler:Profile > Memory Usage,检查资源数量是否随时间增长、未释放的场景实例、未卸载的大纹理。
GPU Profiler:在 Project Settings > Rendering > Debug > GPU Profiling 设为 On,然后查看 Profile > GPU Usage。
常见错误模式与修复
- "Script class 'XXX' does not exist in current project":重命名/移动后自动加载脚本引用断裂。修复:在 Project Settings > Autoload 中重新添加自动加载。
- "Cannot instantiate non-imported scene: res://...":场景文件未保存或路径错误。修复:在编辑器中打开并保存场景,验证路径。
- "Invalid get index 'XXX' (on base: 'Node2D')":访问节点上不存在的属性。修复:检查节点类型与可用属性,使用
has_method()或is()检查。 - "Failed to connect signal":方法名不匹配或信号不存在。修复:验证源节点上信号存在,检查方法签名匹配。
导出/构建错误
- "Export failed with code -1":缺少导出预设配置。修复:重新创建导出预设。
- "Android build failed":SDK/NDK 路径错误。修复:检查 Project Settings > Android。
- "HTML5 export failed":未安装 Emscripten。修复:安装 Godot HTML5 导出模板。
- "Windows export failed":未安装 MSVC。修复:安装 Visual Studio Build Tools。
网络调试
检查连接状态与对等节点数量:
print("Connected: ", Network.multiplayer.is_connected_to_server())
print("Peer count: ", Network.multiplayer.get_peers().size())
记录 RPC 调用:
func _rpc_debug_message(msg):
print("[RPC] ", msg)
使用编辑器中的 Profile > Network Profiler 监控网络流量。延迟补偿调试可记录时间信息:
print("Server time: ", get_tree().get_network_unique_id())
print("Local latency: ", Network.multiplayer.get_connection_status())
Godot 4.7 调试说明
- Perfetto tracing — 新的内置 Android 性能分析器,用于详细性能分析。
- 编辑器日志改进 — 错误消息提供更多上下文。
- tween_await() 调试 — 在 C# 调试器中使用 await 断点调试动画序列。
调试工具函数
class_name DebugUtils extends Node
static func draw_rect_debug(rect: Rect2, color: Color, lifetime: float = 0.0):
# Draw debug rectangle (use with CanvasItem.draw_rect)
pass
static func log_tree(node: Node, indent: int = 0):
# Print scene tree for debugging
var prefix = " ".repeat(indent)
print(prefix + node.name, " (", node.get_class(), ")")
for child in node.get_children():
log_tree(child, indent + 1)
static func check_node_ready(node: Node):
# Verify node is ready before accessing children
if not node.is_inside_tree():
push_error("Node not in tree: ", node.name)
return node.is_inside_tree()
MCP 桥接工具(可选 — 实时编辑器集成)
当 MCP 桥接运行时(Phase 2),以下工具提供实时调试能力:
debugger_output— 读取运行场景的实时调试器输出。output_log— 读取 Godot 输出日志,支持级别过滤(error/warning/info)。scene_run/scene_stop— 启动和停止场景以测试修复。pause_resume— 暂停执行以在断点处检查状态。script_validate— 在不离开智能体工作流的情况下验证脚本。scene_tree— 检查运行时场景层级,查找缺失节点。
注意:MCP 工具需要 Godot 编辑器运行并启用 MCP 插件。技能可独立工作,无需桥接。
适用场景
适用于 Godot 4.x 项目的日常调试、性能优化、多人网络问题排查以及导出构建错误定位。
限制
MCP 桥接工具为可选功能,需编辑器运行并启用 MCP 插件;其余调试能力基于 Godot 编辑器内置功能。