SceneTree.quit
2026/4/14大约 4 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — SceneTree.quit
SceneTree.quit
定义
quit 就像电脑上的"关机"按钮——你一按,整个游戏就关闭了。它会通知场景树"游戏结束了",场景树会按照正常流程关闭所有节点、释放资源,然后退出程序。
打个比方:你在一个商场里,quit 就是"商场关门了,请所有顾客和商家离开"。所有节点(顾客)会按顺序收到通知,做好清理工作后离开,最后商场大门关闭。
这是游戏开发中最基本的"退出游戏"方法。通常在"退出游戏"按钮、按下 ESC 键、或者游戏结束时调用。
函数签名
C#
public void Quit(int exitCode = 0)GDScript
func quit(exit_code: int = 0) -> void参数说明
| 参数 | 类型 | 必需 | 默认值 | 说明 |
|---|---|---|---|---|
exitCode | int | 否 | 0 | 退出码。0 表示正常退出,非零值表示异常退出。对玩家来说没有区别,但可以被外部程序(如脚本、启动器)用来判断游戏是否正常关闭 |
返回值
无返回值(void)。调用后游戏会立即开始退出流程。
代码示例
基础用法:退出游戏
C#
public override void _Ready()
{
// 正常退出游戏
GetTree().Quit();
// 等价于
GetTree().Quit(0); // 退出码 0 = 正常退出
// 注意:上面这行之后的代码不会执行!
GD.Print("这行不会被打印");
}GDScript
func _ready():
# 正常退出游戏
get_tree().quit()
# 等价于
get_tree().quit(0) # 退出码 0 = 正常退出
# 注意:上面这行之后的代码不会执行!
print("这行不会被打印")实际场景:通过按钮和快捷键退出游戏
C#
using Godot;
public partial class MainMenu : Control
{
public override void _Ready()
{
// 获取退出按钮并连接点击信号
var quitButton = GetNode<Button>("VBoxContainer/QuitButton");
quitButton.Pressed += OnQuitButtonPressed;
}
public override void _Input(InputEvent @event)
{
// 按下 ESC 键退出游戏
if (@event.IsActionPressed("ui_cancel"))
{
GetTree().Quit();
}
}
private void OnQuitButtonPressed()
{
GD.Print("退出游戏...");
// 运行结果: 退出游戏...
GetTree().Quit();
}
}GDScript
extends Control
func _ready():
# 获取退出按钮并连接点击信号
var quit_button = $VBoxContainer/QuitButton
quit_button.pressed.connect(_on_quit_button_pressed)
func _input(event):
# 按下 ESC 键退出游戏
if event.is_action_pressed("ui_cancel"):
get_tree().quit()
func _on_quit_button_pressed():
print("退出游戏...")
# 运行结果: 退出游戏...
get_tree().quit()进阶用法:带确认对话框的退出和退出码
C#
using Godot;
public partial class PauseMenu : Control
{
private ConfirmationDialog _exitDialog;
public override void _Ready()
{
// 创建退出确认对话框
_exitDialog = new ConfirmationDialog();
_exitDialog.Title = "退出游戏";
_exitDialog.DialogText = "确定要退出游戏吗?未保存的进度将丢失。";
_exitDialog.OkButtonText = "退出";
_exitDialog.CancelButtonText = "继续游戏";
AddChild(_exitDialog);
_exitDialog.Confirmed += OnExitConfirmed;
}
public override void _UnhandledInput(InputEvent @event)
{
// 按 ESC 打开暂停菜单(不直接退出)
if (@event.IsActionPressed("ui_cancel"))
{
if (!_exitDialog.Visible)
{
_exitDialog.PopupCentered();
}
else
{
_exitDialog.Hide();
}
}
}
private void OnExitConfirmed()
{
GD.Print("玩家确认退出游戏");
// 运行结果: 玩家确认退出游戏
// 正常退出
GetTree().Quit(0);
}
// 在其他地方遇到致命错误时使用异常退出码
private void OnCriticalError(string errorMsg)
{
GD.PrintErr($"致命错误: {errorMsg}");
// 运行结果: 致命错误: 无法加载关键资源
// 使用非零退出码表示异常退出
GetTree().Quit(1);
}
}GDScript
extends Control
var _exit_dialog: ConfirmationDialog
func _ready():
# 创建退出确认对话框
_exit_dialog = ConfirmationDialog.new()
_exit_dialog.title = "退出游戏"
_exit_dialog.dialog_text = "确定要退出游戏吗?未保存的进度将丢失。"
_exit_dialog.ok_button_text = "退出"
_exit_dialog.cancel_button_text = "继续游戏"
add_child(_exit_dialog)
_exit_dialog.confirmed.connect(_on_exit_confirmed)
func _unhandled_input(event):
# 按 ESC 打开暂停菜单(不直接退出)
if event.is_action_pressed("ui_cancel"):
if not _exit_dialog.visible:
_exit_dialog.popup_centered()
else:
_exit_dialog.hide()
func _on_exit_confirmed():
print("玩家确认退出游戏")
# 运行结果: 玩家确认退出游戏
# 正常退出
get_tree().quit(0)
# 在其他地方遇到致命错误时使用异常退出码
func on_critical_error(error_msg: String):
push_error("致命错误: %s" % error_msg)
# 运行结果: 致命错误: 无法加载关键资源
# 使用非零退出码表示异常退出
get_tree().quit(1)注意事项
- 调用后游戏会立即开始退出流程:
Quit()后面的代码可能不会执行。引擎会进入关闭流程,依次触发各节点的_ExitTree()、NotificationWMCloseRequest等通知。 - Web 平台上的行为不同:在浏览器中运行时(HTML5 导出),
Quit()通常不会关闭浏览器标签页,而是回到初始加载页面或显示"游戏已结束"的画面。浏览器出于安全考虑不允许网页自行关闭。 - 移动端不需要手动调用:在 Android 和 iOS 上,用户通过系统的"返回"或"滑动手势"来关闭应用。你通常不需要在移动端游戏中提供"退出"按钮。
- 退出码的用途:
exitCode = 0表示正常关闭,非零值表示异常。这主要在以下场景有用:- 命令行启动游戏时,脚本可以通过退出码判断游戏是否正常结束
- 自动化测试框架根据退出码判断测试通过/失败
- 在编辑器中的效果:在 Godot 编辑器中运行时调用
Quit()会停止当前的游戏预览,回到编辑器界面,不会关闭整个 Godot 编辑器。 - C# 差异:C# 中方法名用 PascalCase(
Quit),GDScript 中用 snake_case(quit)。
