单例(自动加载)

前言

Godot 的场景系统虽然强大而灵活,但有一个缺点:无法保存多个场景都需要的信息(例如玩家的分数或者背包)。

可以通过一些变通方法来解决此问题,但是它们有其自身的局限性:

  • 你可以使用“主”场景来把其它场景当作自己的子节点来加载和卸载。然而,这就意味着这些场景无法再独立正常运行。

  • 信息可以存储在磁盘的 user:// 下,然后由需要它的场景加载,但是经常保存和加载数据很麻烦并且可能很慢。

单例模式是解决需要在场景之间存储持久性信息的常见用例的实用工具。在我们的示例中,只要多个单例具有不同的名称,就可以复用相同的场景或类。

利用这个概念,你可以创建这样的对象:

  • 无论当前运行哪个场景,始终加载。

  • 可以存储全局变量,如玩家信息。

  • 可以处理切换场景和场景间的过渡。

  • 行为类似单例,因为 GDScript 在设计上就不支持全局变量。

自动加载的节点和脚本可以为我们提供这些特征。

备注

Godot 不会让自动加载变成单例设计模式里的那种“真”单例。如果需要,还是可以把它进行多次实例化的。

小技巧

If you’re creating an autoload as part of an editor plugin, consider registering it automatically in the Project Settings when the plugin is enabled.

自动加载

你可以创建自动加载(AutoLoad)来加载场景或者继承自 Node 的脚本。

备注

自动加载脚本时,会创建一个 Node 并把脚本附加上去。加载其它任何场景前,这个节点就会被加到根视图上。

../../_images/singleton.png

要自动加载场景或脚本,请从菜单中选择项目 > 项目设置,然后切换到自动加载选项卡。

../../_images/autoload_tab.png

你可以在这里添加任意数量的场景或脚本。列表中的每个条目都需要一个名称,会被用来给该节点的 name 属性赋值。使用上下箭头键可以操纵将条目添加到全局场景树时的顺序。与普通场景一样,引擎读取这些节点的顺序是从上到下的。

../../_images/autoload_example.png

这意味着,任何节点都可以访问并使用一个名为“PlayerVariables”的单例:

GDScriptC#

  1. var player_vars = get_node("/root/PlayerVariables")
  2. player_vars.health -= 10
  1. var playerVariables = GetNode<PlayerVariables>("/root/PlayerVariables");
  2. playerVariables.Health -= 10; // Instance field.

如果勾选了启用一栏(默认勾选),那么就可以直接访问该单例而不需要 get_node()

GDScriptC#

  1. PlayerVariables.health -= 10
  1. // Static members can be accessed by using the class name.
  2. PlayerVariables.Health -= 10;

请注意,访问自动加载对象(脚本、场景)的方式和访问场景树中的任何其他节点是一样的。实际上,如果你查看正在运行的场景树,就会看到自动加载的节点出现:

../../_images/autoload_runtime.png

警告

Autoloads must not be removed using free() or queue_free() at runtime, or the engine will crash.

自定义场景切换器

本教程将演示如何使用自动加载来构建场景切换器。基本的场景切换可以使用 SceneTree.change_scene()(详情请参见 使用 SceneTree)。然而,如果在更改场景时需要更复杂的行为,这个方法可提供更多功能。

首先,请下载模板 autoload.zip 并在 Godot 中打开。

该项目包含两个场景:Scene1.tscnScene2.tscn。每个场景都包含一个显示场景名称的标签和一个连接了 pressed() 信号的按钮。运行该项目时,它将从 Scene1.tscn 开始,但是按下按钮不会执行任何操作。

Global.gd

切换到 Script(脚本) 选项卡,然后新建一个名为 Global.gd 的脚本。确保它继承自 Node

../../_images/autoload_script.png

下一步是将此脚本添加到自动加载列表中。从菜单中打开项目 > 项目设置,切换到自动加载选项卡,然后单击文件浏览按钮或键入其路径:res://Global.gd。按添加将其添加到自动加载列表:

../../_images/autoload_tutorial1.png

现在,无论何时在项目中运行任何场景,该脚本都将始终加载。

返回到脚本,它需要在 _ready() 函数中获取当前场景。当前场景(带有按钮的那个)和 global.gd 都是根节点的子节点,但是自动加载的节点始终是第一个。这意味着根节点的最后一个子节点始终是加载的场景。

