单例(自动加载)

简介

Godot的场景系统虽然功能强大且灵活,但有一个缺点:没有一种方法可以用于存储多个场景所需的信息(例如 player 的分数或库存)。

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

  • 您可以使用“主”场景,加载和卸载其他场景作为其子级。但是,这意味着您不能再单独运行这些场景并期望它们正常工作。
  • 信息可以存储在 user:// 下的磁盘上,然后由需要它的场景加载,但是经常保存和加载数据很麻烦并且可能很慢。

The Singleton pattern is a useful tool for solving the common use case where you need to store persistent information between scenes. In our case, it's possible to reuse the same scene or class for multiple singletons as long as they have different names.

使用此概念,您可以创建对象:

  • Are always loaded, no matter which scene is currently running.
  • Can store global variables such as player information.
  • Can handle switching scenes and between-scene transitions.
  • Act like a singleton, since GDScript does not support global variables by design.

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

注解

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.

注解

When autoloading a script, a Node will be created and the script will be attached to it. This node will be added to the root viewport before any other scenes are loaded.

../../_images/singleton.png

To autoload a scene or script, select Project > Project Settings from the menu and switch to the AutoLoad tab.

../../_images/autoload_tab.png

您可以在此处添加任意数量的场景或脚本。列表中的每个条目都需要一个名称,该名称被分配为节点的 name 属性。使用向上/向下箭头键可以操纵将条目添加到全局场景树时的顺序。

../../_images/autoload_example.png

这意味着任何节点都可以通过以下方式访问名为 PlayerVariables 的单例:

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

If the Enable column is checked (which is the default), then the singleton can be accessed directly without requiring get_node():

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

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

../../_images/autoload_runtime.png

自定义场景切换器

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

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

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

Global.gd

Switch to the Script tab and create a new script called Global.gd. Make sure it inherits from Node:

../../_images/autoload_script.png

The next step is to add this script to the autoLoad list. Open Project > Project Settings from the menu, switch to the AutoLoad tab 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.png

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

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.

extends Node

var current_scene = null

func _ready():
    var root = get_tree().get_root()
    current_scene = root.get_child(root.get_child_count() - 1)
using Godot;
using System;

public class Global : Godot.Node
{
    public Node CurrentScene { get; set; }

    public override void _Ready()
    {
        Viewport root = GetTree().GetRoot();
        CurrentScene = root.GetChild(root.GetChildCount() - 1);
    }
}

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

func goto_scene(path):
    # This function will usually be called from a signal callback,
    # or some other function in the current scene.
    # Deleting the current scene at this point is
    # a bad idea, because it may still be executing code.
    # This will result in a crash or unexpected behavior.

    # The solution is to defer the load to a later time, when
    # we can be sure that no code from the current scene is running:

    call_deferred("_deferred_goto_scene", path)


func _deferred_goto_scene(path):
    # It is now safe to remove the current scene
    current_scene.free()

    # Load the new scene.
    var s = ResourceLoader.load(path)

    # Instance the new scene.
    current_scene = s.instance()

    # Add it to the active scene, as child of root.
    get_tree().get_root().add_child(current_scene)

    # Optionally, to make it compatible with the SceneTree.change_scene() API.
    get_tree().set_current_scene(current_scene)
public void GotoScene(string path)
{
    // This function will usually be called from a signal callback,
    // or some other function from the current scene.
    // Deleting the current scene at this point is
    // a bad idea, because it may still be executing code.
    // This will result in a crash or unexpected behavior.

    // The solution is to defer the load to a later time, when
    // we can be sure that no code from the current scene is running:

    CallDeferred(nameof(DeferredGotoScene), path);
}

public void DeferredGotoScene(string path)
{
    // It is now safe to remove the current scene
    CurrentScene.Free();

    // Load a new scene.
    var nextScene = (PackedScene)GD.Load(path);

    // Instance the new scene.
    CurrentScene = nextScene.Instance();

    // Add it to the active scene, as child of root.
    GetTree().GetRoot().AddChild(CurrentScene);

    // Optionally, to make it compatible with the SceneTree.change_scene() API.
    GetTree().SetCurrentScene(CurrentScene);
}

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

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

# Add to 'Scene1.gd'.

func _on_Button_pressed():
    Global.goto_scene("res://Scene2.tscn")
// Add to 'Scene1.cs'.

public void OnButtonPressed()
{
    var global = (Global)GetNode("/root/Global");
    global.GotoScene("res://Scene2.tscn");
}

以及

# Add to 'Scene2.gd'.

func _on_Button_pressed():
    Global.goto_scene("res://Scene1.tscn")
// Add to 'Scene2.cs'.

public void OnButtonPressed()
{
    var global = (Global)GetNode("/root/Global");
    global.GotoScene("res://Scene1.tscn");
}

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

注解

When scenes are small, the transition is instantaneous. However, if your scenes are more complex, they may take a noticeable amount of time to appear. To learn how to handle this, see the next tutorial: 后台加载.

Alternatively, if the loading time is relatively short (less than 3 seconds or so), you can display a "loading plaque" by showing some kind of 2D element just before changing the scene. You can then hide it just after the scene is changed. This can be used to indicate to the player that a scene is being loaded.