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 writtenin different languages.
The following two scripts will be used as references throughout this page.
extendsNodevarstr1:String="foo"varstr2:Stringsetget,get_str2funcget_str2()->String:return"foofoo"funcprint_node_name(node:Node)->void:print(node.get_name())funcprint_array(arr:Array)->void:forelementinarr:print(element)funcprint_n_times(msg:String,n:int)->void:foriinrange(n):print(msg)
publicclassMyCSharpNode:Node{publicStringstr1="bar";publicStringstr2{get{return"barbar";}}publicvoidPrintNodeName(Nodenode){GD.Print(node.GetName());}publicvoidPrintArray(String[]arr){foreach(Stringelementinarr){GD.Print(element);}}publicvoidPrintNTimes(Stringmsg,intn){for(inti=0;i<n;++i){GD.Print(msg);}}}
Instantiating nodes¶
If you're not using nodes from the scene tree, you'll probably want toinstantiate nodes directly from the code.
Instantiating C# nodes from GDScript¶
Using C# from GDScript doesn't need much work. Once loaded(seeClasses as resources), the script can be instantiatedwithnew().
varmy_csharp_script=load("res://path_to_cs_file.cs")varmy_csharp_node=my_csharp_script.new()print(my_csharp_node.str2)# barbar
Warning
When creating.cs scripts, you should always keep in mind that the classGodot will use is the one named like the.cs file itself. If that classdoes not exist in the file, you'll see the following error:Invalidcall.Nonexistentfunction`new`inbase.
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 canbe instantiated withGDScript.New().
GDScriptMyGDScript=(GDScript)GD.Load("res://path_to_gd_file.gd");ObjectmyGDScriptNode=(Godot.Object)MyGDScript.New();// This is a Godot.Object
Here we are using anObject, but you can use type conversion likeexplained inType conversion and casting.
Accessing fields¶
Accessing C# fields from GDScript¶
Accessing C# fields from GDScript is straightforward, you shouldn't haveanything to worry about.
print(my_csharp_node.str1)# barmy_csharp_node.str1="BAR"print(my_csharp_node.str1)# BARprint(my_csharp_node.str2)# barbar# my_csharp_node.str2 = "BARBAR" # This line will hang and crash
Note that it doesn't matter if the field is defined as a property or anattribute. However, trying to set a value on a property that does not definea setter will result in a crash.
Accessing GDScript fields from C#¶
As C# is statically typed, accessing GDScript from C# is a bit moreconvoluted, you will have to useObject.Get()andObject.Set(). The first argument is the name of the field you want to access.
GD.Print(myGDScriptNode.Get("str1"));// foomyGDScriptNode.Set("str1","FOO");GD.Print(myGDScriptNode.Get("str1"));// FOOGD.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 theGDScript side knows about.Essentially, you want to work with built-in types as described inGDScript basics or classes extendingObject.
Calling methods¶
Calling C# methods from GDScript¶
Again, calling C# methods from GDScript should be straightforward. Themarshalling process will do its best to cast the arguments to matchfunction signatures.If that's impossible, you'll see the following error:Invalidcall.Nonexistentfunction`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, cmy_csharp_node.PrintArray([1,2,3])# 1, 2, 3
Calling GDScript methods from C#¶
To call GDScript methods from C# you'll need to useObject.Call(). The first argument is thename of the method you want to call. The following arguments will be passedto 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=newString[]{"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, cmyGDScriptNode.Call("print_array",(object)newint[]{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
Warning
As you can see, if the first argument of the called method is an array,you'll need to cast it asobject.Otherwise, each element of your array will be treated as a single argumentand the function signature won't match.
Inheritance¶
A GDScript file may not inherit from a C# script. Likewise, a C# script may notinherit from a GDScript file. Due to how complex this would be to implement,this limitation is unlikely to be lifted in the future. Seethis GitHub issuefor more information.