Skip to content

Commit 4fa6edc

Browse files
committed
Merge pull request #64930 from raulsntos/dotnet/docs
Update C# signal documentation and remove bind array
2 parents a17e0a0 + 92e4b4e commit 4fa6edc

9 files changed

+48
-50
lines changed

doc/classes/Callable.xml

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818
callable.call("invalid") # Invalid call, should have at least 2 arguments.
1919
[/gdscript]
2020
[csharp]
21-
public void PrintArgs(object arg1, object arg2, object arg3 = null)
21+
// Default parameter values are not supported.
22+
public void PrintArgs(Variant arg1, Variant arg2, Variant arg3 = default)
2223
{
2324
GD.PrintS(arg1, arg2, arg3);
2425
}
2526

2627
public void Test()
2728
{
28-
Callable callable = new Callable(this, nameof(PrintArgs));
29-
callable.Call("hello", "world"); // Prints "hello world null".
29+
// Invalid calls fail silently.
30+
Callable callable = new Callable(this, MethodName.PrintArgs);
31+
callable.Call("hello", "world"); // Default parameter values are not supported, should have 3 arguments.
3032
callable.Call(Vector2.Up, 42, callable); // Prints "(0, -1) 42 Node(Node.cs)::PrintArgs".
31-
callable.Call("invalid"); // Invalid call, should have at least 2 arguments.
33+
callable.Call("invalid"); // Invalid call, should have 3 arguments.
3234
}
3335
[/csharp]
3436
[/codeblocks]

doc/classes/EditorCommandPalette.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[csharp]
1717
EditorCommandPalette commandPalette = GetEditorInterface().GetCommandPalette();
1818
// ExternalCommand is a function that will be called with the command is executed.
19-
Callable commandCallable = new Callable(this, nameof(ExternalCommand));
19+
Callable commandCallable = new Callable(this, MethodName.ExternalCommand);
2020
commandPalette.AddCommand("command", "test/command", commandCallable)
2121
[/csharp]
2222
[/codeblocks]

doc/classes/Expression.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public override void _Ready()
3030
{
31-
GetNode("LineEdit").TextSubmitted += OnTextEntered;
31+
GetNode<LineEdit>("LineEdit").TextSubmitted += OnTextEntered;
3232
}
3333

3434
private void OnTextEntered(string command)

doc/classes/HTTPRequest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
}
7070

7171
// Called when the HTTP request is completed.
72-
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
72+
private void HttpRequestCompleted(int result, int responseCode, string[] headers, byte[] body)
7373
{
7474
var json = new JSON();
7575
json.Parse(body.GetStringFromUTF8());
@@ -128,7 +128,7 @@
128128
}
129129

