2

所以我使用的是Godot 3 的Mono 版本。我的脚本是用C# 编写的。我正在尝试遵循本教程:http ://docs.godotengine.org/en/latest/getting_started/step_by_step/singletons_autoload.html

但是代码是在 GDScript 中的,我对它的最佳尝试都没有成功。我已经正确编译了脚本(必须将它们添加到我的.csproj),但我似乎无法访问我在其中设置的 PlayerVarsGlobal.cs对象TitleScene.cs

Global.cs使用系统配置为自动加载;使用戈多;

public class Global : Node {

  private PlayerVars playerVars;

  public override void _Ready () {
    this.playerVars = new PlayerVars();
    int total = 5;
    Godot.GD.Print(what: "total is " + total);
    this.playerVars.total = total;
    GetNode("/root/").Set("playerVars",this.playerVars);
  }

}

PlayerVars.cs存储变量的类。

public class PlayerVars {
  public int total;
}

TitleScene.cs- 附加到我的默认场景:

using System;
using Godot;

public class TitleScene : Node {

    public override void _Ready () {
        Node playervars = (Node) GetNode("/root/playerVars");
        Godot.GD.Print("total in titlescene is" + playervars.total);
    }
}

我觉得我在做一些明显错误的事情。有任何想法吗?

4

1 回答 1

3

好的,想通了。

您可以通过在项目属性中的此屏幕上给出的名称引用节点:

属性屏幕

就我而言,它是global

所以现在我的Global.cs样子是这样的:

using System;
using Godot;

public class Global : Node
{

  private PlayerVars playerVars;

  public override void _Ready()
  {
    // Called every time the node is added to the scene.
    // Initialization here
    Summator summator = new Summator();
    playerVars = new PlayerVars();

    playerVars.total = 5;

    Godot.GD.Print(what: "total is " + playerVars.total);

  }

  public PlayerVars GetPlayerVars(){
    return playerVars;
  }

}

我的TitleScene.cs样子是这样的:

using System;
using Godot;

public class TitleScene : Node
{

  public override void _Ready()
  {
    // Must be cast to the Global type we derived from Node earlier to
    // use its custom methods and props
    Global global = (Global) GetNode("/root/global");
    Godot.GD.Print(global.GetPlayerVars().total);
  }

}
于 2018-02-03T01:25:11.827 回答