单例(自动加载)

简介

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

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

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

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

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

  • 无论当前运行哪个场景,始终加载。
  • 可以存储全局变量,如玩家信息。
  • 可以处理切换场景和场景间过渡。
  • **行为**类似单例模式,因为GDScript在设计上不支持全局变量。

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

注解

Godot不会按照单例设计模式将AutoLoad设为“真正的”单例。如有必要,用户仍然可以对它进行多次实例化。

自动加载

你可以创建一个AutoLoad来加载一个场景或一个继承自 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");
}

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

注解

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

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