|
@@ -24,8 +24,8 @@ Timer example
|
|
|
-------------
|
|
|
|
|
|
To see how signals work, let's try using a :ref:`Timer <class_Timer>` node. Create
|
|
|
-a new scene with a Node and two children: a Timer and a :ref:`Sprite <class_Sprite>`.
|
|
|
-In the Scene dock, rename Node to TimerExample.
|
|
|
+a new scene with a Node2D and two children: a Timer and a :ref:`Sprite <class_Sprite>`.
|
|
|
+In the Scene dock, rename Node2D to TimerExample.
|
|
|
|
|
|
For the Sprite's texture, you can use the Godot icon, or any other image you
|
|
|
like. Do so by selecting ``Load`` in the Sprite's Texture attribute drop-down menu.
|
|
@@ -46,20 +46,21 @@ the Timer reaches ``0``.
|
|
|
|
|
|
.. image:: img/signals_node_tab_timer.png
|
|
|
|
|
|
-Click on the "timeout()" signal and click "Connect...". You'll see the following
|
|
|
-window, where you can define how you want to connect the signal:
|
|
|
+Click on the "timeout()" signal and click "Connect..." at the bottom of the signals
|
|
|
+panel. You'll see the following window, where you can define how you want to connect
|
|
|
+the signal:
|
|
|
|
|
|
.. image:: img/signals_connect_dialog_timer.png
|
|
|
|
|
|
On the left side, you'll see the nodes in your scene and can select the node that
|
|
|
-you want to "listen" for the signal. Note that the Timer node is red - this is
|
|
|
-*not* an error, but is a visual indication that it's the node that is emitting
|
|
|
-the signal. Select the root node.
|
|
|
+you want to "listen" for the signal. Note that the Timer node is blue, this is a
|
|
|
+visual indication that it's the node that is emitting the signal. Select the root
|
|
|
+node.
|
|
|
|
|
|
.. warning:: The target node *must* have a script attached or you'll receive
|
|
|
an error message.
|
|
|
|
|
|
-On the bottom of the window is a field labeled "Method In Node". This is the name
|
|
|
+On the bottom of the window is a field labeled "Receiver Method". This is the name
|
|
|
of the function in the target node's script that you want to use. By default,
|
|
|
Godot will create this function using the naming convention ``_on_<node_name>_<signal_name>``
|
|
|
but you can change it if you wish.
|
|
@@ -69,14 +70,14 @@ Click "Connect" and you'll see that the function has been created in the script:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends Node
|
|
|
+ extends Node2D
|
|
|
|
|
|
func _on_Timer_timeout():
|
|
|
pass # replace with function body
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public class TimerExample : Node
|
|
|
+ public class TimerExample : Node2D
|
|
|
{
|
|
|
private void _on_Timer_timeout()
|
|
|
{
|
|
@@ -90,7 +91,7 @@ the signal is received. Let's make the Sprite blink:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends Node
|
|
|
+ extends Node2D
|
|
|
|
|
|
func _on_Timer_timeout():
|
|
|
# Note: the `$` operator is a shorthand for `get_node()`,
|
|
@@ -99,7 +100,7 @@ the signal is received. Let's make the Sprite blink:
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public class TimerExample : Node
|
|
|
+ public class TimerExample : Node2D
|
|
|
{
|
|
|
public void _on_Timer_timeout()
|
|
|
{
|
|
@@ -131,7 +132,7 @@ Here is the code for our Timer connection:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends Node
|
|
|
+ extends Node2D
|
|
|
|
|
|
func _ready():
|
|
|
$Timer.connect("timeout", self, "_on_Timer_timeout")
|
|
@@ -141,7 +142,7 @@ Here is the code for our Timer connection:
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public class TimerExample : Node
|
|
|
+ public class TimerExample : Node2D
|
|
|
{
|
|
|
public override void _Ready()
|
|
|
{
|
|
@@ -164,13 +165,13 @@ You can also declare your own custom signals in Godot:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends Node
|
|
|
+ extends Node2D
|
|
|
|
|
|
signal my_signal
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public class Main : Node
|
|
|
+ public class Main : Node2D
|
|
|
{
|
|
|
[Signal]
|
|
|
public delegate void MySignal();
|
|
@@ -184,7 +185,7 @@ To emit a signal via code, use the ``emit_signal`` function:
|
|
|
.. tabs::
|
|
|
.. code-tab:: gdscript GDScript
|
|
|
|
|
|
- extends Node
|
|
|
+ extends Node2D
|
|
|
|
|
|
signal my_signal
|
|
|
|
|
@@ -193,7 +194,7 @@ To emit a signal via code, use the ``emit_signal`` function:
|
|
|
|
|
|
.. code-tab:: csharp
|
|
|
|
|
|
- public class Main : Node
|
|
|
+ public class Main : Node2D
|
|
|
{
|
|
|
[Signal]
|
|
|
public delegate void MySignal();
|