cross_language_scripting.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. _doc_cross_language_scripting:
  2. Cross-language scripting
  3. ========================
  4. Godot allows you to mix and match scripting languages to suit your needs.
  5. This means a single project can define nodes in both C# and GDScript.
  6. This page will go through the possible interactions between two nodes written
  7. in different languages.
  8. The following two scripts will be used as references throughout this page.
  9. .. tabs::
  10. .. code-tab:: gdscript GDScript
  11. extends Node
  12. var my_field: String = "foo"
  13. func print_node_name(node: Node) -> void:
  14. print(node.get_name())
  15. func print_array(arr: Array) -> void:
  16. for element in arr:
  17. print(element)
  18. func print_n_times(msg: String, n: int) -> void:
  19. for i in range(n):
  20. print(msg)
  21. .. code-tab:: csharp
  22. using Godot;
  23. public partial class MyCSharpNode : Node
  24. {
  25. public string myField = "bar";
  26. public void PrintNodeName(Node node)
  27. {
  28. GD.Print(node.Name);
  29. }
  30. public void PrintArray(string[] arr)
  31. {
  32. foreach (string element in arr)
  33. {
  34. GD.Print(element);
  35. }
  36. }
  37. public void PrintNTimes(string msg, int n)
  38. {
  39. for (int i = 0; i < n; ++i)
  40. {
  41. GD.Print(msg);
  42. }
  43. }
  44. }
  45. Instantiating nodes
  46. -------------------
  47. If you're not using nodes from the scene tree, you'll probably want to
  48. instantiate nodes directly from the code.
  49. Instantiating C# nodes from GDScript
  50. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. Using C# from GDScript doesn't need much work. Once loaded
  52. (see :ref:`doc_gdscript_classes_as_resources`), the script can be instantiated
  53. with :ref:`new() <class_CSharpScript_method_new>`.
  54. .. code-block:: gdscript
  55. var my_csharp_script = load("res://Path/To/MyCSharpNode.cs")
  56. var my_csharp_node = my_csharp_script.new()
  57. .. warning::
  58. When creating ``.cs`` scripts, you should always keep in mind that the class
  59. Godot will use is the one named like the ``.cs`` file itself. If that class
  60. does not exist in the file, you'll see the following error:
  61. ``Invalid call. Nonexistent function `new` in base``.
  62. For example, MyCoolNode.cs should contain a class named MyCoolNode.
  63. The C# class needs to derive a Godot class, for example ``GodotObject``.
  64. Otherwise, the same error will occur.
  65. You also need to check your ``.cs`` file is referenced in the project's
  66. ``.csproj`` file. Otherwise, the same error will occur.
  67. Instantiating GDScript nodes from C#
  68. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. From the C# side, everything work the same way. Once loaded, the GDScript can
  70. be instantiated with :ref:`GDScript.New() <class_GDScript_method_new>`.
  71. .. code-block:: csharp
  72. GDScript MyGDScript = GD.Load<GDScript>("res://path/to/my_gd_script.gd");
  73. GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject.
  74. Here we are using an :ref:`class_Object`, but you can use type conversion like
  75. explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`.
  76. Accessing fields
  77. ----------------
  78. Accessing C# fields from GDScript
  79. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. Accessing C# fields from GDScript is straightforward, you shouldn't have
  81. anything to worry about.
  82. .. code-block:: gdscript
  83. print(my_csharp_node.myField) # bar
  84. my_csharp_node.myField = "BAR"
  85. print(my_csharp_node.myField) # BAR
  86. Accessing GDScript fields from C#
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. As C# is statically typed, accessing GDScript from C# is a bit more
  89. convoluted, you will have to use :ref:`GodotObject.Get() <class_Object_method_get>`
  90. and :ref:`GodotObject.Set() <class_Object_method_set>`. The first argument is the name of the field you want to access.
  91. .. code-block:: csharp
  92. GD.Print(myGDScriptNode.Get("my_field")); // foo
  93. myGDScriptNode.Set("my_field", "FOO");
  94. GD.Print(myGDScriptNode.Get("my_field")); // FOO
  95. Keep in mind that when setting a field value you should only use types the
  96. GDScript side knows about.
  97. Essentially, you want to work with built-in types as described in :ref:`doc_gdscript` or classes extending :ref:`class_Object`.
  98. Calling methods
  99. ---------------
  100. Calling C# methods from GDScript
  101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102. Again, calling C# methods from GDScript should be straightforward. The
  103. marshalling process will do its best to cast the arguments to match
  104. function signatures.
  105. If that's impossible, you'll see the following error: ``Invalid call. Nonexistent function `FunctionName```.
  106. .. code-block:: gdscript
  107. my_csharp_node.PrintNodeName(self) # myGDScriptNode
  108. # my_csharp_node.PrintNodeName() # This line will fail.
  109. my_csharp_node.PrintNTimes("Hello there!", 2) # Hello there! Hello there!
  110. my_csharp_node.PrintArray(["a", "b", "c"]) # a, b, c
  111. my_csharp_node.PrintArray([1, 2, 3]) # 1, 2, 3
  112. Calling GDScript methods from C#
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. To call GDScript methods from C# you'll need to use
  115. :ref:`GodotObject.Call() <class_Object_method_call>`. The first argument is the
  116. name of the method you want to call. The following arguments will be passed
  117. to said method.
  118. .. code-block:: csharp
  119. myGDScriptNode.Call("print_node_name", this); // my_csharp_node
  120. // myGDScriptNode.Call("print_node_name"); // This line will fail silently and won't error out.
  121. myGDScriptNode.Call("print_n_times", "Hello there!", 2); // Hello there! Hello there!
  122. string[] arr = new string[] { "a", "b", "c" };
  123. myGDScriptNode.Call("print_array", arr); // a, b, c
  124. myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3
  125. // Note how the type of each array entry does not matter as long as it can be handled by the marshaller.
  126. .. warning::
  127. As you can see, if the first argument of the called method is an array,
  128. you'll need to cast it as ``object``.
  129. Otherwise, each element of your array will be treated as a single argument
  130. and the function signature won't match.
  131. Inheritance
  132. -----------
  133. A GDScript file may not inherit from a C# script. Likewise, a C# script may not
  134. inherit from a GDScript file. Due to how complex this would be to implement,
  135. this limitation is unlikely to be lifted in the future. See
  136. `this GitHub issue <https://github.com/godotengine/godot/issues/38352>`__
  137. for more information.