Browse Source

Added C# code examples for Using signals page. (#6010)

* Added C# code examples for using signals page.

Also changed a bit one GDscript example.
Stanislav Balia 3 years ago
parent
commit
13fdc670de
1 changed files with 141 additions and 1 deletions
  1. 141 1
      getting_started/step_by_step/signals.rst

+ 141 - 1
getting_started/step_by_step/signals.rst

@@ -84,7 +84,7 @@ Your scene tree and viewport should look like this.
 
 .. image:: img/signals_09_scene_setup.png
 
-Save your newly created scene. You can then run it with :kbd:`F6`.
+Save your newly created scene. You can then run it with :kbd:`F6` (:kbd:`Cmd + R` on macOS).
 At the moment, the button will be visible, but nothing will happen if you
 press it.
 
@@ -153,6 +153,13 @@ the ``not`` keyword to invert the value.
     func _on_Button_pressed():
         set_process(not is_processing())
 
+ .. code-tab:: csharp C#
+
+    public void OnButtonPressed()
+    {
+        SetProcess(!IsProcessing());
+    }
+
 This function will toggle processing and, in turn, the icon's motion on and off
 upon pressing the button.
 
@@ -168,6 +175,15 @@ following code, which we saw two lessons ago:
         var velocity = Vector2.UP.rotated(rotation) * speed
         position += velocity * delta
 
+ .. code-tab:: csharp C#
+
+    public override void _Process(float delta)
+    {
+        Rotation += AngularSpeed * delta;
+        var velocity = Vector2.Up.Rotated(Rotation) * Speed;
+        Position += velocity * delta;
+    }
+
 Your complete ``Sprite2D.gd`` code should look like the following.
 
 .. tabs::
@@ -188,6 +204,28 @@ Your complete ``Sprite2D.gd`` code should look like the following.
     func _on_Button_pressed():
         set_process(not is_processing())
 
+ .. code-tab:: csharp C#
+
+    using Godot;
+
+    public class Sprite : Godot.Sprite2D
+    {
+        private float Speed = 400;
+        private float AngularSpeed = Mathf.Pi;
+
+        public override void _Process(float delta)
+        {
+            Rotation += AngularSpeed * delta;
+            var velocity = Vector2.Up.Rotated(Rotation) * Speed;
+            Position += velocity * delta;
+        }
+
+        public void OnButtonPressed()
+        {
+            SetProcess(!IsProcessing());
+        }
+    }
+
 Run the scene now and click the button to see the sprite start and stop.
 
 Connecting a signal via code
@@ -240,6 +278,13 @@ in a variable.
     func _ready():
         var timer = get_node("Timer")
 
+ .. code-tab:: csharp C#
+
+    public override void _Ready()
+    {
+        var timer = GetNode<Timer>("Timer");
+    }
+
 The function ``get_node()`` looks at the Sprite2D's children and gets nodes by
 their name. For example, if you renamed the Timer node to "BlinkingTimer" in the
 editor, you would have to change the call to ``get_node("BlinkingTimer")``.
@@ -255,6 +300,14 @@ We can now connect the Timer to the Sprite2D in the ``_ready()`` function.
         var timer = get_node("Timer")
         timer.timeout.connect(_on_Timer_timeout)
 
+ .. code-tab:: csharp C#
+
+    public override void _Ready()
+    {
+        var timer = GetNode<Timer>("Timer");
+        timer.Connect("timeout", this, nameof(OnTimerTimeout));
+    }
+
 The line reads like so: we connect the Timer's "timeout" signal to the node to
 which the script is attached. When the Timer emits ``timeout``, we want to call
 the function ``_on_Timer_timeout()``, that we need to define. Let's add it at the
@@ -266,6 +319,13 @@ bottom of our script and use it to toggle our sprite's visibility.
     func _on_Timer_timeout():
         visible = not visible
 
+ .. code-tab:: csharp C#
+
+    public void OnTimerTimeout()
+    {
+        Visible = !Visible;
+    }
+
 The ``visible`` property is a boolean that controls the visibility of our node.
 The line ``visible = not visible`` toggles the value. If ``visible`` is
 ``true``, it becomes ``false``, and vice-versa.
@@ -306,6 +366,39 @@ Here is the complete ``Sprite2D.gd`` file for reference.
     func _on_Timer_timeout():
         visible = not visible
 
+ .. code-tab:: csharp C#
+
+    using Godot;
+
+    public class Sprite : Godot.Sprite2D
+    {
+        private float Speed = 400;
+        private float AngularSpeed = Mathf.Pi;
+
+        public override void _Ready()
+        {
+            var timer = GetNode<Timer>("Timer");
+            timer.Connect("timeout", this, nameof(OnTimerTimeout));
+        }
+
+        public override void _Process(float delta)
+        {
+            Rotation += AngularSpeed * delta;
+            var velocity = Vector2.Up.Rotated(Rotation) * Speed;
+            Position += velocity * delta;
+        }
+
+        public void OnButtonPressed()
+        {
+            SetProcess(!IsProcessing());
+        }
+
+        public void OnTimerTimeout()
+        {
+            Visible = !Visible;
+        }
+    }
+
 Custom signals
 --------------
 
@@ -326,6 +419,18 @@ reaches 0.
 
     var health = 10
 
+ .. code-tab:: csharp C#
+
+    using Godot;
+
+    public class CustomSignal : Node2D
+    {
+        [Signal]
+        public delegate void HealthDepleted();
+
+        private int Health = 10;
+    }
+
 .. note:: As signals represent events that just occurred, we generally use an
           action verb in the past tense in their names.
 
@@ -344,6 +449,18 @@ To emit a signal in your scripts, call ``emit_signal()``.
         if health <= 0:
             emit_signal("health_depleted")
 
+ .. code-tab:: csharp C#
+
+    public void TakeDamage(int amount)
+    {
+        Health -= amount;
+
+        if (Health < 0)
+        {
+            EmitSignal(nameof(HealthDepleted));
+        }
+    }
+
 A signal can optionally declare one or more arguments. Specify the argument
 names between parentheses:
 
@@ -354,6 +471,20 @@ names between parentheses:
 
     signal health_changed(old_value, new_value)
 
+    var health = 10
+
+ .. code-tab:: csharp C#
+
+    using Godot;
+
+    public class CustomSignal : Node
+    {
+        [Signal]
+        public delegate void HealthChanged(int oldValue, int newValue);
+
+        private int Health = 10;
+    }
+
 .. note::
 
     The signal arguments show up in the editor's node dock, and Godot can use
@@ -372,6 +503,15 @@ To emit values along with the signal, add them as extra arguments to the
         health -= amount
         emit_signal("health_changed", old_health, health)
 
+ .. code-tab:: csharp C#
+
+    public void TakeDamage(int amount)
+    {
+        var oldHealth = Health;
+        Health -= amount;
+        EmitSignal(nameof(HealthChanged), oldHealth, Health);
+    }
+
 Summary
 -------