Cross-language scripting
The following two scripts will be used as references throughout this page.
GDScript
C#
{
public String str1 = "bar";
public String str2 { get { return "barbar"; } }
public void PrintNodeName(Node node)
{
GD.Print(node.GetName());
public void PrintArray(String[] arr)
{
foreach (String element in arr)
{
GD.Print(element);
}
}
public void PrintNTimes(String msg, int n)
{
for (int i = 0; i < n; ++i)
{
GD.Print(msg);
}
}
}
If you’re not using nodes from the scene tree, you’ll probably want to instantiate nodes directly from the code.
Using C# from GDScript doesn’t need much work. Once loaded (see ), the script can be instantiated with new().
Warning
For example, MyCoolNode.cs should contain a class named MyCoolNode.
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 MyGDScript = (GDScript) GD.Load("res://path_to_gd_file.gd");
Here we are using an Object, but you can use type conversion like explained in .
Accessing C# fields from GDScript is straightforward, you shouldn’t have anything to worry about.
Note that it doesn’t matter if the field is defined as a property or an attribute. However, trying to set a value on a property that does not define a setter will result in a crash.
Accessing GDScript fields from C
GD.Print(myGDScriptNode.Get("str1")); // foo
GD.Print(myGDScriptNode.Get("str1")); // FOO
GD.Print(myGDScriptNode.Get("str2")); // foofoo
// myGDScriptNode.Set("str2", "FOOFOO"); // This line won't do anything
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 basics or classes extending .
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`
.
Calling GDScript methods from C
To call GDScript methods from C# you’ll need to use Object.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!
// When dealing with functions taking a single array as arguments, we need to be careful.
// 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.
String[] arr = new String[] { "a", "b", "c" };
// myGDScriptNode.Call("print_array", arr); // This line will fail silently and won't error out.
myGDScriptNode.Call("print_array", (object)arr); // a, b, c
myGDScriptNode.Call("print_array", (object)new int[] { 1, 2, 3 }); // 1, 2, 3
Warning
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 for more information.