|
305 | 305 | [/gdscript]
|
306 | 306 | [csharp]
|
307 | 307 | 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); |
309 | 309 | [/csharp]
|
310 | 310 | [/codeblocks]
|
311 | 311 | [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 | 323 | [/gdscript]
|
324 | 324 | [csharp]
|
325 | 325 | 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); |
327 | 327 | [/csharp]
|
328 | 328 | [/codeblocks]
|
329 | 329 | [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 | 342 | [/gdscript]
|
343 | 343 | [csharp]
|
344 | 344 | 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 }); |
346 | 346 | [/csharp]
|
347 | 347 | [/codeblocks]
|
348 | 348 | [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 | 394 |
|
395 | 395 | // This assumes that a `Player` class exists, which defines a `Hit` signal.
|
396 | 396 | 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); |
399 | 399 | }
|
400 | 400 |
|
401 | 401 | private void OnButtonDown()
|
|
405 | 405 |
|
406 | 406 | private void OnPlayerHit(string weaponType, int damage)
|
407 | 407 | {
|
408 |
| - GD.Print(String.Format("Hit with weapon {0} for {1} damage.", weaponType, damage)); |
| 408 | + GD.Print($"Hit with weapon {weaponType} for {damage} damage."); |
409 | 409 | }
|
410 | 410 | [/csharp]
|
411 | 411 | [/codeblocks]
|
|
431 | 431 | public override void _Ready()
|
432 | 432 | {
|
433 | 433 | 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: |
441 | 435 | 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)); |
444 | 440 | }
|
445 | 441 |
|
446 | 442 | private void OnButtonDown()
|
|
458 | 454 | func _ready():
|
459 | 455 | # This assumes that a `Player` class exists, which defines a `hit` signal.
|
460 | 456 | var player = Player.new()
|
| 457 | + # Using Callable.bind(). |
461 | 458 | player.hit.connect(_on_player_hit.bind("sword", 100))
|
462 | 459 |
|
463 | 460 | # Parameters added when emitting the signal are passed first.
|
|
473 | 470 | {
|
474 | 471 | // This assumes that a `Player` class exists, which defines a `Hit` signal.
|
475 | 472 | 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); |
480 | 476 |
|
481 | 477 | // Parameters added when emitting the signal are passed first.
|
482 |
| - player.EmitSignal("hit", "Dark lord", 5); |
| 478 | + player.EmitSignal(SignalName.Hit, "Dark lord", 5); |
483 | 479 | }
|
484 | 480 |
|
485 | 481 | // We pass two arguments when emitting (`hit_by`, `level`),
|
486 | 482 | // and bind two more arguments when connecting (`weapon_type`, `damage`).
|
487 | 483 | private void OnPlayerHit(string hitBy, int level, string weaponType, int damage)
|
488 | 484 | {
|
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."); |
490 | 486 | }
|
491 | 487 | [/csharp]
|
492 | 488 | [/codeblocks]
|
|
512 | 508 | emit_signal("game_over")
|
513 | 509 | [/gdscript]
|
514 | 510 | [csharp]
|
515 |
| - EmitSignal("Hit", "sword", 100); |
516 |
| - EmitSignal("GameOver"); |
| 511 | + EmitSignal(SignalName.Hit, "sword", 100); |
| 512 | + EmitSignal(SignalName.GameOver); |
517 | 513 | [/csharp]
|
518 | 514 | [/codeblocks]
|
519 | 515 | [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 | 577 | var b = node.GetIndexed("position:y"); // b is -10
|
582 | 578 | [/csharp]
|
583 | 579 | [/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. |
585 | 581 | [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.
|
586 | 582 | </description>
|
587 | 583 | </method>
|
|
868 | 864 | GD.Print(node.Position); // Prints (42, -10)
|
869 | 865 | [/csharp]
|
870 | 866 | [/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. |
872 | 868 | </description>
|
873 | 869 | </method>
|
874 | 870 | <method name="set_message_translation">
|
|
0 commit comments