Cross-language scripting

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

    GDScript

    C#

    1. {
    2. public String str1 = "bar";
    3. public String str2 { get { return "barbar"; } }
    4. public void PrintNodeName(Node node)
    5. {
    6. GD.Print(node.GetName());
    7. public void PrintArray(String[] arr)
    8. {
    9. foreach (String element in arr)
    10. {
    11. GD.Print(element);
    12. }
    13. }
    14. public void PrintNTimes(String msg, int n)
    15. {
    16. for (int i = 0; i < n; ++i)
    17. {
    18. GD.Print(msg);
    19. }
    20. }
    21. }

    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 .

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

    1. GD.Print(myGDScriptNode.Get("str1")); // foo
    2. GD.Print(myGDScriptNode.Get("str1")); // FOO
    3. GD.Print(myGDScriptNode.Get("str2")); // foofoo
    4. // 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.

    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

    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.