class_signal.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Signal.xml.
  6. .. _class_Signal:
  7. Signal
  8. ======
  9. Built-in type representing a signal defined in an object.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. **Signal** is a built-in :ref:`Variant<class_Variant>` type that represents a signal of an :ref:`Object<class_Object>` instance. Like all :ref:`Variant<class_Variant>` types, it can be stored in variables and passed to functions. Signals allow all connected :ref:`Callable<class_Callable>`\ s (and by extension their respective objects) to listen and react to events, without directly referencing one another. This keeps the code flexible and easier to manage.
  14. In GDScript, signals can be declared with the ``signal`` keyword. In C#, you may use the ``[Signal]`` attribute on a delegate.
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. signal attacked
  18. # Additional arguments may be declared.
  19. # These arguments must be passed when the signal is emitted.
  20. signal item_dropped(item_name, amount)
  21. .. code-tab:: csharp
  22. [Signal]
  23. delegate void Attacked();
  24. // Additional arguments may be declared.
  25. // These arguments must be passed when the signal is emitted.
  26. [Signal]
  27. delegate void ItemDropped(itemName: string, amount: int);
  28. .. rst-class:: classref-introduction-group
  29. Tutorials
  30. ---------
  31. - :doc:`Using Signals <../getting_started/step_by_step/signals>`
  32. - `GDScript Basics <../tutorials/scripting/gdscript/gdscript_basics.html#signals>`__
  33. .. rst-class:: classref-reftable-group
  34. Constructors
  35. ------------
  36. .. table::
  37. :widths: auto
  38. +-----------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`Signal<class_Signal>` | :ref:`Signal<class_Signal_constructor_Signal>` **(** **)** |
  40. +-----------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Signal<class_Signal>` | :ref:`Signal<class_Signal_constructor_Signal>` **(** :ref:`Signal<class_Signal>` from **)** |
  42. +-----------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Signal<class_Signal>` | :ref:`Signal<class_Signal_constructor_Signal>` **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` signal **)** |
  44. +-----------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
  45. .. rst-class:: classref-reftable-group
  46. Methods
  47. -------
  48. .. table::
  49. :widths: auto
  50. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`int<class_int>` | :ref:`connect<class_Signal_method_connect>` **(** :ref:`Callable<class_Callable>` callable, :ref:`int<class_int>` flags=0 **)** |
  52. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  53. | void | :ref:`disconnect<class_Signal_method_disconnect>` **(** :ref:`Callable<class_Callable>` callable **)** |
  54. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  55. | void | :ref:`emit<class_Signal_method_emit>` **(** ... **)** |vararg| |const| |
  56. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Array<class_Array>` | :ref:`get_connections<class_Signal_method_get_connections>` **(** **)** |const| |
  58. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`StringName<class_StringName>` | :ref:`get_name<class_Signal_method_get_name>` **(** **)** |const| |
  60. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Object<class_Object>` | :ref:`get_object<class_Signal_method_get_object>` **(** **)** |const| |
  62. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`int<class_int>` | :ref:`get_object_id<class_Signal_method_get_object_id>` **(** **)** |const| |
  64. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`is_connected<class_Signal_method_is_connected>` **(** :ref:`Callable<class_Callable>` callable **)** |const| |
  66. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`is_null<class_Signal_method_is_null>` **(** **)** |const| |
  68. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------+
  69. .. rst-class:: classref-reftable-group
  70. Operators
  71. ---------
  72. .. table::
  73. :widths: auto
  74. +-------------------------+----------------------------------------------------------------------------------------------------+
  75. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Signal_operator_neq_Signal>` **(** :ref:`Signal<class_Signal>` right **)** |
  76. +-------------------------+----------------------------------------------------------------------------------------------------+
  77. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Signal_operator_eq_Signal>` **(** :ref:`Signal<class_Signal>` right **)** |
  78. +-------------------------+----------------------------------------------------------------------------------------------------+
  79. .. rst-class:: classref-section-separator
  80. ----
  81. .. rst-class:: classref-descriptions-group
  82. Constructor Descriptions
  83. ------------------------
  84. .. _class_Signal_constructor_Signal:
  85. .. rst-class:: classref-constructor
  86. :ref:`Signal<class_Signal>` **Signal** **(** **)**
  87. Constructs an empty **Signal** with no object nor signal name bound.
  88. .. rst-class:: classref-item-separator
  89. ----
  90. .. rst-class:: classref-constructor
  91. :ref:`Signal<class_Signal>` **Signal** **(** :ref:`Signal<class_Signal>` from **)**
  92. Constructs a **Signal** as a copy of the given **Signal**.
  93. .. rst-class:: classref-item-separator
  94. ----
  95. .. rst-class:: classref-constructor
  96. :ref:`Signal<class_Signal>` **Signal** **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` signal **)**
  97. Creates a new **Signal** named ``signal`` in the specified ``object``.
  98. .. rst-class:: classref-section-separator
  99. ----
  100. .. rst-class:: classref-descriptions-group
  101. Method Descriptions
  102. -------------------
  103. .. _class_Signal_method_connect:
  104. .. rst-class:: classref-method
  105. :ref:`int<class_int>` **connect** **(** :ref:`Callable<class_Callable>` callable, :ref:`int<class_int>` flags=0 **)**
  106. Connects this signal to the specified ``callable``. Optional ``flags`` can be also added to configure the connection's behavior (see :ref:`ConnectFlags<enum_Object_ConnectFlags>` constants). You can provide additional arguments to the connected ``callable`` by using :ref:`Callable.bind<class_Callable_method_bind>`.
  107. A signal can only be connected once to the same :ref:`Callable<class_Callable>`. If the signal is already connected, returns :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` and pushes an error message, unless the signal is connected with :ref:`Object.CONNECT_REFERENCE_COUNTED<class_Object_constant_CONNECT_REFERENCE_COUNTED>`. To prevent this, use :ref:`is_connected<class_Signal_method_is_connected>` first to check for existing connections.
  108. ::
  109. for button in $Buttons.get_children():
  110. button.pressed.connect(_on_pressed.bind(button))
  111. func _on_pressed(button):
  112. print(button.name, " was pressed")
  113. .. rst-class:: classref-item-separator
  114. ----
  115. .. _class_Signal_method_disconnect:
  116. .. rst-class:: classref-method
  117. void **disconnect** **(** :ref:`Callable<class_Callable>` callable **)**
  118. Disconnects this signal from the specified :ref:`Callable<class_Callable>`. If the connection does not exist, generates an error. Use :ref:`is_connected<class_Signal_method_is_connected>` to make sure that the connection exists.
  119. .. rst-class:: classref-item-separator
  120. ----
  121. .. _class_Signal_method_emit:
  122. .. rst-class:: classref-method
  123. void **emit** **(** ... **)** |vararg| |const|
  124. Emits this signal. All :ref:`Callable<class_Callable>`\ s connected to this signal will be triggered. This method supports a variable number of arguments, so parameters can be passed as a comma separated list.
  125. .. rst-class:: classref-item-separator
  126. ----
  127. .. _class_Signal_method_get_connections:
  128. .. rst-class:: classref-method
  129. :ref:`Array<class_Array>` **get_connections** **(** **)** |const|
  130. Returns the list of :ref:`Callable<class_Callable>`\ s connected to this signal.
  131. .. rst-class:: classref-item-separator
  132. ----
  133. .. _class_Signal_method_get_name:
  134. .. rst-class:: classref-method
  135. :ref:`StringName<class_StringName>` **get_name** **(** **)** |const|
  136. Returns the name of this signal.
  137. .. rst-class:: classref-item-separator
  138. ----
  139. .. _class_Signal_method_get_object:
  140. .. rst-class:: classref-method
  141. :ref:`Object<class_Object>` **get_object** **(** **)** |const|
  142. Returns the object emitting this signal.
  143. .. rst-class:: classref-item-separator
  144. ----
  145. .. _class_Signal_method_get_object_id:
  146. .. rst-class:: classref-method
  147. :ref:`int<class_int>` **get_object_id** **(** **)** |const|
  148. Returns the ID of the object emitting this signal (see :ref:`Object.get_instance_id<class_Object_method_get_instance_id>`).
  149. .. rst-class:: classref-item-separator
  150. ----
  151. .. _class_Signal_method_is_connected:
  152. .. rst-class:: classref-method
  153. :ref:`bool<class_bool>` **is_connected** **(** :ref:`Callable<class_Callable>` callable **)** |const|
  154. Returns ``true`` if the specified :ref:`Callable<class_Callable>` is connected to this signal.
  155. .. rst-class:: classref-item-separator
  156. ----
  157. .. _class_Signal_method_is_null:
  158. .. rst-class:: classref-method
  159. :ref:`bool<class_bool>` **is_null** **(** **)** |const|
  160. Returns ``true`` if the signal's name does not exist in its object, or the object is not valid.
  161. .. rst-class:: classref-section-separator
  162. ----
  163. .. rst-class:: classref-descriptions-group
  164. Operator Descriptions
  165. ---------------------
  166. .. _class_Signal_operator_neq_Signal:
  167. .. rst-class:: classref-operator
  168. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Signal<class_Signal>` right **)**
  169. Returns ``true`` if the signals do not share the same object and name.
  170. .. rst-class:: classref-item-separator
  171. ----
  172. .. _class_Signal_operator_eq_Signal:
  173. .. rst-class:: classref-operator
  174. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Signal<class_Signal>` right **)**
  175. Returns ``true`` if both signals share the same object and name.
  176. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  177. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  178. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  179. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  180. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  181. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`