class_callable.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/Callable.xml.
  6. .. _class_Callable:
  7. Callable
  8. ========
  9. Built-in type representing a method in an object instance or a standalone function.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. **Callable** is a built-in :ref:`Variant<class_Variant>` type that represents a function. It can either be a method within an :ref:`Object<class_Object>` instance, or a standalone function not related to any object, like a lambda function. Like all :ref:`Variant<class_Variant>` types, it can be stored in variables and passed to other functions. It is most commonly used for signal callbacks.
  14. \ **Example:**\
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. func print_args(arg1, arg2, arg3 = ""):
  18. prints(arg1, arg2, arg3)
  19. func test():
  20. var callable = Callable(self, "print_args")
  21. callable.call("hello", "world") # Prints "hello world ".
  22. callable.call(Vector2.UP, 42, callable) # Prints "(0, -1) 42 Node(node.gd)::print_args".
  23. callable.call("invalid") # Invalid call, should have at least 2 arguments.
  24. .. code-tab:: csharp
  25. // Default parameter values are not supported.
  26. public void PrintArgs(Variant arg1, Variant arg2, Variant arg3 = default)
  27. {
  28. GD.PrintS(arg1, arg2, arg3);
  29. }
  30. public void Test()
  31. {
  32. // Invalid calls fail silently.
  33. Callable callable = new Callable(this, MethodName.PrintArgs);
  34. callable.Call("hello", "world"); // Default parameter values are not supported, should have 3 arguments.
  35. callable.Call(Vector2.Up, 42, callable); // Prints "(0, -1) 42 Node(Node.cs)::PrintArgs".
  36. callable.Call("invalid"); // Invalid call, should have 3 arguments.
  37. }
  38. In GDScript, it's possible to create lambda functions within a method. Lambda functions are custom callables that are not associated with an :ref:`Object<class_Object>` instance. Optionally, lambda functions can also be named. The name will be displayed in the debugger, or when calling :ref:`get_method<class_Callable_method_get_method>`.
  39. ::
  40. func _init():
  41. var my_lambda = func (message):
  42. print(message)
  43. # Prints Hello everyone!
  44. my_lambda.call("Hello everyone!")
  45. # Prints "Attack!", when the button_pressed signal is emitted.
  46. button_pressed.connect(func(): print("Attack!"))
  47. .. rst-class:: classref-reftable-group
  48. Constructors
  49. ------------
  50. .. table::
  51. :widths: auto
  52. +---------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Callable<class_Callable>` | :ref:`Callable<class_Callable_constructor_Callable>` **(** **)** |
  54. +---------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`Callable<class_Callable>` | :ref:`Callable<class_Callable_constructor_Callable>` **(** :ref:`Callable<class_Callable>` from **)** |
  56. +---------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Callable<class_Callable>` | :ref:`Callable<class_Callable_constructor_Callable>` **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method **)** |
  58. +---------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  59. .. rst-class:: classref-reftable-group
  60. Methods
  61. -------
  62. .. table::
  63. :widths: auto
  64. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Callable<class_Callable>` | :ref:`bind<class_Callable_method_bind>` **(** ... **)** |vararg| |const| |
  66. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Callable<class_Callable>` | :ref:`bindv<class_Callable_method_bindv>` **(** :ref:`Array<class_Array>` arguments **)** |
  68. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Variant<class_Variant>` | :ref:`call<class_Callable_method_call>` **(** ... **)** |vararg| |const| |
  70. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  71. | void | :ref:`call_deferred<class_Callable_method_call_deferred>` **(** ... **)** |vararg| |const| |
  72. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  73. | :ref:`Variant<class_Variant>` | :ref:`callv<class_Callable_method_callv>` **(** :ref:`Array<class_Array>` arguments **)** |const| |
  74. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  75. | :ref:`Array<class_Array>` | :ref:`get_bound_arguments<class_Callable_method_get_bound_arguments>` **(** **)** |const| |
  76. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  77. | :ref:`int<class_int>` | :ref:`get_bound_arguments_count<class_Callable_method_get_bound_arguments_count>` **(** **)** |const| |
  78. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  79. | :ref:`StringName<class_StringName>` | :ref:`get_method<class_Callable_method_get_method>` **(** **)** |const| |
  80. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Object<class_Object>` | :ref:`get_object<class_Callable_method_get_object>` **(** **)** |const| |
  82. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  83. | :ref:`int<class_int>` | :ref:`get_object_id<class_Callable_method_get_object_id>` **(** **)** |const| |
  84. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  85. | :ref:`int<class_int>` | :ref:`hash<class_Callable_method_hash>` **(** **)** |const| |
  86. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  87. | :ref:`bool<class_bool>` | :ref:`is_custom<class_Callable_method_is_custom>` **(** **)** |const| |
  88. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  89. | :ref:`bool<class_bool>` | :ref:`is_null<class_Callable_method_is_null>` **(** **)** |const| |
  90. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  91. | :ref:`bool<class_bool>` | :ref:`is_standard<class_Callable_method_is_standard>` **(** **)** |const| |
  92. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`is_valid<class_Callable_method_is_valid>` **(** **)** |const| |
  94. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  95. | void | :ref:`rpc<class_Callable_method_rpc>` **(** ... **)** |vararg| |const| |
  96. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  97. | void | :ref:`rpc_id<class_Callable_method_rpc_id>` **(** :ref:`int<class_int>` peer_id, ... **)** |vararg| |const| |
  98. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  99. | :ref:`Callable<class_Callable>` | :ref:`unbind<class_Callable_method_unbind>` **(** :ref:`int<class_int>` argcount **)** |const| |
  100. +-------------------------------------+-------------------------------------------------------------------------------------------------------------+
  101. .. rst-class:: classref-reftable-group
  102. Operators
  103. ---------
  104. .. table::
  105. :widths: auto
  106. +-------------------------+------------------------------------------------------------------------------------------------------------+
  107. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Callable_operator_neq_Callable>` **(** :ref:`Callable<class_Callable>` right **)** |
  108. +-------------------------+------------------------------------------------------------------------------------------------------------+
  109. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Callable_operator_eq_Callable>` **(** :ref:`Callable<class_Callable>` right **)** |
  110. +-------------------------+------------------------------------------------------------------------------------------------------------+
  111. .. rst-class:: classref-section-separator
  112. ----
  113. .. rst-class:: classref-descriptions-group
  114. Constructor Descriptions
  115. ------------------------
  116. .. _class_Callable_constructor_Callable:
  117. .. rst-class:: classref-constructor
  118. :ref:`Callable<class_Callable>` **Callable** **(** **)**
  119. Constructs an empty **Callable**, with no object nor method bound.
  120. .. rst-class:: classref-item-separator
  121. ----
  122. .. rst-class:: classref-constructor
  123. :ref:`Callable<class_Callable>` **Callable** **(** :ref:`Callable<class_Callable>` from **)**
  124. Constructs a **Callable** as a copy of the given **Callable**.
  125. .. rst-class:: classref-item-separator
  126. ----
  127. .. rst-class:: classref-constructor
  128. :ref:`Callable<class_Callable>` **Callable** **(** :ref:`Object<class_Object>` object, :ref:`StringName<class_StringName>` method **)**
  129. Creates a new **Callable** for the method named ``method`` in the specified ``object``.
  130. .. rst-class:: classref-section-separator
  131. ----
  132. .. rst-class:: classref-descriptions-group
  133. Method Descriptions
  134. -------------------
  135. .. _class_Callable_method_bind:
  136. .. rst-class:: classref-method
  137. :ref:`Callable<class_Callable>` **bind** **(** ... **)** |vararg| |const|
  138. Returns a copy of this **Callable** with one or more arguments bound. When called, the bound arguments are passed *after* the arguments supplied by :ref:`call<class_Callable_method_call>`.
  139. .. rst-class:: classref-item-separator
  140. ----
  141. .. _class_Callable_method_bindv:
  142. .. rst-class:: classref-method
  143. :ref:`Callable<class_Callable>` **bindv** **(** :ref:`Array<class_Array>` arguments **)**
  144. Returns a copy of this **Callable** with one or more arguments bound, reading them from an array. When called, the bound arguments are passed *after* the arguments supplied by :ref:`call<class_Callable_method_call>`.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_Callable_method_call:
  148. .. rst-class:: classref-method
  149. :ref:`Variant<class_Variant>` **call** **(** ... **)** |vararg| |const|
  150. Calls the method represented by this **Callable**. Arguments can be passed and should match the method's signature.
  151. .. rst-class:: classref-item-separator
  152. ----
  153. .. _class_Callable_method_call_deferred:
  154. .. rst-class:: classref-method
  155. void **call_deferred** **(** ... **)** |vararg| |const|
  156. Calls the method represented by this **Callable** in deferred mode, i.e. during the idle frame. Arguments can be passed and should match the method's signature.
  157. ::
  158. func _ready():
  159. grab_focus.call_deferred()
  160. .. rst-class:: classref-item-separator
  161. ----
  162. .. _class_Callable_method_callv:
  163. .. rst-class:: classref-method
  164. :ref:`Variant<class_Variant>` **callv** **(** :ref:`Array<class_Array>` arguments **)** |const|
  165. Calls the method represented by this **Callable**. Unlike :ref:`call<class_Callable_method_call>`, this method expects all arguments to be contained inside the ``arguments`` :ref:`Array<class_Array>`.
  166. .. rst-class:: classref-item-separator
  167. ----
  168. .. _class_Callable_method_get_bound_arguments:
  169. .. rst-class:: classref-method
  170. :ref:`Array<class_Array>` **get_bound_arguments** **(** **)** |const|
  171. Return the bound arguments (as long as :ref:`get_bound_arguments_count<class_Callable_method_get_bound_arguments_count>` is greater than zero), or empty (if :ref:`get_bound_arguments_count<class_Callable_method_get_bound_arguments_count>` is less than or equal to zero).
  172. .. rst-class:: classref-item-separator
  173. ----
  174. .. _class_Callable_method_get_bound_arguments_count:
  175. .. rst-class:: classref-method
  176. :ref:`int<class_int>` **get_bound_arguments_count** **(** **)** |const|
  177. Returns the total amount of arguments bound (or unbound) via successive :ref:`bind<class_Callable_method_bind>` or :ref:`unbind<class_Callable_method_unbind>` calls. If the amount of arguments unbound is greater than the ones bound, this function returns a value less than zero.
  178. .. rst-class:: classref-item-separator
  179. ----
  180. .. _class_Callable_method_get_method:
  181. .. rst-class:: classref-method
  182. :ref:`StringName<class_StringName>` **get_method** **(** **)** |const|
  183. Returns the name of the method represented by this **Callable**. If the callable is a lambda function, returns the function's name.
  184. .. rst-class:: classref-item-separator
  185. ----
  186. .. _class_Callable_method_get_object:
  187. .. rst-class:: classref-method
  188. :ref:`Object<class_Object>` **get_object** **(** **)** |const|
  189. Returns the object on which this **Callable** is called.
  190. .. rst-class:: classref-item-separator
  191. ----
  192. .. _class_Callable_method_get_object_id:
  193. .. rst-class:: classref-method
  194. :ref:`int<class_int>` **get_object_id** **(** **)** |const|
  195. Returns the ID of this **Callable**'s object (see :ref:`Object.get_instance_id<class_Object_method_get_instance_id>`).
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. _class_Callable_method_hash:
  199. .. rst-class:: classref-method
  200. :ref:`int<class_int>` **hash** **(** **)** |const|
  201. Returns the 32-bit hash value of this **Callable**'s object.
  202. \ **Note:** **Callable**\ s with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does *not* imply the callables are equal, because different callables can have identical hash values due to hash collisions. The engine uses a 32-bit hash algorithm for :ref:`hash<class_Callable_method_hash>`.
  203. .. rst-class:: classref-item-separator
  204. ----
  205. .. _class_Callable_method_is_custom:
  206. .. rst-class:: classref-method
  207. :ref:`bool<class_bool>` **is_custom** **(** **)** |const|
  208. Returns ``true`` if this **Callable** is a custom callable. Custom callables are created from :ref:`bind<class_Callable_method_bind>` or :ref:`unbind<class_Callable_method_unbind>`. In GDScript, lambda functions are also custom callables.
  209. .. rst-class:: classref-item-separator
  210. ----
  211. .. _class_Callable_method_is_null:
  212. .. rst-class:: classref-method
  213. :ref:`bool<class_bool>` **is_null** **(** **)** |const|
  214. Returns ``true`` if this **Callable** has no target to call the method on.
  215. .. rst-class:: classref-item-separator
  216. ----
  217. .. _class_Callable_method_is_standard:
  218. .. rst-class:: classref-method
  219. :ref:`bool<class_bool>` **is_standard** **(** **)** |const|
  220. Returns ``true`` if this **Callable** is a standard callable. This method is the opposite of :ref:`is_custom<class_Callable_method_is_custom>`. Returns ``false`` if this callable is a lambda function.
  221. .. rst-class:: classref-item-separator
  222. ----
  223. .. _class_Callable_method_is_valid:
  224. .. rst-class:: classref-method
  225. :ref:`bool<class_bool>` **is_valid** **(** **)** |const|
  226. Returns ``true`` if the callable's object exists and has a valid method name assigned, or is a custom callable.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. _class_Callable_method_rpc:
  230. .. rst-class:: classref-method
  231. void **rpc** **(** ... **)** |vararg| |const|
  232. Perform an RPC (Remote Procedure Call). This is used for multiplayer and is normally not available, unless the function being called has been marked as *RPC*. Calling this method on unsupported functions will result in an error. See :ref:`Node.rpc<class_Node_method_rpc>`.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_Callable_method_rpc_id:
  236. .. rst-class:: classref-method
  237. void **rpc_id** **(** :ref:`int<class_int>` peer_id, ... **)** |vararg| |const|
  238. Perform an RPC (Remote Procedure Call) on a specific peer ID (see multiplayer documentation for reference). This is used for multiplayer and is normally not available unless the function being called has been marked as *RPC*. Calling this method on unsupported functions will result in an error. See :ref:`Node.rpc_id<class_Node_method_rpc_id>`.
  239. .. rst-class:: classref-item-separator
  240. ----
  241. .. _class_Callable_method_unbind:
  242. .. rst-class:: classref-method
  243. :ref:`Callable<class_Callable>` **unbind** **(** :ref:`int<class_int>` argcount **)** |const|
  244. Returns a copy of this **Callable** with the arguments unbound, as defined by ``argcount``. Calling the returned **Callable** will call the method without the extra arguments that are supplied in the **Callable** on which you are calling this method.
  245. .. rst-class:: classref-section-separator
  246. ----
  247. .. rst-class:: classref-descriptions-group
  248. Operator Descriptions
  249. ---------------------
  250. .. _class_Callable_operator_neq_Callable:
  251. .. rst-class:: classref-operator
  252. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Callable<class_Callable>` right **)**
  253. Returns ``true`` if both **Callable**\ s invoke different targets.
  254. .. rst-class:: classref-item-separator
  255. ----
  256. .. _class_Callable_operator_eq_Callable:
  257. .. rst-class:: classref-operator
  258. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Callable<class_Callable>` right **)**
  259. Returns ``true`` if both **Callable**\ s invoke the same custom target.
  260. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  261. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  262. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  263. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  264. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  265. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`