GDScriptC#

  1. extends Node
  2. var current_scene = null
  3. func _ready():
  4. var root = get_tree().root
  5. current_scene = root.get_child(root.get_child_count() - 1)
  1. using Godot;
  2. using System;
  3. public class Global : Godot.Node
  4. {
  5. public Node CurrentScene { get; set; }
  6. public override void _Ready()
  7. {
  8. Viewport root = GetTree().Root;
  9. CurrentScene = root.GetChild(root.GetChildCount() - 1);
  10. }
  11. }

现在我们需要一个用于更改场景的函数。这个函数需要释放当前场景,并将其替换为请求的场景。

GDScriptC#

  1. func goto_scene(path):
  2. # This function will usually be called from a signal callback,
  3. # or some other function in the current scene.
  4. # Deleting the current scene at this point is
  5. # a bad idea, because it may still be executing code.
  6. # This will result in a crash or unexpected behavior.
  7. # The solution is to defer the load to a later time, when
  8. # we can be sure that no code from the current scene is running:
  9. call_deferred("_deferred_goto_scene", path)
  10. func _deferred_goto_scene(path):
  11. # It is now safe to remove the current scene
  12. current_scene.free()
  13. # Load the new scene.
  14. var s = ResourceLoader.load(path)
  15. # Instance the new scene.
  16. current_scene = s.instance()
  17. # Add it to the active scene, as child of root.
  18. get_tree().root.add_child(current_scene)
  19. # Optionally, to make it compatible with the SceneTree.change_scene() API.
  20. get_tree().current_scene = current_scene
  1. public void GotoScene(string path)
  2. {
  3. // This function will usually be called from a signal callback,
  4. // or some other function from the current scene.
  5. // Deleting the current scene at this point is
  6. // a bad idea, because it may still be executing code.
  7. // This will result in a crash or unexpected behavior.
  8. // The solution is to defer the load to a later time, when
  9. // we can be sure that no code from the current scene is running:
  10. CallDeferred(nameof(DeferredGotoScene), path);
  11. }
  12. public void DeferredGotoScene(string path)
  13. {
  14. // It is now safe to remove the current scene
  15. CurrentScene.Free();
  16. // Load a new scene.
  17. var nextScene = (PackedScene)GD.Load(path);
  18. // Instance the new scene.
  19. CurrentScene = nextScene.Instance();
  20. // Add it to the active scene, as child of root.
  21. GetTree().Root.AddChild(CurrentScene);
  22. // Optionally, to make it compatible with the SceneTree.change_scene() API.
  23. GetTree().CurrentScene = CurrentScene;
  24. }

使用 Object.call_deferred(),第二个函数将仅在当前场景中的所有代码完成后运行。因此,当前场景在仍在使用(即其代码仍在运行)时不会被删除。

最后,我们需要在两个场景中填充空的回调函数:

GDScriptC#

  1. # Add to 'Scene1.gd'.
  2. func _on_Button_pressed():
  3. Global.goto_scene("res://Scene2.tscn")
  1. // Add to 'Scene1.cs'.
  2. public void OnButtonPressed()
  3. {
  4. var global = GetNode<Global>("/root/Global");
  5. global.GotoScene("res://Scene2.tscn");
  6. }

以及

GDScriptC#

  1. # Add to 'Scene2.gd'.
  2. func _on_Button_pressed():
  3. Global.goto_scene("res://Scene1.tscn")
  1. // Add to 'Scene2.cs'.
  2. public void OnButtonPressed()
  3. {
  4. var global = GetNode<Global>("/root/Global");
  5. global.GotoScene("res://Scene1.tscn");
  6. }

运行该项目,并测试您可以通过按下按钮来切换场景。

备注

当场景较小时,过渡是瞬时的。但是,如果你的场景比较复杂,则可能需要花费相当长的时间才能显示出来。要了解如何处理此问题,请参阅下一个教程:后台加载

另外,如果加载时间相对较短(少于 3 秒左右),你可以在改变场景之前,通过显示某种 2D 元素来显示一个“加载中图标”,然后在改变场景后隐藏它。这能让玩家知道场景正在载入。