.. Intention: give the user a first taste of signals. We should write more documentation in the scripting/ section. .. Note: GDScript snippets use one line return instead of two because they're really short. .. meta:: :keywords: Signal .. _doc_signals: Using signals ============= In this lesson, we will look at signals. They are messages that nodes emit when something specific happens to them, like a button being pressed. Other nodes can connect to that signal and call a function when the event occurs. Signals are a delegation mechanism built into Godot that allows one game object to react to a change in another without them referencing one another. Using signals limits `coupling ( https://en.wikipedia.org/wiki/Coupling_(computer_programming) )`_ and keeps your code flexible. For example, you might have a life bar on the screen that represents the player’s health. When the player takes damage or uses a healing potion, you want the bar to reflect the change. To do so, in Godot, you would use signals. .. note:: As mentioned in the introduction, signals are Godot's version of the observer pattern. You can learn more about it here: https://gameprogrammingpatterns.com/observer.html We will now use a signal to make our Godot icon from the previous lesson (`doc_scripting_player_input`) move and stop by pressing a button. .. Example Scene setup ----------- To add a button to our game, we will create a new "main" scene which will include both a button and the `Sprite.tscn` scene that we scripted in previous lessons. Create a new scene by going to the menu Scene -> New Scene. ![](img/signals_01_new_scene.png) In the Scene dock, click the 2D Scene button. This will add a Node2D as our root. ![](img/signals_02_2d_scene.png) In the FileSystem dock, click and drag the `Sprite.tscn` file you saved previously onto the Node2D to instantiate it. ![](img/signals_03_dragging_scene.png) We want to add another node as a sibling of the Sprite. To do so, right-click on Node2D and select Add Child Node. ![](img/signals_04_add_child_node.png) Search for the Button node type and add it. ![](img/signals_05_add_button.png) The node is small by default. Click and drag on the bottom-right handle of the Button in the viewport to resize it. ![](img/signals_06_drag_button.png) If you don't see the handles, ensure the select tool is active in the toolbar. ![](img/signals_07_select_tool.png) Click and drag on the button itself to move it closer to the sprite. You can also write a label on the Button by editing its Text property in the Inspector. Enter "Toggle motion". ![](img/signals_08_toggle_motion_text.png) Your scene tree and viewport should look like this. ![](img/signals_09_scene_setup.png) Save your newly created scene. You can then run it with :kbd:`F6`. At the moment, the button will be visible, but nothing will happen if you press it. Connecting a signal in the editor --------------------------------- Here, we want to connect the Button's "pressed" signal to our Sprite, and we want to call a new function that will toggle its motion on and off. We need to have a script attached to the Sprite node, which we do from the previous lesson. You can connect signals in the Node dock. Select the Button node and, on the right side of the editor, click on the tab named "Node" next to the Inspector. ![](img/signals_10_node_dock.png) The dock displays a list of signals available on the selected node. ![](img/signals_11_pressed_signals.png) Double-click the "pressed" signal to open the node connection window. ![](img/signals_12_node_connection.png) There, you can connect the signal to the Sprite node. The node needs a receiver method, a function that Godot will call when the Button emits the signal. The editor generates one for you. By convention, we name these callback methods "_on_NodeName_signal_name". Here, it'll be "_on_Button_pressed". .. note:: When connecting signals via the editor's Node dock, you can use two modes. The simple one only allows you to connect to nodes that have a script attached to them and creates a new callback function on them. ![](img/signals_advanced_connection_window.png) The advanced view lets you connect to any node and any built-in function, add arguments to the callback, and set options. You can toggle the mode in the window's bottom-right by clicking the Advanced button. Click the Connect button to complete the signal connection and jump to the Script workspace. You should see the new method with a connection icon in the left margin. ![](img/signals_13_signals_connection_icon.png) If you click the icon, a window pops up and displays information about the connection. This feature is only available when connecting nodes in the editor. ![](img/signals_14_signals_connection_info.png) Let's replace the line with the `pass` keyword with code that'll toggle the node's motion. Our Sprite moves thanks to code in the `_process()` function. Godot provides a method to toggle processing on and off: `Node.set_process()