130130
// Called when the HTTP request is completed.
131-
private void HttpRequestCompleted(int result, int response_code, string[] headers, byte[] body)
131+
private void HttpRequestCompleted(int result, int responseCode, string[] headers, byte[] body)
132132
{
133133
if (result != (int)HTTPRequest.Result.Success)
134134
{

doc/classes/Object.xml

+21-25
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
[/gdscript]
306306
[csharp]
307307
var node = new Node3D();
308-
node.Call("rotate", new Vector3(1f, 0f, 0f), 1.571f);
308+
node.Call(Node3D.MethodName.Rotate, new Vector3(1f, 0f, 0f), 1.571f);
309309
[/csharp]
310310
[/codeblocks]
311311
[b]Note:[/b] In C#, [param method] must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the [code]MethodName[/code] class to avoid allocating a new [StringName] on each call.
@@ -323,7 +323,7 @@
323323
[/gdscript]
324324
[csharp]
325325
var node = new Node3D();
326-
node.CallDeferred("rotate", new Vector3(1f, 0f, 0f), 1.571f);
326+
node.CallDeferred(Node3D.MethodName.Rotate, new Vector3(1f, 0f, 0f), 1.571f);
327327
[/csharp]
328328
[/codeblocks]
329329
[b]Note:[/b] In C#, [param method] must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the [code]MethodName[/code] class to avoid allocating a new [StringName] on each call.
@@ -342,7 +342,7 @@
342342
[/gdscript]
343343
[csharp]
344344
var node = new Node3D();
345-
node.Callv("rotate", new Godot.Collections.Array { new Vector3(1f, 0f, 0f), 1.571f });
345+
node.Callv(Node3D.MethodName.Rotate, new Godot.Collections.Array { new Vector3(1f, 0f, 0f), 1.571f });
346346
[/csharp]
347347
[/codeblocks]
348348
[b]Note:[/b] In C#, [param method] must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the [code]MethodName[/code] class to avoid allocating a new [StringName] on each call
@@ -394,8 +394,8 @@
394394

395395
// This assumes that a `Player` class exists, which defines a `Hit` signal.
396396
var player = new Player();
397-
// Signals as events (`player.Hit += OnPlayerHit;`) do not support argument binding. You have to use:
398-
player.Hit.Connect(OnPlayerHit, new Godot.Collections.Array {"sword", 100 });
397+
// We can use lambdas when we need to bind additional parameters.
398+
player.Hit += () => OnPlayerHit("sword", 100);
399399
}
400400

401401
private void OnButtonDown()
@@ -405,7 +405,7 @@
405405

406406
private void OnPlayerHit(string weaponType, int damage)
407407
{
408-
GD.Print(String.Format("Hit with weapon {0} for {1} damage.", weaponType, damage));
408+
GD.Print($"Hit with weapon {weaponType} for {damage} damage.");
409409
}
410410
[/csharp]
411411
[/codeblocks]
@@ -431,16 +431,12 @@
431431
public override void _Ready()
432432
{
433433
var button = new Button();
434-
// Option 1: Object.Connect() with an implicit Callable for the defined function.
435-
button.Connect("button_down", OnButtonDown);
436-
// Option 2: Object.connect() with a constructed Callable using a target object and method name.
437-
button.Connect("button_down", new Callable(self, nameof(OnButtonDown)));
438-
// Option 3: Signal.connect() with an implicit Callable for the defined function.
439-
button.ButtonDown.Connect(OnButtonDown);
440-
// Option 3b: In C#, we can use signals as events and connect with this more idiomatic syntax:
434+
// Option 1: In C#, we can use signals as events and connect with this idiomatic syntax:
441435
button.ButtonDown += OnButtonDown;
442-
// Option 4: Signal.connect() with a constructed Callable using a target object and method name.
443-
button.ButtonDown.Connect(new Callable(self, nameof(OnButtonDown)));
436+
// Option 2: Object.Connect() with a constructed Callable from a method group.
437+
button.Connect(Button.SignalName.ButtonDown, Callable.From(OnButtonDown));
438+
// Option 3: Object.Connect() with a constructed Callable using a target object and method name.
439+
button.Connect(Button.SignalName.ButtonDown, new Callable(this, MethodName.OnButtonDown));
444440
}
445441

446442
private void OnButtonDown()
@@ -458,6 +454,7 @@
458454
func _ready():
459455
# This assumes that a `Player` class exists, which defines a `hit` signal.
460456
var player = Player.new()
457+
# Using Callable.bind().
461458
player.hit.connect(_on_player_hit.bind("sword", 100))
462459

463460
# Parameters added when emitting the signal are passed first.
@@ -473,20 +470,19 @@
473470
{
474471
// This assumes that a `Player` class exists, which defines a `Hit` signal.
475472
var player = new Player();
476-
// Option 1: Using Callable.Bind(). This way we can still use signals as events.
477-
player.Hit += OnPlayerHit.Bind("sword", 100);
478-
// Option 2: Using a `binds` Array in Signal.Connect().
479-
player.Hit.Connect(OnPlayerHit, new Godot.Collections.Array{ "sword", 100 });
473+
// Using lambda expressions that create a closure that captures the additional parameters.
474+
// The lambda only receives the parameters defined by the signal's delegate.
475+
player.Hit += (hitBy, level) => OnPlayerHit(hitBy, level, "sword", 100);
480476

481477
// Parameters added when emitting the signal are passed first.
482-
player.EmitSignal("hit", "Dark lord", 5);
478+
player.EmitSignal(SignalName.Hit, "Dark lord", 5);
483479
}
484480

485481
// We pass two arguments when emitting (`hit_by`, `level`),
486482
// and bind two more arguments when connecting (`weapon_type`, `damage`).
487483
private void OnPlayerHit(string hitBy, int level, string weaponType, int damage)
488484
{
489-
GD.Print(String.Format("Hit by {0} (level {1}) with weapon {2} for {3} damage.", hitBy, level, weaponType, damage));
485+
GD.Print($"Hit by {hitBy} (level {level}) with weapon {weaponType} for {damage} damage.");
490486
}
491487
[/csharp]
492488
[/codeblocks]
@@ -512,8 +508,8 @@
512508
emit_signal("game_over")
513509
[/gdscript]
514510
[csharp]
515-
EmitSignal("Hit", "sword", 100);
516-
EmitSignal("GameOver");
511+
EmitSignal(SignalName.Hit, "sword", 100);
512+
EmitSignal(SignalName.GameOver);
517513
[/csharp]
518514
[/codeblocks]
519515
[b]Note:[/b] In C#, [param signal] must be in snake_case when referring to built-in Godot signals. Prefer using the names exposed in the [code]SignalName[/code] class to avoid allocating a new [StringName] on each call.
@@ -581,7 +577,7 @@
581577
var b = node.GetIndexed("position:y"); // b is -10
582578
[/csharp]
583579
[/codeblocks]
584-
[b]Note:[/b] In C#, [param property_path] must be in snake_case when referring to built-in Godot properties.
580+
[b]Note:[/b] In C#, [param property_path] must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the [code]PropertyName[/code] class to avoid allocating a new [StringName] on each call.
585581
[b]Note:[/b] This method does not support actual paths to nodes in the [SceneTree], only sub-property paths. In the context of nodes, use [method Node.get_node_and_resource] instead.
586582
</description>
587583
</method>
@@ -868,7 +864,7 @@
868864
GD.Print(node.Position); // Prints (42, -10)
869865
[/csharp]
870866
[/codeblocks]
871-
[b]Note:[/b] In C#, [param property_path] must be in snake_case when referring to built-in Godot properties.
867+
[b]Note:[/b] In C#, [param property_path] must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the [code]PropertyName[/code] class to avoid allocating a new [StringName] on each call.
872868
</description>
873869
</method>
874870
<method name="set_message_translation">

doc/classes/Performance.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
[csharp]
4646
public override void _Ready()
4747
{
48-
var monitorValue = new Callable(this, nameof(GetMonitorValue));
48+
var monitorValue = new Callable(this, MethodName.GetMonitorValue);
4949

5050
// Adds monitor with name "MyName" to category "MyCategory".
5151
Performance.AddCustomMonitor("MyCategory/MyMonitor", monitorValue);

doc/classes/Signal.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
[/gdscript]
1717
[csharp]
1818
[Signal]
19-
delegate void Attacked();
19+
delegate void AttackedEventHandler();
2020

2121
// Additional arguments may be declared.
2222
// These arguments must be passed when the signal is emitted.
2323
[Signal]
24-
delegate void ItemDropped(itemName: string, amount: int);
24+
delegate void ItemDroppedEventHandler(string itemName, int amount);
2525
[/csharp]
2626
[/codeblocks]
2727
</description>

doc/classes/Tween.xml

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Tween tween = GetTree().CreateTween();
2020
tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
2121
tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f);
22-
tween.TweenCallback(new Callable(GetNode("Sprite").QueueFree));
22+
tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
2323
[/csharp]
2424
[/codeblocks]
2525
This sequence will make the [code]$Sprite[/code] node turn red, then shrink, before finally calling [method Node.queue_free] to free the sprite. [Tweener]s are executed one after another by default. This behavior can be changed using [method parallel] and [method set_parallel].
@@ -35,7 +35,7 @@
3535
Tween tween = GetTree().CreateTween();
3636
tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f).SetTrans(Tween.TransitionType.Sine);
3737
tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f).SetTrans(Tween.TransitionType.Bounce);
38-
tween.TweenCallback(new Callable(GetNode("Sprite").QueueFree));
38+
tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
3939
[/csharp]
4040
[/codeblocks]
4141
Most of the [Tween] methods can be chained this way too. In the following example the [Tween] is bound to the running script's node and a default transition is set for its [Tweener]s:
@@ -50,7 +50,7 @@
5050
var tween = GetTree().CreateTween().BindNode(this).SetTrans(Tween.TransitionType.Elastic);
5151
tween.TweenProperty(GetNode("Sprite"), "modulate", Colors.Red, 1.0f);
5252
tween.TweenProperty(GetNode("Sprite"), "scale", Vector2.Zero, 1.0f);
53-
tween.TweenCallback(new Callable(GetNode("Sprite").QueueFree));
53+
tween.TweenCallback(Callable.From(GetNode("Sprite").QueueFree));
5454
[/csharp]
5555
[/codeblocks]
5656
Another interesting use for [Tween]s is animating arbitrary sets of objects:
@@ -281,7 +281,7 @@
281281
[/gdscript]
282282
[csharp]
283283
Tween tween = GetTree().CreateTween().SetLoops();
284-
tween.TweenCallback(new Callable(Shoot)).SetDelay(1.0f);
284+
tween.TweenCallback(Callable.From(Shoot)).SetDelay(1.0f);
285285
[/csharp]
286286
[/codeblocks]
287287
[b]Example:[/b] Turning a sprite red and then blue, with 2 second delay:
@@ -294,8 +294,8 @@
294294
[csharp]
295295
Tween tween = GetTree().CreateTween();
296296
Sprite2D sprite = GetNode&lt;Sprite2D&gt;("Sprite");
297-
tween.TweenCallback(new Callable(() =&gt; sprite.Modulate = Colors.Red)).SetDelay(2.0f);
298-
tween.TweenCallback(new Callable(() =&gt; sprite.Modulate = Colors.Blue)).SetDelay(2.0f);
297+
tween.TweenCallback(Callable.From(() =&gt; sprite.Modulate = Colors.Red)).SetDelay(2.0f);
298+
tween.TweenCallback(Callable.From(() =&gt; sprite.Modulate = Colors.Blue)).SetDelay(2.0f);
299299
[/csharp]
300300
[/codeblocks]
301301
</description>
@@ -332,10 +332,10 @@
332332
[csharp]
333333
Tween tween = CreateTween().SetLoops();
334334
tween.TweenProperty(GetNode("Sprite"), "position:x", 200.0f, 1.0f).AsRelative();
335-
tween.TweenCallback(new Callable(Jump));
335+
tween.TweenCallback(Callable.From(Jump));
336336
tween.TweenInterval(2.0f);
337337
tween.TweenProperty(GetNode("Sprite"), "position:x", -200.0f, 1.0f).AsRelative();
338-
tween.TweenCallback(new Callable(Jump));
338+
tween.TweenCallback(Callable.From(Jump));
339339
tween.TweenInterval(2.0f);
340340
[/csharp]
341341
[/codeblocks]
@@ -357,7 +357,7 @@
357357
[/gdscript]
358358
[csharp]
359359
Tween tween = CreateTween();
360-
tween.TweenMethod(new Callable(() =&gt; LookAt(Vector3.Up)), new Vector3(-1.0f, 0.0f, -1.0f), new Vector3(1.0f, 0.0f, -1.0f), 1.0f); // The LookAt() method takes up vector as second argument.
360+
tween.TweenMethod(Callable.From(() =&gt; LookAt(Vector3.Up)), new Vector3(-1.0f, 0.0f, -1.0f), new Vector3(1.0f, 0.0f, -1.0f), 1.0f); // The LookAt() method takes up vector as second argument.
361361
[/csharp]
362362
[/codeblocks]
363363
[b]Example:[/b] Setting the text of a [Label], using an intermediate method and after a delay:
@@ -376,7 +376,7 @@
376376
base._Ready();
377377

