mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-05 22:09:56 +03:00
Update & fix C# blocks under tutorials/scripting/
- `_Process` and `_PhysicsProcess` take their delta argument as a `double` - Use string interpolation instead of concatenating strings - Use `string` instead of `String` (`using System` was removed from everywhere in a previous PR) - Use `System.Array.Empty<T>()` to init empty arrays - Use `SignalName.*` instead of `nameof(*)` for signals - Do not use `event` as an argument name (that's a keyword) - Match node paths to screenshots when retrieving nodes from the tree - Add a C# example for scene unique nodes - Update the "Cross language scripting" page - Add a note about using `is` against `null`
This commit is contained in:
@@ -218,7 +218,7 @@ Here's a blank C# script with some comments to demonstrate how it works.
|
||||
GD.Print("Hello from C# to Godot :)");
|
||||
}
|
||||
|
||||
public override void _Process(float delta)
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
// Called every frame. Delta is time since the last frame.
|
||||
// Update game logic here.
|
||||
|
||||
@@ -466,7 +466,7 @@ GDScript C#
|
||||
``PackedByteArray`` ``byte[]``
|
||||
``PackedFloat32Array`` ``float[]``
|
||||
``PackedFloat64Array`` ``double[]``
|
||||
``PackedStringArray`` ``String[]``
|
||||
``PackedStringArray`` ``string[]``
|
||||
``PackedColorArray`` ``Color[]``
|
||||
``PackedVector2Array`` ``Vector2[]``
|
||||
``PackedVector3Array`` ``Vector3[]``
|
||||
|
||||
@@ -307,9 +307,9 @@ Exported arrays should be initialized empty.
|
||||
.. code-block:: csharp
|
||||
|
||||
[Export]
|
||||
private Vector3[] Vector3s = new Vector3[0];
|
||||
private Vector3[] Vector3s = System.Array.Empty<Vector3>();
|
||||
[Export]
|
||||
private String[] String = new String[0];
|
||||
private string[] Strings = System.Array.Empty<string>();
|
||||
|
||||
|
||||
You can omit the default value, but then it would be null if not assigned.
|
||||
|
||||
@@ -67,7 +67,7 @@ Generic methods are also provided to make this type conversion transparent.
|
||||
|
||||
To check if the node can be cast to Sprite2D, you can use the ``is`` operator.
|
||||
The ``is`` operator returns false if the node cannot be cast to Sprite2D,
|
||||
otherwise it returns true.
|
||||
otherwise it returns true. Note that using the ``is`` operator against ``null`` is always going to be falsy.
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
@@ -76,6 +76,11 @@ otherwise it returns true.
|
||||
// Yup, it's a Sprite2D!
|
||||
}
|
||||
|
||||
if (null is Sprite2D)
|
||||
{
|
||||
// This block can never happen.
|
||||
}
|
||||
|
||||
For more advanced type checking, you can look into `Pattern Matching <https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching>`_.
|
||||
|
||||
.. _doc_c_sharp_signals:
|
||||
@@ -107,7 +112,7 @@ If you want to connect a signal in the editor, you need to (re)build the project
|
||||
|
||||
public void MyCallbackWithArguments(string foo, int bar)
|
||||
{
|
||||
GD.Print("My callback with: ", foo, " and ", bar, "!");
|
||||
GD.Print($"My callback with: {foo} and {bar}!");
|
||||
}
|
||||
|
||||
public void SomeFunction()
|
||||
@@ -122,11 +127,11 @@ Emitting signals is done with the ``EmitSignal`` method.
|
||||
|
||||
public void SomeFunction()
|
||||
{
|
||||
EmitSignal(nameof(MySignal));
|
||||
EmitSignal(nameof(MySignalWithArguments), "hello there", 28);
|
||||
EmitSignal(SignalName.MySignal);
|
||||
EmitSignal(SignalName.MySignalWithArguments, "hello there", 28);
|
||||
}
|
||||
|
||||
Notice that you can always reference a signal name with the ``nameof`` keyword (applied on the delegate itself).
|
||||
Notice that you can always reference a signal name with its generated ``SignalName``.
|
||||
|
||||
It is possible to bind values when establishing a connection by passing a Godot array.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user