所以我正在使用Mono版本的Godot 3.我的脚本在C#中。我正在尝试按照本教程:http://docs.godotengine.org/en/latest/getting_started/step_by_step/singletons_autoload.html
但代码是在GDScript中,我最好的尝试是适应它是不成功的。我已经正确编译了脚本(必须将它们添加到我的.csproj
)但我似乎无法访问我在Global.cs
中TitleScene.cs
中设置的PlayerVars对象
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);
}
}
我觉得我做的事情显而易见。有什么想法吗?
答案 0 :(得分:2)
好的,想通了。
您可以在项目属性中使用您在此屏幕上提供的名称来引用节点:
就我而言,它是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);
}
}