Merge pull request #8195 from wlsnmrk/cross-scripting-style

Improve code style for cross-language scripting examples
This commit is contained in:
Max Hilbrunner
2024-11-19 10:39:43 +01:00
committed by GitHub
@@ -16,7 +16,11 @@ The following two scripts will be used as references throughout this page.
extends Node extends Node
var my_field: String = "foo" var my_property: String = "my gdscript value":
get:
return my_property
set(value):
my_property = value
signal my_signal signal my_signal
signal my_signal_with_params(msg: String, n: int) signal my_signal_with_params(msg: String, n: int)
@@ -44,7 +48,7 @@ The following two scripts will be used as references throughout this page.
public partial class MyCSharpNode : Node public partial class MyCSharpNode : Node
{ {
public string myField = "bar"; public string MyProperty { get; set; } = "my c# value";
[Signal] public delegate void MySignalEventHandler(); [Signal] public delegate void MySignalEventHandler();
[Signal] public delegate void MySignalWithParamsEventHandler(string msg, int n); [Signal] public delegate void MySignalWithParamsEventHandler(string msg, int n);
@@ -96,8 +100,8 @@ with :ref:`new() <class_CSharpScript_method_new>`.
.. code-block:: gdscript .. code-block:: gdscript
var my_csharp_script = load("res://Path/To/MyCSharpNode.cs") var MyCSharpScript = load("res://Path/To/MyCSharpNode.cs")
var my_csharp_node = my_csharp_script.new() var my_csharp_node = MyCSharpScript.new()
.. warning:: .. warning::
@@ -122,8 +126,8 @@ be instantiated with :ref:`GDScript.New() <class_GDScript_method_new>`.
.. code-block:: csharp .. code-block:: csharp
GDScript MyGDScript = GD.Load<GDScript>("res://path/to/my_gd_script.gd"); var myGDScript = GD.Load<GDScript>("res://path/to/my_gd_script.gd");
GodotObject myGDScriptNode = (GodotObject)MyGDScript.New(); // This is a GodotObject. var myGDScriptNode = (GodotObject)myGDScript.New(); // This is a GodotObject.
Here we are using an :ref:`class_Object`, but you can use type conversion like Here we are using an :ref:`class_Object`, but you can use type conversion like
explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`. explained in :ref:`doc_c_sharp_features_type_conversion_and_casting`.
@@ -139,22 +143,26 @@ anything to worry about.
.. code-block:: gdscript .. code-block:: gdscript
print(my_csharp_node.myField) # bar # Output: "my c# value".
my_csharp_node.myField = "BAR" print(my_csharp_node.MyProperty)
print(my_csharp_node.myField) # BAR my_csharp_node.MyProperty = "MY C# VALUE"
# Output: "MY C# VALUE".
print(my_csharp_node.MyProperty)
Accessing GDScript fields from C# Accessing GDScript fields from C#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As C# is statically typed, accessing GDScript from C# is a bit more As C# is statically typed, accessing GDScript from C# is a bit more
convoluted, you will have to use :ref:`GodotObject.Get() <class_Object_method_get>` convoluted. You will have to use :ref:`GodotObject.Get() <class_Object_method_get>`
and :ref:`GodotObject.Set() <class_Object_method_set>`. The first argument is the name of the field you want to access. and :ref:`GodotObject.Set() <class_Object_method_set>`. The first argument is the name of the field you want to access.
.. code-block:: csharp .. code-block:: csharp
GD.Print(myGDScriptNode.Get("my_field")); // foo // Output: "my gdscript value".
myGDScriptNode.Set("my_field", "FOO"); GD.Print(myGDScriptNode.Get("my_property"));
GD.Print(myGDScriptNode.Get("my_field")); // FOO myGDScriptNode.Set("my_property", "MY GDSCRIPT VALUE");
// Output: "MY GDSCRIPT VALUE".
GD.Print(myGDScriptNode.Get("my_property"));
Keep in mind that when setting a field value you should only use types the Keep in mind that when setting a field value you should only use types the
GDScript side knows about. GDScript side knows about.
@@ -173,13 +181,18 @@ If that's impossible, you'll see the following error: ``Invalid call. Nonexisten
.. code-block:: gdscript .. code-block:: gdscript
my_csharp_node.PrintNodeName(self) # myGDScriptNode # Output: "my_gd_script_node" (or name of node where this code is placed).
# my_csharp_node.PrintNodeName() # This line will fail. my_csharp_node.PrintNodeName(self)
# This line will fail.
# my_csharp_node.PrintNodeName()
my_csharp_node.PrintNTimes("Hello there!", 2) # Hello there! Hello there! # Outputs "Hello there!" twice, once per line.
my_csharp_node.PrintNTimes("Hello there!", 2)
my_csharp_node.PrintArray(["a", "b", "c"]) # a, b, c # Output: "a", "b", "c" (one per line).
my_csharp_node.PrintArray([1, 2, 3]) # 1, 2, 3 my_csharp_node.PrintArray(["a", "b", "c"])
# Output: "1", "2", "3" (one per line).
my_csharp_node.PrintArray([1, 2, 3])
Calling GDScript methods from C# Calling GDScript methods from C#
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -191,22 +204,21 @@ to said method.
.. code-block:: csharp .. code-block:: csharp
myGDScriptNode.Call("print_node_name", this); // my_csharp_node // Output: "MyCSharpNode" (or name of node where this code is placed).
// myGDScriptNode.Call("print_node_name"); // This line will fail silently and won't error out. myGDScriptNode.Call("print_node_name", this);
// This line will fail silently and won't error out.
// myGDScriptNode.Call("print_node_name");
myGDScriptNode.Call("print_n_times", "Hello there!", 2); // Hello there! Hello there! // Outputs "Hello there!" twice, once per line.
myGDScriptNode.Call("print_n_times", "Hello there!", 2);
string[] arr = new string[] { "a", "b", "c" }; string[] arr = new string[] { "a", "b", "c" };
myGDScriptNode.Call("print_array", arr); // a, b, c // Output: "a", "b", "c" (one per line).
myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 }); // 1, 2, 3 myGDScriptNode.Call("print_array", arr);
// Note how the type of each array entry does not matter as long as it can be handled by the marshaller. // Output: "1", "2", "3" (one per line).
myGDScriptNode.Call("print_array", new int[] { 1, 2, 3 });
.. warning:: // Note how the type of each array entry does not matter
// as long as it can be handled by the marshaller.
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.
.. _connecting_to_signals_cross_language: .. _connecting_to_signals_cross_language: