Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

クロスランゲージでのスクリプト作成

Godot allows you to mix and match scripting languages to suit your needs. This means a single project can define nodes in both C# and GDScript. This page will go through the possible interactions between two nodes written in different languages.

次の2つのスクリプトは、このページ全体で参照として使用されます。

extends Node

var my_field: String = "foo"

func print_node_name(node: Node) -> void:
    print(node.get_name())

func print_array(arr: Array) -> void:
    for element in arr:
        print(element)

func print_n_times(msg: String, n: int) -> void:
    for i in range(n):
        print(msg)

ノードのインスタンス化

シーンツリーのノードを使用していない場合は、コードから直接ノードをインスタンス化することをお勧めします。

GDScriptからのC#ノードのインスタンス化

Using C# from GDScript doesn't need much work. Once loaded (see リソースとしてのクラス), the script can be instantiated with new().

var my_csharp_script = load("res://Path/To/MyCSharpNode.cs")
var my_csharp_node = my_csharp_script.new()

警告

When creating .cs scripts, you should always keep in mind that the class Godot will use is the one named like the .cs file itself. If that class does not exist in the file, you'll see the following error: Invalid call. Nonexistent function `new` in base.

たとえば、MyCoolNode.csにはMyCoolNodeという名前のクラスが含まれている必要があります。

The C# class needs to derive a Godot class, for example GodotObject. Otherwise, the same error will occur.

You also need to check your .cs file is referenced in the project's .csproj file. Otherwise, the same error will occur.

C#からのGDScriptノードのインスタンス化

C#側から見ても、すべてが同じように機能します。ロードされると、GDScriptは GDScript.New() でインスタンス化できます。

GDScript MyGDScript = GD.Load<GDScript>("res://path/to/my_gd_script.gd");
GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject.

Here we are using an Object, but you can use type conversion like explained in 型変換とキャスト.

フィールドへのアクセス

GDScriptからC#フィールドにアクセスする

GDScriptからC#フィールドにアクセスするのは簡単です。心配する必要はありません。

print(my_csharp_node.myField) # bar
my_csharp_node.myField = "BAR"
print(my_csharp_node.myField) # BAR

C#からGDScriptフィールドにアクセスする

As C# is statically typed, accessing GDScript from C# is a bit more convoluted, you will have to use GodotObject.Get() and GodotObject.Set(). The first argument is the name of the field you want to access.

GD.Print(myGDScriptNode.Get("my_field")); // foo
myGDScriptNode.Set("my_field", "FOO");
GD.Print(myGDScriptNode.Get("my_field")); // FOO

フィールド値を設定するときは、GDScript側が知っている型のみを使用する必要があることに注意してください。基本的に、GDScript reference または Object を拡張するクラスで説明されている組み込み型を使用する必要があります。

メソッドの呼び出し

GDScriptからのC#メソッドの呼び出し

Again, calling C# methods from GDScript should be straightforward. The marshalling process will do its best to cast the arguments to match function signatures. If that's impossible, you'll see the following error: Invalid call. Nonexistent function `FunctionName`.

my_csharp_node.PrintNodeName(self) # myGDScriptNode
# my_csharp_node.PrintNodeName() # This line will fail.

my_csharp_node.PrintNTimes("Hello there!", 2) # Hello there! Hello there!

my_csharp_node.PrintArray(["a", "b", "c"]) # a, b, c
my_csharp_node.PrintArray([1, 2, 3]) # 1, 2, 3

C#からのGDScriptメソッドの呼び出し

To call GDScript methods from C# you'll need to use GodotObject.Call(). The first argument is the name of the method you want to call. The following arguments will be passed to said method.

myGDScriptNode.Call("print_node_name", this); // my_csharp_node
// myGDScriptNode.Call("print_node_name"); // This line will fail silently and won't error out.

myGDScriptNode.Call("print_n_times", "Hello there!", 2); // Hello there! Hello there!

string[] arr = new string[] { "a", "b", "c" };
myGDScriptNode.Call("print_array", arr); // a, b, c
myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3
// Note how the type of each array entry does not matter as long as it can be handled by the marshaller.

警告

As you can see, if the first argument of the called method is an array, you'll need to cast it as object. Otherwise, each element of your array will be treated as a single argument and the function signature won't match.

継承

A GDScript file may not inherit from a C# script. Likewise, a C# script may not inherit from a GDScript file. Due to how complex this would be to implement, this limitation is unlikely to be lifted in the future. See this GitHub issue for more information.