Cross-language scripting

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.

The following two scripts will be used as references throughout this page.

GDScriptC#

  1. extends Node
  2. var my_property: String = "my gdscript value":
  3. get:
  4. return my_property
  5. set(value):
  6. my_property = value
  7. signal my_signal
  8. func print_node_name(node: Node) -> void:
  9. print(node.get_name())
  10. func print_array(arr: Array) -> void:
  11. for element in arr:
  12. print(element)
  13. func print_n_times(msg: String, n: int) -> void:
  14. for i in range(n):
  15. print(msg)
  16. func my_signal_handler():
  17. print("The signal handler was called!")
  1. using Godot;
  2. public partial class MyCSharpNode : Node
  3. {
  4. public string MyProperty { get; set; } = "my c# value";
  5. [Signal] public delegate void MySignalEventHandler();
  6. public void PrintNodeName(Node node)
  7. {
  8. GD.Print(node.Name);
  9. }
  10. public void PrintArray(string[] arr)
  11. {
  12. foreach (string element in arr)
  13. {
  14. GD.Print(element);
  15. }
  16. }
  17. public void PrintNTimes(string msg, int n)
  18. {
  19. for (int i = 0; i < n; ++i)
  20. {
  21. GD.Print(msg);
  22. }
  23. }
  24. public void MySignalHandler()
  25. {
  26. GD.Print("The signal handler was called!");
  27. }
  28. }

Instantiating nodes

If you’re not using nodes from the scene tree, you’ll probably want to instantiate nodes directly from the code.

Instantiating C# nodes from GDScript

Using C# from GDScript doesn’t need much work. Once loaded (see Classes as resources), the script can be instantiated with new().

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

Warning

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.

For example, MyCoolNode.cs should contain a class named 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.

Instantiating GDScript nodes from C

From the C# side, everything work the same way. Once loaded, the GDScript can be instantiated with GDScript.New().

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

Here we are using an Object, but you can use type conversion like explained in Type conversion and casting.

Accessing fields

Accessing C# fields from GDScript

Accessing C# fields from GDScript is straightforward, you shouldn’t have anything to worry about.

  1. # Output: "my c# value".
  2. print(my_csharp_node.MyProperty)
  3. my_csharp_node.MyProperty = "MY C# VALUE"
  4. # Output: "MY C# VALUE".
  5. print(my_csharp_node.MyProperty)

Accessing GDScript fields from C

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.

  1. // Output: "my gdscript value".
  2. GD.Print(myGDScriptNode.Get("my_property"));
  3. myGDScriptNode.Set("my_property", "MY GDSCRIPT VALUE");
  4. // Output: "MY GDSCRIPT VALUE".
  5. GD.Print(myGDScriptNode.Get("my_property"));

Keep in mind that when setting a field value you should only use types the GDScript side knows about. Essentially, you want to work with built-in types as described in GDScript reference or classes extending Object.

Calling methods

Calling C# methods from GDScript

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` .

  1. # Output: "my_gd_script_node" (or name of node where this code is placed).
  2. my_csharp_node.PrintNodeName(self)
  3. # This line will fail.
  4. # my_csharp_node.PrintNodeName()
  5. # Outputs "Hello there!" twice, once per line.
  6. my_csharp_node.PrintNTimes("Hello there!", 2)
  7. # Output: "a", "b", "c" (one per line).
  8. my_csharp_node.PrintArray(["a", "b", "c"])
  9. # Output: "1", "2", "3" (one per line).
  10. my_csharp_node.PrintArray([1, 2, 3])

Calling GDScript methods from C

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.

  1. // Output: "MyCSharpNode" (or name of node where this code is placed).
  2. myGDScriptNode.Call("print_node_name", this);
  3. // This line will fail silently and won't error out.
  4. // myGDScriptNode.Call("print_node_name");
  5. // Outputs "Hello there!" twice, once per line.
  6. myGDScriptNode.Call("print_n_times", "Hello there!", 2);
  7. string[] arr = new string[] { "a", "b", "c" };
  8. // Output: "a", "b", "c" (one per line).
  9. myGDScriptNode.Call("print_array", arr);
  10. // Output: "1", "2", "3" (one per line).
  11. myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 });
  12. // Note how the type of each array entry does not matter
  13. // as long as it can be handled by the marshaller.

Connecting to signals

Connecting to C# signals from GDScript

Connecting to a C# signal from GDScript is the same as connecting to a signal defined in GDScript:

  1. my_csharp_node.MySignal.connect(my_signal_handler)

Connecting to GDScript signals from C

Connecting to a GDScript signal from C# only works with the Connect method because no C# static types exist for signals defined by GDScript:

  1. myGDScriptNode.Connect("my_signal", Callable.From(MySignalHandler));

Inheritance

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.


User-contributed notes

Please read the User-contributed notes policy before submitting a comment.

Previous Next