单例(自动加载)

前言

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

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

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

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

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

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

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

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

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

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

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

备注

Godot won’t make an Autoload a “true” singleton as per the singleton design pattern. It may still be instanced more than once by the user if desired.

小技巧

如果你创建的自动加载是编辑器插件的一部分,请考虑在启用插件时将其自动注册到项目设置中

自动加载

You can create an Autoload to load a scene or a script that inherits from Node.

备注

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

../../_images/singleton.webp

To autoload a scene or script, start from the menu and navigate to Project > Project Settings > Globals > Autoload.

../../_images/autoload_tab.webp

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

../../_images/autoload_example.webp

If the Enable column is checked (which is the default), then the singleton can be accessed directly in GDScript:

GDScript

  1. PlayerVariables.health -= 10

The Enable column has no effect in C# code. However, if the singleton is a C# script, a similar effect can be achieved by including a static property called Instance and assigning it in _Ready():

C#

  1. public partial class PlayerVariables : Node
  2. {
  3. public static PlayerVariables Instance { get; private set; }
  4. public int Health { get; set; }
  5. public override void _Ready()
  6. {
  7. Instance = this;
  8. }
  9. }

This allows the singleton to be accessed from C# code without GetNode() and without a typecast:

C#

  1. PlayerVariables.Instance.Health -= 10;

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

../../_images/autoload_runtime.webp

警告

运行时绝对不能通过 free()queue_free() 去移除自动加载,否则引擎会崩溃。

自定义场景切换器

This tutorial will demonstrate building a scene switcher using autoloads. For basic scene switching, you can use the SceneTree.change_scene_to_file() method (see 使用 SceneTree for details). However, if you need more complex behavior when changing scenes, this method provides more functionality.

To begin, download the template from here: singleton_autoload_starter.zip and open it in Godot.

The project contains two scenes: scene_1.tscn and scene_2.tscn. Each scene contains a label displaying the scene name and a button with its pressed() signal connected. When you run the project, it starts in scene_1.tscn. However, pressing the button does nothing.

Creating the script

Open the Script window and create a new script called global.gd. Make sure it inherits from Node:

../../_images/autoload_script.webp

The next step is to add this script to the autoLoad list. Starting from the menu, open Project > Project Settings > Globals > Autoload and select the script by clicking the browse button or typing its path: res://global.gd. Press Add to add it to the autoload list:

../../_images/autoload_tutorial1.webp

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

Returning to the script, it needs to fetch the current scene in the _ready() function. Both the current scene (the one with the button) and global.gd are children of root, but autoloaded nodes are always first. This means that the last child of root is always the loaded scene.

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. public partial class Global : Node
  3. {
  4. public Node CurrentScene { get; set; }
  5. public override void _Ready()
  6. {
  7. Viewport root = GetTree().Root;
  8. CurrentScene = root.GetChild(root.GetChildCount() - 1);
  9. }
  10. }

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

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.instantiate()
  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_to_file() 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(MethodName.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 = GD.Load<PackedScene>(path);
  18. // Instance the new scene.
  19. CurrentScene = nextScene.Instantiate();
  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_to_file() API.
  23. GetTree().CurrentScene = CurrentScene;
  24. }

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

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

GDScriptC#

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

以及

GDScriptC#

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

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

备注

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

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