跨语言脚本

Godot允许您混合和匹配脚本语言以满足您的需求. 这意味着一个项目可以同时用C#和GDScript定义节点. 本页将介绍用不同语言编写的两个节点之间可能的交互.

以下两个脚本用在整个页面中作为参考.

GDScriptC#

  1. extends Node
  2. var str1 : String = "foo"
  3. var str2 : String setget ,get_str2
  4. func get_str2() -> String:
  5. return "foofoo"
  6. func print_node_name(node : Node) -> void:
  7. print(node.get_name())
  8. func print_array(arr : Array) -> void:
  9. for element in arr:
  10. print(element)
  11. func print_n_times(msg : String, n : int) -> void:
  12. for i in range(n):
  13. print(msg)
  1. public class MyCSharpNode : Node
  2. {
  3. public String str1 = "bar";
  4. public String str2 { get { return "barbar"; } }
  5. public void PrintNodeName(Node node)
  6. {
  7. GD.Print(node.GetName());
  8. }
  9. public void PrintArray(String[] arr)
  10. {
  11. foreach (String element in arr)
  12. {
  13. GD.Print(element);
  14. }
  15. }
  16. public void PrintNTimes(String msg, int n)
  17. {
  18. for (int i = 0; i < n; ++i)
  19. {
  20. GD.Print(msg);
  21. }
  22. }
  23. }

实例化节点

如果不使用场景树中的节点, 则可能需要直接从代码实例化节点.

在 GDScript 中实例化 C# 节点

从GDScript中使用C#并不麻烦. 加载后(见 类作为资源)脚本就可以使用 new() 进行实例化.

  1. var my_csharp_script = load("res://path_to_cs_file.cs")
  2. var my_csharp_node = my_csharp_script.new()
  3. print(my_csharp_node.str2) # barbar

警告

创建 .cs 脚本时, 应始终记住 Godot 将使用和这个 .cs 文件名相同的类. 如果文件中不存在该类, 您将看到以下错误: Invalid call. Nonexistent function `new` in base .

比如,MyCoolNode.cs 应该包含一个名为 MyCoolNode 的类.

您还需要检查在项目的 .csproj 文件中引用了该 .cs 文件的内容. 否则, 将发生相同的错误.

在C#中实例化GDScript节点

在 C# 端, 所有的工作方式相同. 加载后,GDScript 可以被示例化, 使用 GDScript.New().

  1. GDScript MyGDScript = (GDScript) GD.Load("res://path_to_gd_file.gd");
  2. Object myGDScriptNode = (Godot.Object) MyGDScript.New(); // This is a Godot.Object

在这里我们使用一个 Object , 但是也可以使用类型转换, 如 类型转换和强制转换 章节所述.

访问字段

从 GDScript 中访问 C# 字段

从GDScript访问 C# 字段很简单, 没什么可担心的.

  1. print(my_csharp_node.str1) # bar
  2. my_csharp_node.str1 = "BAR"
  3. print(my_csharp_node.str1) # BAR
  4. print(my_csharp_node.str2) # barbar
  5. # my_csharp_node.str2 = "BARBAR" # This line will hang and crash

需要注意的是, 字段定义为属性(property)或特性(attribute)并不重要, 但尝试在未定义 setter 的属性(property)上设置值将导致崩溃.

从 C# 中访问 GDSscript

由于 C# 是静态类型, 因此从 C# 访问 GDScript 会有点复杂, 因此您必须使用 Object.Get()Object.Set() . 第一个参数是要访问的字段的名称.

  1. GD.Print(myGDScriptNode.Get("str1")); // foo
  2. myGDScriptNode.Set("str1", "FOO");
  3. GD.Print(myGDScriptNode.Get("str1")); // FOO
  4. GD.Print(myGDScriptNode.Get("str2")); // foofoo
  5. // myGDScriptNode.Set("str2", "FOOFOO"); // This line won't do anything

牢记在给字段赋值时只能使用 GDScript 知道的类型. 实质上指的是 GDScript 的内置类型 GDScript 基础 或者 Object 的扩展类.

调用方法

在GDScript中调用C#方法

从 GDScript 调用 C# 方法同样是很简单的. 调用过程将尽力强制转换你的参数类型去匹配函数签名. 如果失败则会看到以下错误 Invalid call. Nonexistent function `FunctionName` .

  1. my_csharp_node.PrintNodeName(self) # myGDScriptNode
  2. # my_csharp_node.PrintNodeName() # This line will fail.
  3. my_csharp_node.PrintNTimes("Hello there!", 2) # Hello there! Hello there!
  4. my_csharp_node.PrintArray(["a", "b", "c"]) # a, b, c
  5. my_csharp_node.PrintArray([1, 2, 3]) # 1, 2, 3

从 C# 中 调用 GDScript 方法

从 C# 中调用 GDScript 方法需要使用 Object.Call() . 第一个参数是想要调用方法的名称. 接下来的其他参数会传递给被调用的方法.

  1. myGDScriptNode.Call("print_node_name", this); // my_csharp_node
  2. // myGDScriptNode.Call("print_node_name"); // This line will fail silently and won't error out.
  3. myGDScriptNode.Call("print_n_times", "Hello there!", 2); // Hello there! Hello there!
  4. // When dealing with functions taking a single array as arguments, we need to be careful.
  5. // If we don't cast it into an object, the engine will treat each element of the array as a separate argument and the call will fail.
  6. String[] arr = new String[] { "a", "b", "c" };
  7. // myGDScriptNode.Call("print_array", arr); // This line will fail silently and won't error out.
  8. myGDScriptNode.Call("print_array", (object)arr); // a, b, c
  9. myGDScriptNode.Call("print_array", (object)new int[] { 1, 2, 3 }); // 1, 2, 3
  10. // Note how the type of each array entry does not matter as long as it can be handled by the marshaller

警告

如您所见,如果被调用方法的第一个参数为数组类型,你需要强制转换成 object。否则数组的每个元素将被当做单个参数传入,这将导致与被调用的函数签名参数不匹配。

继承

GDScript文件可能无法从C#脚本继承. 同样地,C#脚本可能无法从GDScript文件继承. 由于实现起来非常复杂, 因此将来不太可能取消此限制. 详见 这个 GitHub issue .