378378
Tween tween = CreateTween();
379-
tween.TweenMethod(new Callable(SetLabelText), 0.0f, 10.0f, 1.0f).SetDelay(1.0f);
379+
tween.TweenMethod(Callable.From&lt;int&gt;(SetLabelText), 0.0f, 10.0f, 1.0f).SetDelay(1.0f);
380380
}
381381

382382
private void SetLabelText(int value)

doc/classes/UndoRedo.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
{
4949
var node = GetNode&lt;Node2D&gt;("MyNode2D");
5050
UndoRedo.CreateAction("Move the node");
51-
UndoRedo.AddDoMethod(this, nameof(DoSomething));
52-
UndoRedo.AddUndoMethod(this, nameof(UndoSomething));
53-
UndoRedo.AddDoProperty(node, "position", new Vector2(100, 100));
54-
UndoRedo.AddUndoProperty(node, "position", node.Position);
51+
UndoRedo.AddDoMethod(this, MethodName.DoSomething);
52+
UndoRedo.AddUndoMethod(this, MethodName.UndoSomething);
53+
UndoRedo.AddDoProperty(node, Node2D.PropertyName.Position, new Vector2(100, 100));
54+
UndoRedo.AddUndoProperty(node, Node2D.PropertyName.Position, node.Position);
5555
UndoRedo.CommitAction();
5656
}
5757
[/csharp]

0 commit comments

Comments
 (0)