cross_language_scripting.rst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_cs_file.cs")
  56. var my_csharp_node = my_csharp_script.new()
  57. print(my_csharp_node.str2) # barbar
  58. .. warning::
  59. When creating ``.cs`` scripts, you should always keep in mind that the class
  60. Godot will use is the one named like the ``.cs`` file itself. If that class
  61. does not exist in the file, you'll see the following error:
  62. ``Invalid call. Nonexistent function `new` in base``.
  63. For example, MyCoolNode.cs should contain a class named MyCoolNode.
  64. The C# class needs to derive a Godot class, for example ``GodotObject``.
  65. Otherwise, the same error will occur.
  66. You also need to check your ``.cs`` file is referenced in the project's
  67. ``.csproj`` file. Otherwise, the same error will occur.
  68. Instantiating GDScript nodes from C#
  69. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. From the C# side, everything work the same way. Once loaded, the GDScript can
  71. be instantiated with :ref:`GDScript.New() <class_GDScript_method_new>`.
  72. .. code-block:: csharp
  73. GDScript MyGDScript = (GDScript)GD.Load("res://path_to_gd_file.gd");
  74. GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject
  75. Here we are using an :ref:`class_Object`, but you can use type conversion like
  76. explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`.
  77. Accessing fields
  78. ----------------
  79. Accessing C# fields from GDScript
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. Accessing C# fields from GDScript is straightforward, you shouldn't have
  82. anything to worry about.
  83. .. code-block:: gdscript
  84. print(my_csharp_node.myField) # bar
  85. my_csharp_node.myField = "BAR"
  86. print(my_csharp_node.myField) # BAR
  87. Accessing GDScript fields from C#
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. As C# is statically typed, accessing GDScript from C# is a bit more
  90. convoluted, you will have to use :ref:`GodotObject.Get() <class_Object_method_get>`
  91. and :ref:`GodotObject.Set() <class_Object_method_set>`. The first argument is the name of the field you want to access.
  92. .. code-block:: csharp
  93. GD.Print(myGDScriptNode.Get("my_field")); // foo
  94. myGDScriptNode.Set("my_field", "FOO");
  95. GD.Print(myGDScriptNode.Get("my_field")); // FOO
  96. Keep in mind that when setting a field value you should only use types the
  97. GDScript side knows about.
  98. Essentially, you want to work with built-in types as described in :ref:`doc_gdscript` or classes extending :ref:`class_Object`.
  99. Calling methods
  100. ---------------
  101. Calling C# methods from GDScript
  102. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  103. Again, calling C# methods from GDScript should be straightforward. The
  104. marshalling process will do its best to cast the arguments to match
  105. function signatures.
  106. If that's impossible, you'll see the following error: ``Invalid call. Nonexistent function `FunctionName```.
  107. .. code-block:: gdscript
  108. my_csharp_node.PrintNodeName(self) # myGDScriptNode
  109. # my_csharp_node.PrintNodeName() # This line will fail.
  110. my_csharp_node.PrintNTimes("Hello there!", 2) # Hello there! Hello there!
  111. my_csharp_node.PrintArray(["a", "b", "c"]) # a, b, c
  112. my_csharp_node.PrintArray([1, 2, 3]) # 1, 2, 3
  113. Calling GDScript methods from C#
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. To call GDScript methods from C# you'll need to use
  116. :ref:`GodotObject.Call() <class_Object_method_call>`. The first argument is the
  117. name of the method you want to call. The following arguments will be passed
  118. to said method.
  119. .. code-block:: csharp
  120. myGDScriptNode.Call("print_node_name", this); // my_csharp_node
  121. // myGDScriptNode.Call("print_node_name"); // This line will fail silently and won't error out.
  122. myGDScriptNode.Call("print_n_times", "Hello there!", 2); // Hello there! Hello there!
  123. string[] arr = new string[] { "a", "b", "c" };
  124. myGDScriptNode.Call("print_array", arr); // a, b, c
  125. myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3
  126. // Note how the type of each array entry does not matter as long as it can be handled by the marshaller
  127. .. warning::
  128. As you can see, if the first argument of the called method is an array,
  129. you'll need to cast it as ``object``.
  130. Otherwise, each element of your array will be treated as a single argument
  131. and the function signature won't match.
  132. Inheritance
  133. -----------
  134. A GDScript file may not inherit from a C# script. Likewise, a C# script may not
  135. inherit from a GDScript file. Due to how complex this would be to implement,
  136. this limitation is unlikely to be lifted in the future. See
  137. `this GitHub issue <https://github.com/godotengine/godot/issues/38352>`__
  138. for more information.