class_object.rst 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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/4.0/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.0/doc/classes/Object.xml.
  6. .. _class_Object:
  7. Object
  8. ======
  9. **Inherited By:** :ref:`AudioServer<class_AudioServer>`, :ref:`CameraServer<class_CameraServer>`, :ref:`ClassDB<class_ClassDB>`, :ref:`DisplayServer<class_DisplayServer>`, :ref:`EditorFileSystemDirectory<class_EditorFileSystemDirectory>`, :ref:`EditorPaths<class_EditorPaths>`, :ref:`EditorSelection<class_EditorSelection>`, :ref:`EditorUndoRedoManager<class_EditorUndoRedoManager>`, :ref:`EditorVCSInterface<class_EditorVCSInterface>`, :ref:`Engine<class_Engine>`, :ref:`EngineDebugger<class_EngineDebugger>`, :ref:`GDExtensionManager<class_GDExtensionManager>`, :ref:`Geometry2D<class_Geometry2D>`, :ref:`Geometry3D<class_Geometry3D>`, :ref:`GodotSharp<class_GodotSharp>`, :ref:`Input<class_Input>`, :ref:`InputMap<class_InputMap>`, :ref:`IP<class_IP>`, :ref:`JavaClassWrapper<class_JavaClassWrapper>`, :ref:`JavaScriptBridge<class_JavaScriptBridge>`, :ref:`JNISingleton<class_JNISingleton>`, :ref:`JSONRPC<class_JSONRPC>`, :ref:`MainLoop<class_MainLoop>`, :ref:`Marshalls<class_Marshalls>`, :ref:`MovieWriter<class_MovieWriter>`, :ref:`NavigationMeshGenerator<class_NavigationMeshGenerator>`, :ref:`NavigationServer2D<class_NavigationServer2D>`, :ref:`NavigationServer3D<class_NavigationServer3D>`, :ref:`Node<class_Node>`, :ref:`OS<class_OS>`, :ref:`Performance<class_Performance>`, :ref:`PhysicsDirectBodyState2D<class_PhysicsDirectBodyState2D>`, :ref:`PhysicsDirectBodyState3D<class_PhysicsDirectBodyState3D>`, :ref:`PhysicsDirectSpaceState2D<class_PhysicsDirectSpaceState2D>`, :ref:`PhysicsDirectSpaceState3D<class_PhysicsDirectSpaceState3D>`, :ref:`PhysicsServer2D<class_PhysicsServer2D>`, :ref:`PhysicsServer2DManager<class_PhysicsServer2DManager>`, :ref:`PhysicsServer3D<class_PhysicsServer3D>`, :ref:`PhysicsServer3DManager<class_PhysicsServer3DManager>`, :ref:`PhysicsServer3DRenderingServerHandler<class_PhysicsServer3DRenderingServerHandler>`, :ref:`ProjectSettings<class_ProjectSettings>`, :ref:`RefCounted<class_RefCounted>`, :ref:`RenderingDevice<class_RenderingDevice>`, :ref:`RenderingServer<class_RenderingServer>`, :ref:`ResourceLoader<class_ResourceLoader>`, :ref:`ResourceSaver<class_ResourceSaver>`, :ref:`ResourceUID<class_ResourceUID>`, :ref:`ScriptLanguage<class_ScriptLanguage>`, :ref:`TextServerManager<class_TextServerManager>`, :ref:`ThemeDB<class_ThemeDB>`, :ref:`TileData<class_TileData>`, :ref:`Time<class_Time>`, :ref:`TranslationServer<class_TranslationServer>`, :ref:`TreeItem<class_TreeItem>`, :ref:`UndoRedo<class_UndoRedo>`, :ref:`WorkerThreadPool<class_WorkerThreadPool>`, :ref:`XRServer<class_XRServer>`
  10. Base class for all other classes in the engine.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. An advanced :ref:`Variant<class_Variant>` type. All classes in the engine inherit from Object. Each class may define new properties, methods or signals, which are available to all inheriting classes. For example, a :ref:`Sprite2D<class_Sprite2D>` instance is able to call :ref:`Node.add_child<class_Node_method_add_child>` because it inherits from :ref:`Node<class_Node>`.
  15. You can create new instances, using ``Object.new()`` in GDScript, or ``new Object`` in C#.
  16. To delete an Object instance, call :ref:`free<class_Object_method_free>`. This is necessary for most classes inheriting Object, because they do not manage memory on their own, and will otherwise cause memory leaks when no longer in use. There are a few classes that perform memory management. For example, :ref:`RefCounted<class_RefCounted>` (and by extension :ref:`Resource<class_Resource>`) deletes itself when no longer referenced, and :ref:`Node<class_Node>` deletes its children when freed.
  17. Objects can have a :ref:`Script<class_Script>` attached to them. Once the :ref:`Script<class_Script>` is instantiated, it effectively acts as an extension to the base class, allowing it to define and inherit new properties, methods and signals.
  18. Inside a :ref:`Script<class_Script>`, :ref:`_get_property_list<class_Object_method__get_property_list>` may be overridden to customize properties in several ways. This allows them to be available to the editor, display as lists of options, sub-divide into groups, save on disk, etc. Scripting languages offer easier ways to customize properties, such as with the :ref:`@GDScript.@export<class_@GDScript_annotation_@export>` annotation.
  19. Godot is very dynamic. An object's script, and therefore its properties, methods and signals, can be changed at run-time. Because of this, there can be occasions where, for example, a property required by a method may not exist. To prevent run-time errors, see methods such as :ref:`set<class_Object_method_set>`, :ref:`get<class_Object_method_get>`, :ref:`call<class_Object_method_call>`, :ref:`has_method<class_Object_method_has_method>`, :ref:`has_signal<class_Object_method_has_signal>`, etc. Note that these methods are **much** slower than direct references.
  20. In GDScript, you can also check if a given property, method, or signal name exists in an object with the ``in`` operator:
  21. ::
  22. var node = Node.new()
  23. print("name" in node) # Prints true
  24. print("get_parent" in node) # Prints true
  25. print("tree_entered" in node) # Prints true
  26. print("unknown" in node) # Prints false
  27. Notifications are :ref:`int<class_int>` constants commonly sent and received by objects. For example, on every rendered frame, the :ref:`SceneTree<class_SceneTree>` notifies nodes inside the tree with a :ref:`Node.NOTIFICATION_PROCESS<class_Node_constant_NOTIFICATION_PROCESS>`. The nodes receive it and may call :ref:`Node._process<class_Node_method__process>` to update. To make use of notifications, see :ref:`notification<class_Object_method_notification>` and :ref:`_notification<class_Object_method__notification>`.
  28. Lastly, every object can also contain metadata (data about data). :ref:`set_meta<class_Object_method_set_meta>` can be useful to store information that the object itself does not depend on. To keep your code clean, making excessive use of metadata is discouraged.
  29. \ **Note:** Unlike references to a :ref:`RefCounted<class_RefCounted>`, references to an object stored in a variable can become invalid without being set to ``null``. To check if an object has been deleted, do *not* compare it against ``null``. Instead, use :ref:`@GlobalScope.is_instance_valid<class_@GlobalScope_method_is_instance_valid>`. It's also recommended to inherit from :ref:`RefCounted<class_RefCounted>` for classes storing data instead of **Object**.
  30. \ **Note:** The ``script`` is not exposed like most properties. To set or get an object's :ref:`Script<class_Script>` in code, use :ref:`set_script<class_Object_method_set_script>` and :ref:`get_script<class_Object_method_get_script>`, respectively.
  31. .. rst-class:: classref-introduction-group
  32. Tutorials
  33. ---------
  34. - :doc:`Object class introduction <../contributing/development/core_and_modules/object_class>`
  35. - :doc:`When and how to avoid using nodes for everything <../tutorials/best_practices/node_alternatives>`
  36. - :doc:`Object notifications <../tutorials/best_practices/godot_notifications>`
  37. .. rst-class:: classref-reftable-group
  38. Methods
  39. -------
  40. .. table::
  41. :widths: auto
  42. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`Variant<class_Variant>` | :ref:`_get<class_Object_method__get>` **(** :ref:`StringName<class_StringName>` property **)** |virtual| |
  44. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`_get_property_list<class_Object_method__get_property_list>` **(** **)** |virtual| |
  46. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | void | :ref:`_init<class_Object_method__init>` **(** **)** |virtual| |
  48. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | void | :ref:`_notification<class_Object_method__notification>` **(** :ref:`int<class_int>` what **)** |virtual| |
  50. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`bool<class_bool>` | :ref:`_property_can_revert<class_Object_method__property_can_revert>` **(** :ref:`StringName<class_StringName>` property **)** |virtual| |
  52. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Variant<class_Variant>` | :ref:`_property_get_revert<class_Object_method__property_get_revert>` **(** :ref:`StringName<class_StringName>` property **)** |virtual| |
  54. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`bool<class_bool>` | :ref:`_set<class_Object_method__set>` **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |virtual| |
  56. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`String<class_String>` | :ref:`_to_string<class_Object_method__to_string>` **(** **)** |virtual| |
  58. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | void | :ref:`add_user_signal<class_Object_method_add_user_signal>` **(** :ref:`String<class_String>` signal, :ref:`Array<class_Array>` arguments=[] **)** |
  60. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Variant<class_Variant>` | :ref:`call<class_Object_method_call>` **(** :ref:`StringName<class_StringName>` method, ... **)** |vararg| |
  62. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`Variant<class_Variant>` | :ref:`call_deferred<class_Object_method_call_deferred>` **(** :ref:`StringName<class_StringName>` method, ... **)** |vararg| |
  64. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Variant<class_Variant>` | :ref:`callv<class_Object_method_callv>` **(** :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` arg_array **)** |
  66. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`can_translate_messages<class_Object_method_can_translate_messages>` **(** **)** |const| |
  68. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`connect<class_Object_method_connect>` **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable, :ref:`int<class_int>` flags=0 **)** |
  70. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | void | :ref:`disconnect<class_Object_method_disconnect>` **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable **)** |
  72. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`emit_signal<class_Object_method_emit_signal>` **(** :ref:`StringName<class_StringName>` signal, ... **)** |vararg| |
  74. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | void | :ref:`free<class_Object_method_free>` **(** **)** |
  76. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`Variant<class_Variant>` | :ref:`get<class_Object_method_get>` **(** :ref:`StringName<class_StringName>` property **)** |const| |
  78. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`String<class_String>` | :ref:`get_class<class_Object_method_get_class>` **(** **)** |const| |
  80. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`get_incoming_connections<class_Object_method_get_incoming_connections>` **(** **)** |const| |
  82. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`Variant<class_Variant>` | :ref:`get_indexed<class_Object_method_get_indexed>` **(** :ref:`NodePath<class_NodePath>` property_path **)** |const| |
  84. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | :ref:`int<class_int>` | :ref:`get_instance_id<class_Object_method_get_instance_id>` **(** **)** |const| |
  86. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | :ref:`Variant<class_Variant>` | :ref:`get_meta<class_Object_method_get_meta>` **(** :ref:`StringName<class_StringName>` name, :ref:`Variant<class_Variant>` default=null **)** |const| |
  88. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`get_meta_list<class_Object_method_get_meta_list>` **(** **)** |const| |
  90. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`get_method_list<class_Object_method_get_method_list>` **(** **)** |const| |
  92. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`get_property_list<class_Object_method_get_property_list>` **(** **)** |const| |
  94. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`Variant<class_Variant>` | :ref:`get_script<class_Object_method_get_script>` **(** **)** |const| |
  96. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`get_signal_connection_list<class_Object_method_get_signal_connection_list>` **(** :ref:`StringName<class_StringName>` signal **)** |const| |
  98. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`Dictionary[]<class_Dictionary>` | :ref:`get_signal_list<class_Object_method_get_signal_list>` **(** **)** |const| |
  100. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`bool<class_bool>` | :ref:`has_meta<class_Object_method_has_meta>` **(** :ref:`StringName<class_StringName>` name **)** |const| |
  102. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | :ref:`bool<class_bool>` | :ref:`has_method<class_Object_method_has_method>` **(** :ref:`StringName<class_StringName>` method **)** |const| |
  104. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`bool<class_bool>` | :ref:`has_signal<class_Object_method_has_signal>` **(** :ref:`StringName<class_StringName>` signal **)** |const| |
  106. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  107. | :ref:`bool<class_bool>` | :ref:`has_user_signal<class_Object_method_has_user_signal>` **(** :ref:`StringName<class_StringName>` signal **)** |const| |
  108. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  109. | :ref:`bool<class_bool>` | :ref:`is_blocking_signals<class_Object_method_is_blocking_signals>` **(** **)** |const| |
  110. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  111. | :ref:`bool<class_bool>` | :ref:`is_class<class_Object_method_is_class>` **(** :ref:`String<class_String>` class **)** |const| |
  112. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  113. | :ref:`bool<class_bool>` | :ref:`is_connected<class_Object_method_is_connected>` **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable **)** |const| |
  114. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  115. | :ref:`bool<class_bool>` | :ref:`is_queued_for_deletion<class_Object_method_is_queued_for_deletion>` **(** **)** |const| |
  116. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  117. | void | :ref:`notification<class_Object_method_notification>` **(** :ref:`int<class_int>` what, :ref:`bool<class_bool>` reversed=false **)** |
  118. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | void | :ref:`notify_property_list_changed<class_Object_method_notify_property_list_changed>` **(** **)** |
  120. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. | :ref:`bool<class_bool>` | :ref:`property_can_revert<class_Object_method_property_can_revert>` **(** :ref:`StringName<class_StringName>` property **)** |const| |
  122. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  123. | :ref:`Variant<class_Variant>` | :ref:`property_get_revert<class_Object_method_property_get_revert>` **(** :ref:`StringName<class_StringName>` property **)** |const| |
  124. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  125. | void | :ref:`remove_meta<class_Object_method_remove_meta>` **(** :ref:`StringName<class_StringName>` name **)** |
  126. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | void | :ref:`set<class_Object_method_set>` **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |
  128. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | void | :ref:`set_block_signals<class_Object_method_set_block_signals>` **(** :ref:`bool<class_bool>` enable **)** |
  130. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. | void | :ref:`set_deferred<class_Object_method_set_deferred>` **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |
  132. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  133. | void | :ref:`set_indexed<class_Object_method_set_indexed>` **(** :ref:`NodePath<class_NodePath>` property_path, :ref:`Variant<class_Variant>` value **)** |
  134. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  135. | void | :ref:`set_message_translation<class_Object_method_set_message_translation>` **(** :ref:`bool<class_bool>` enable **)** |
  136. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  137. | void | :ref:`set_meta<class_Object_method_set_meta>` **(** :ref:`StringName<class_StringName>` name, :ref:`Variant<class_Variant>` value **)** |
  138. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  139. | void | :ref:`set_script<class_Object_method_set_script>` **(** :ref:`Variant<class_Variant>` script **)** |
  140. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  141. | :ref:`String<class_String>` | :ref:`to_string<class_Object_method_to_string>` **(** **)** |
  142. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  143. | :ref:`String<class_String>` | :ref:`tr<class_Object_method_tr>` **(** :ref:`StringName<class_StringName>` message, :ref:`StringName<class_StringName>` context="" **)** |const| |
  144. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | :ref:`String<class_String>` | :ref:`tr_n<class_Object_method_tr_n>` **(** :ref:`StringName<class_StringName>` message, :ref:`StringName<class_StringName>` plural_message, :ref:`int<class_int>` n, :ref:`StringName<class_StringName>` context="" **)** |const| |
  146. +---------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  147. .. rst-class:: classref-section-separator
  148. ----
  149. .. rst-class:: classref-descriptions-group
  150. Signals
  151. -------
  152. .. _class_Object_signal_property_list_changed:
  153. .. rst-class:: classref-signal
  154. **property_list_changed** **(** **)**
  155. Emitted when :ref:`notify_property_list_changed<class_Object_method_notify_property_list_changed>` is called.
  156. .. rst-class:: classref-item-separator
  157. ----
  158. .. _class_Object_signal_script_changed:
  159. .. rst-class:: classref-signal
  160. **script_changed** **(** **)**
  161. Emitted when the object's script is changed.
  162. \ **Note:** When this signal is emitted, the new script is not initialized yet. If you need to access the new script, defer connections to this signal with :ref:`CONNECT_DEFERRED<class_Object_constant_CONNECT_DEFERRED>`.
  163. .. rst-class:: classref-section-separator
  164. ----
  165. .. rst-class:: classref-descriptions-group
  166. Enumerations
  167. ------------
  168. .. _enum_Object_ConnectFlags:
  169. .. rst-class:: classref-enumeration
  170. enum **ConnectFlags**:
  171. .. _class_Object_constant_CONNECT_DEFERRED:
  172. .. rst-class:: classref-enumeration-constant
  173. :ref:`ConnectFlags<enum_Object_ConnectFlags>` **CONNECT_DEFERRED** = ``1``
  174. Deferred connections trigger their :ref:`Callable<class_Callable>`\ s on idle time, rather than instantly.
  175. .. _class_Object_constant_CONNECT_PERSIST:
  176. .. rst-class:: classref-enumeration-constant
  177. :ref:`ConnectFlags<enum_Object_ConnectFlags>` **CONNECT_PERSIST** = ``2``
  178. Persisting connections are stored when the object is serialized (such as when using :ref:`PackedScene.pack<class_PackedScene_method_pack>`). In the editor, connections created through the Node dock are always persisting.
  179. .. _class_Object_constant_CONNECT_ONE_SHOT:
  180. .. rst-class:: classref-enumeration-constant
  181. :ref:`ConnectFlags<enum_Object_ConnectFlags>` **CONNECT_ONE_SHOT** = ``4``
  182. One-shot connections disconnect themselves after emission.
  183. .. _class_Object_constant_CONNECT_REFERENCE_COUNTED:
  184. .. rst-class:: classref-enumeration-constant
  185. :ref:`ConnectFlags<enum_Object_ConnectFlags>` **CONNECT_REFERENCE_COUNTED** = ``8``
  186. Reference-counted connections can be assigned to the same :ref:`Callable<class_Callable>` multiple times. Each disconnection decreases the internal counter. The signal fully disconnects only when the counter reaches 0.
  187. .. rst-class:: classref-section-separator
  188. ----
  189. .. rst-class:: classref-descriptions-group
  190. Constants
  191. ---------
  192. .. _class_Object_constant_NOTIFICATION_POSTINITIALIZE:
  193. .. rst-class:: classref-constant
  194. **NOTIFICATION_POSTINITIALIZE** = ``0``
  195. Notification received when the object is initialized, before its script is attached. Used internally.
  196. .. _class_Object_constant_NOTIFICATION_PREDELETE:
  197. .. rst-class:: classref-constant
  198. **NOTIFICATION_PREDELETE** = ``1``
  199. Notification received when the object is about to be deleted. Can act as the deconstructor of some programming languages.
  200. .. rst-class:: classref-section-separator
  201. ----
  202. .. rst-class:: classref-descriptions-group
  203. Method Descriptions
  204. -------------------
  205. .. _class_Object_method__get:
  206. .. rst-class:: classref-method
  207. :ref:`Variant<class_Variant>` **_get** **(** :ref:`StringName<class_StringName>` property **)** |virtual|
  208. Override this method to customize the behavior of :ref:`get<class_Object_method_get>`. Should return the given ``property``'s value, or ``null`` if the ``property`` should be handled normally.
  209. Combined with :ref:`_set<class_Object_method__set>` and :ref:`_get_property_list<class_Object_method__get_property_list>`, this method allows defining custom properties, which is particularly useful for editor plugins. Note that a property must be present in :ref:`get_property_list<class_Object_method_get_property_list>`, otherwise this method will not be called.
  210. .. tabs::
  211. .. code-tab:: gdscript
  212. func _get(property):
  213. if (property == "fake_property"):
  214. print("Getting my property!")
  215. return 4
  216. func _get_property_list():
  217. return [
  218. { "name": "fake_property", "type": TYPE_INT }
  219. ]
  220. .. code-tab:: csharp
  221. public override Variant _Get(StringName property)
  222. {
  223. if (property == "FakeProperty")
  224. {
  225. GD.Print("Getting my property!");
  226. return 4;
  227. }
  228. return default;
  229. }
  230. public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
  231. {
  232. return new Godot.Collections.Array<Godot.Collections.Dictionary>()
  233. {
  234. new Godot.Collections.Dictionary()
  235. {
  236. { "name", "FakeProperty" },
  237. { "type", (int)Variant.Type.Int }
  238. }
  239. };
  240. }
  241. .. rst-class:: classref-item-separator
  242. ----
  243. .. _class_Object_method__get_property_list:
  244. .. rst-class:: classref-method
  245. :ref:`Dictionary[]<class_Dictionary>` **_get_property_list** **(** **)** |virtual|
  246. Override this method to customize how script properties should be handled by the engine.
  247. Should return a property list, as an :ref:`Array<class_Array>` of dictionaries. The result is added to the array of :ref:`get_property_list<class_Object_method_get_property_list>`, and should be formatted in the same way. Each :ref:`Dictionary<class_Dictionary>` must at least contain the ``name`` and ``type`` entries.
  248. The example below displays ``hammer_type`` in the Inspector dock, only if ``holding_hammer`` is ``true``:
  249. .. tabs::
  250. .. code-tab:: gdscript
  251. @tool
  252. extends Node2D
  253. @export var holding_hammer = false:
  254. set(value):
  255. holding_hammer = value
  256. notify_property_list_changed()
  257. var hammer_type = 0
  258. func _get_property_list():
  259. # By default, `hammer_type` is not visible in the editor.
  260. var property_usage = PROPERTY_USAGE_NO_EDITOR
  261. if holding_hammer:
  262. property_usage = PROPERTY_USAGE_DEFAULT
  263. var properties = []
  264. properties.append({
  265. "name": "hammer_type",
  266. "type": TYPE_INT,
  267. "usage": property_usage, # See above assignment.
  268. "hint": PROPERTY_HINT_ENUM,
  269. "hint_string": "Wooden,Iron,Golden,Enchanted"
  270. })
  271. return properties
  272. .. code-tab:: csharp
  273. [Tool]
  274. public partial class MyNode2D : Node2D
  275. {
  276. private bool _holdingHammer;
  277. [Export]
  278. public bool HoldingHammer
  279. {
  280. get => _holdingHammer;
  281. set
  282. {
  283. _holdingHammer = value;
  284. NotifyPropertyListChanged();
  285. }
  286. }
  287. public int HammerType { get; set; }
  288. public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
  289. {
  290. // By default, `HammerType` is not visible in the editor.
  291. var propertyUsage = PropertyUsageFlags.NoEditor;
  292. if (HoldingHammer)
  293. {
  294. propertyUsage = PropertyUsageFlags.Default;
  295. }
  296. var properties = new Godot.Collections.Array<Godot.Collections.Dictionary>();
  297. properties.Add(new Godot.Collections.Dictionary()
  298. {
  299. { "name", "HammerType" },
  300. { "type", (int)Variant.Type.Int },
  301. { "usage", (int)propertyUsage }, // See above assignment.
  302. { "hint", (int)PropertyHint.Enum },
  303. { "hint_string", "Wooden,Iron,Golden,Enchanted" }
  304. });
  305. return properties;
  306. }
  307. }
  308. \ **Note:** This method is intended for advanced purposes. For most common use cases, the scripting languages offer easier ways to handle properties. See :ref:`@GDScript.@export<class_@GDScript_annotation_@export>`, :ref:`@GDScript.@export_enum<class_@GDScript_annotation_@export_enum>`, :ref:`@GDScript.@export_group<class_@GDScript_annotation_@export_group>`, etc.
  309. \ **Note:** If the object's script is not :ref:`@GDScript.@tool<class_@GDScript_annotation_@tool>`, this method will not be called in the editor.
  310. .. rst-class:: classref-item-separator
  311. ----
  312. .. _class_Object_method__init:
  313. .. rst-class:: classref-method
  314. void **_init** **(** **)** |virtual|
  315. Called when the object's script is instantiated, oftentimes after the object is initialized in memory (through ``Object.new()`` in GDScript, or ``new Object`` in C#). It can be also defined to take in parameters. This method is similar to a constructor in most programming languages.
  316. \ **Note:** If :ref:`_init<class_Object_method__init>` is defined with *required* parameters, the Object with script may only be created directly. If any other means (such as :ref:`PackedScene.instantiate<class_PackedScene_method_instantiate>` or :ref:`Node.duplicate<class_Node_method_duplicate>`) are used, the script's initialization will fail.
  317. .. rst-class:: classref-item-separator
  318. ----
  319. .. _class_Object_method__notification:
  320. .. rst-class:: classref-method
  321. void **_notification** **(** :ref:`int<class_int>` what **)** |virtual|
  322. Called when the object receives a notification, which can be identified in ``what`` by comparing it with a constant. See also :ref:`notification<class_Object_method_notification>`.
  323. .. tabs::
  324. .. code-tab:: gdscript
  325. func _notification(what):
  326. if what == NOTIFICATION_PREDELETE:
  327. print("Goodbye!")
  328. .. code-tab:: csharp
  329. public override void _Notification(int what)
  330. {
  331. if (what == NotificationPredelete)
  332. {
  333. GD.Print("Goodbye!");
  334. }
  335. }
  336. \ **Note:** The base **Object** defines a few notifications (:ref:`NOTIFICATION_POSTINITIALIZE<class_Object_constant_NOTIFICATION_POSTINITIALIZE>` and :ref:`NOTIFICATION_PREDELETE<class_Object_constant_NOTIFICATION_PREDELETE>`). Inheriting classes such as :ref:`Node<class_Node>` define a lot more notifications, which are also received by this method.
  337. .. rst-class:: classref-item-separator
  338. ----
  339. .. _class_Object_method__property_can_revert:
  340. .. rst-class:: classref-method
  341. :ref:`bool<class_bool>` **_property_can_revert** **(** :ref:`StringName<class_StringName>` property **)** |virtual|
  342. Override this method to customize the given ``property``'s revert behavior. Should return ``true`` if the ``property`` can be reverted in the Inspector dock. Use :ref:`_property_get_revert<class_Object_method__property_get_revert>` to specify the ``property``'s default value.
  343. \ **Note:** This method must return consistently, regardless of the current value of the ``property``.
  344. .. rst-class:: classref-item-separator
  345. ----
  346. .. _class_Object_method__property_get_revert:
  347. .. rst-class:: classref-method
  348. :ref:`Variant<class_Variant>` **_property_get_revert** **(** :ref:`StringName<class_StringName>` property **)** |virtual|
  349. Override this method to customize the given ``property``'s revert behavior. Should return the default value for the ``property``. If the default value differs from the ``property``'s current value, a revert icon is displayed in the Inspector dock.
  350. \ **Note:** :ref:`_property_can_revert<class_Object_method__property_can_revert>` must also be overridden for this method to be called.
  351. .. rst-class:: classref-item-separator
  352. ----
  353. .. _class_Object_method__set:
  354. .. rst-class:: classref-method
  355. :ref:`bool<class_bool>` **_set** **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)** |virtual|
  356. Override this method to customize the behavior of :ref:`set<class_Object_method_set>`. Should set the ``property`` to ``value`` and return ``true``, or ``false`` if the ``property`` should be handled normally. The *exact* way to set the ``property`` is up to this method's implementation.
  357. Combined with :ref:`_get<class_Object_method__get>` and :ref:`_get_property_list<class_Object_method__get_property_list>`, this method allows defining custom properties, which is particularly useful for editor plugins. Note that a property *must* be present in :ref:`get_property_list<class_Object_method_get_property_list>`, otherwise this method will not be called.
  358. .. tabs::
  359. .. code-tab:: gdscript
  360. func _set(property, value):
  361. if (property == "fake_property"):
  362. print("Setting my property to ", value)
  363. func _get_property_list():
  364. return [
  365. { "name": "fake_property", "type": TYPE_INT }
  366. ]
  367. .. code-tab:: csharp
  368. public override void _Set(StringName property, Variant value)
  369. {
  370. if (property == "FakeProperty")
  371. {
  372. GD.Print($"Setting my property to {value}");
  373. return true;
  374. }
  375. return false;
  376. }
  377. public override Godot.Collections.Array<Godot.Collections.Dictionary> _GetPropertyList()
  378. {
  379. return new Godot.Collections.Array<Godot.Collections.Dictionary>()
  380. {
  381. new Godot.Collections.Dictionary()
  382. {
  383. { "name", "FakeProperty" },
  384. { "type", (int)Variant.Type.Int }
  385. }
  386. };
  387. }
  388. .. rst-class:: classref-item-separator
  389. ----
  390. .. _class_Object_method__to_string:
  391. .. rst-class:: classref-method
  392. :ref:`String<class_String>` **_to_string** **(** **)** |virtual|
  393. Override this method to customize the return value of :ref:`to_string<class_Object_method_to_string>`, and therefore the object's representation as a :ref:`String<class_String>`.
  394. ::
  395. func _to_string():
  396. return "Welcome to Godot 4!"
  397. func _init():
  398. print(self) # Prints Welcome to Godot 4!"
  399. var a = str(self) # a is "Welcome to Godot 4!"
  400. .. rst-class:: classref-item-separator
  401. ----
  402. .. _class_Object_method_add_user_signal:
  403. .. rst-class:: classref-method
  404. void **add_user_signal** **(** :ref:`String<class_String>` signal, :ref:`Array<class_Array>` arguments=[] **)**
  405. Adds a user-defined ``signal``. Optional arguments for the signal can be added as an :ref:`Array<class_Array>` of dictionaries, each defining a ``name`` :ref:`String<class_String>` and a ``type`` :ref:`int<class_int>` (see :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>`). See also :ref:`has_user_signal<class_Object_method_has_user_signal>`.
  406. .. tabs::
  407. .. code-tab:: gdscript
  408. add_user_signal("hurt", [
  409. { "name": "damage", "type": TYPE_INT },
  410. { "name": "source", "type": TYPE_OBJECT }
  411. ])
  412. .. code-tab:: csharp
  413. AddUserSignal("Hurt", new Godot.Collections.Array()
  414. {
  415. new Godot.Collections.Dictionary()
  416. {
  417. { "name", "damage" },
  418. { "type", (int)Variant.Type.Int }
  419. },
  420. new Godot.Collections.Dictionary()
  421. {
  422. { "name", "source" },
  423. { "type", (int)Variant.Type.Object }
  424. }
  425. });
  426. .. rst-class:: classref-item-separator
  427. ----
  428. .. _class_Object_method_call:
  429. .. rst-class:: classref-method
  430. :ref:`Variant<class_Variant>` **call** **(** :ref:`StringName<class_StringName>` method, ... **)** |vararg|
  431. Calls the ``method`` on the object and returns the result. This method supports a variable number of arguments, so parameters can be passed as a comma separated list.
  432. .. tabs::
  433. .. code-tab:: gdscript
  434. var node = Node3D.new()
  435. node.call("rotate", Vector3(1.0, 0.0, 0.0), 1.571)
  436. .. code-tab:: csharp
  437. var node = new Node3D();
  438. node.Call(Node3D.MethodName.Rotate, new Vector3(1f, 0f, 0f), 1.571f);
  439. \ **Note:** In C#, ``method`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``MethodName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  440. .. rst-class:: classref-item-separator
  441. ----
  442. .. _class_Object_method_call_deferred:
  443. .. rst-class:: classref-method
  444. :ref:`Variant<class_Variant>` **call_deferred** **(** :ref:`StringName<class_StringName>` method, ... **)** |vararg|
  445. Calls the ``method`` on the object during idle time. This method supports a variable number of arguments, so parameters can be passed as a comma separated list.
  446. .. tabs::
  447. .. code-tab:: gdscript
  448. var node = Node3D.new()
  449. node.call_deferred("rotate", Vector3(1.0, 0.0, 0.0), 1.571)
  450. .. code-tab:: csharp
  451. var node = new Node3D();
  452. node.CallDeferred(Node3D.MethodName.Rotate, new Vector3(1f, 0f, 0f), 1.571f);
  453. \ **Note:** In C#, ``method`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``MethodName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  454. .. rst-class:: classref-item-separator
  455. ----
  456. .. _class_Object_method_callv:
  457. .. rst-class:: classref-method
  458. :ref:`Variant<class_Variant>` **callv** **(** :ref:`StringName<class_StringName>` method, :ref:`Array<class_Array>` arg_array **)**
  459. Calls the ``method`` on the object and returns the result. Unlike :ref:`call<class_Object_method_call>`, this method expects all parameters to be contained inside ``arg_array``.
  460. .. tabs::
  461. .. code-tab:: gdscript
  462. var node = Node3D.new()
  463. node.callv("rotate", [Vector3(1.0, 0.0, 0.0), 1.571])
  464. .. code-tab:: csharp
  465. var node = new Node3D();
  466. node.Callv(Node3D.MethodName.Rotate, new Godot.Collections.Array { new Vector3(1f, 0f, 0f), 1.571f });
  467. \ **Note:** In C#, ``method`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``MethodName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  468. .. rst-class:: classref-item-separator
  469. ----
  470. .. _class_Object_method_can_translate_messages:
  471. .. rst-class:: classref-method
  472. :ref:`bool<class_bool>` **can_translate_messages** **(** **)** |const|
  473. Returns ``true`` if the object is allowed to translate messages with :ref:`tr<class_Object_method_tr>` and :ref:`tr_n<class_Object_method_tr_n>`. See also :ref:`set_message_translation<class_Object_method_set_message_translation>`.
  474. .. rst-class:: classref-item-separator
  475. ----
  476. .. _class_Object_method_connect:
  477. .. rst-class:: classref-method
  478. :ref:`Error<enum_@GlobalScope_Error>` **connect** **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable, :ref:`int<class_int>` flags=0 **)**
  479. Connects a ``signal`` by name to a ``callable``. Optional ``flags`` can be also added to configure the connection's behavior (see :ref:`ConnectFlags<enum_Object_ConnectFlags>` constants).
  480. A signal can only be connected once to the same :ref:`Callable<class_Callable>`. If the signal is already connected, this method returns :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` and pushes an error message, unless the signal is connected with :ref:`CONNECT_REFERENCE_COUNTED<class_Object_constant_CONNECT_REFERENCE_COUNTED>`. To prevent this, use :ref:`is_connected<class_Object_method_is_connected>` first to check for existing connections.
  481. If the ``callable``'s object is freed, the connection will be lost.
  482. \ **Examples with recommended syntax:**\
  483. Connecting signals is one of the most common operations in Godot and the API gives many options to do so, which are described further down. The code block below shows the recommended approach.
  484. .. tabs::
  485. .. code-tab:: gdscript
  486. func _ready():
  487. var button = Button.new()
  488. # `button_down` here is a Signal variant type, and we thus call the Signal.connect() method, not Object.connect().
  489. # See discussion below for a more in-depth overview of the API.
  490. button.button_down.connect(_on_button_down)
  491. # This assumes that a `Player` class exists, which defines a `hit` signal.
  492. var player = Player.new()
  493. # We use Signal.connect() again, and we also use the Callable.bind() method,
  494. # which returns a new Callable with the parameter binds.
  495. player.hit.connect(_on_player_hit.bind("sword", 100))
  496. func _on_button_down():
  497. print("Button down!")
  498. func _on_player_hit(weapon_type, damage):
  499. print("Hit with weapon %s for %d damage." % [weapon_type, damage])
  500. .. code-tab:: csharp
  501. public override void _Ready()
  502. {
  503. var button = new Button();
  504. // C# supports passing signals as events, so we can use this idiomatic construct:
  505. button.ButtonDown += OnButtonDown;
  506. // This assumes that a `Player` class exists, which defines a `Hit` signal.
  507. var player = new Player();
  508. // We can use lambdas when we need to bind additional parameters.
  509. player.Hit += () => OnPlayerHit("sword", 100);
  510. }
  511. private void OnButtonDown()
  512. {
  513. GD.Print("Button down!");
  514. }
  515. private void OnPlayerHit(string weaponType, int damage)
  516. {
  517. GD.Print($"Hit with weapon {weaponType} for {damage} damage.");
  518. }
  519. \ **\ ``Object.connect()`` or ``Signal.connect()``?**\
  520. As seen above, the recommended method to connect signals is not :ref:`connect<class_Object_method_connect>`. The code block below shows the four options for connecting signals, using either this legacy method or the recommended :ref:`Signal.connect<class_Signal_method_connect>`, and using either an implicit :ref:`Callable<class_Callable>` or a manually defined one.
  521. .. tabs::
  522. .. code-tab:: gdscript
  523. func _ready():
  524. var button = Button.new()
  525. # Option 1: Object.connect() with an implicit Callable for the defined function.
  526. button.connect("button_down", _on_button_down)
  527. # Option 2: Object.connect() with a constructed Callable using a target object and method name.
  528. button.connect("button_down", Callable(self, "_on_button_down"))
  529. # Option 3: Signal.connect() with an implicit Callable for the defined function.
  530. button.button_down.connect(_on_button_down)
  531. # Option 4: Signal.connect() with a constructed Callable using a target object and method name.
  532. button.button_down.connect(Callable(self, "_on_button_down"))
  533. func _on_button_down():
  534. print("Button down!")
  535. .. code-tab:: csharp
  536. public override void _Ready()
  537. {
  538. var button = new Button();
  539. // Option 1: In C#, we can use signals as events and connect with this idiomatic syntax:
  540. button.ButtonDown += OnButtonDown;
  541. // Option 2: GodotObject.Connect() with a constructed Callable from a method group.
  542. button.Connect(Button.SignalName.ButtonDown, Callable.From(OnButtonDown));
  543. // Option 3: GodotObject.Connect() with a constructed Callable using a target object and method name.
  544. button.Connect(Button.SignalName.ButtonDown, new Callable(this, MethodName.OnButtonDown));
  545. }
  546. private void OnButtonDown()
  547. {
  548. GD.Print("Button down!");
  549. }
  550. While all options have the same outcome (``button``'s :ref:`BaseButton.button_down<class_BaseButton_signal_button_down>` signal will be connected to ``_on_button_down``), **option 3** offers the best validation: it will print a compile-time error if either the ``button_down`` :ref:`Signal<class_Signal>` or the ``_on_button_down`` :ref:`Callable<class_Callable>` are not defined. On the other hand, **option 2** only relies on string names and will only be able to validate either names at runtime: it will print a runtime error if ``"button_down"`` doesn't correspond to a signal, or if ``"_on_button_down"`` is not a registered method in the object ``self``. The main reason for using options 1, 2, or 4 would be if you actually need to use strings (e.g. to connect signals programmatically based on strings read from a configuration file). Otherwise, option 3 is the recommended (and fastest) method.
  551. \ **Binding and passing parameters:**\
  552. The syntax to bind parameters is through :ref:`Callable.bind<class_Callable_method_bind>`, which returns a copy of the :ref:`Callable<class_Callable>` with its parameters bound.
  553. When calling :ref:`emit_signal<class_Object_method_emit_signal>`, the signal parameters can be also passed. The examples below show the relationship between these signal parameters and bound parameters.
  554. .. tabs::
  555. .. code-tab:: gdscript
  556. func _ready():
  557. # This assumes that a `Player` class exists, which defines a `hit` signal.
  558. var player = Player.new()
  559. # Using Callable.bind().
  560. player.hit.connect(_on_player_hit.bind("sword", 100))
  561. # Parameters added when emitting the signal are passed first.
  562. player.emit_signal("hit", "Dark lord", 5)
  563. # We pass two arguments when emitting (`hit_by`, `level`),
  564. # and bind two more arguments when connecting (`weapon_type`, `damage`).
  565. func _on_player_hit(hit_by, level, weapon_type, damage):
  566. print("Hit by %s (level %d) with weapon %s for %d damage." % [hit_by, level, weapon_type, damage])
  567. .. code-tab:: csharp
  568. public override void _Ready()
  569. {
  570. // This assumes that a `Player` class exists, which defines a `Hit` signal.
  571. var player = new Player();
  572. // Using lambda expressions that create a closure that captures the additional parameters.
  573. // The lambda only receives the parameters defined by the signal's delegate.
  574. player.Hit += (hitBy, level) => OnPlayerHit(hitBy, level, "sword", 100);
  575. // Parameters added when emitting the signal are passed first.
  576. player.EmitSignal(SignalName.Hit, "Dark lord", 5);
  577. }
  578. // We pass two arguments when emitting (`hit_by`, `level`),
  579. // and bind two more arguments when connecting (`weapon_type`, `damage`).
  580. private void OnPlayerHit(string hitBy, int level, string weaponType, int damage)
  581. {
  582. GD.Print($"Hit by {hitBy} (level {level}) with weapon {weaponType} for {damage} damage.");
  583. }
  584. .. rst-class:: classref-item-separator
  585. ----
  586. .. _class_Object_method_disconnect:
  587. .. rst-class:: classref-method
  588. void **disconnect** **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable **)**
  589. Disconnects a ``signal`` by name from a given ``callable``. If the connection does not exist, generates an error. Use :ref:`is_connected<class_Object_method_is_connected>` to make sure that the connection exists.
  590. .. rst-class:: classref-item-separator
  591. ----
  592. .. _class_Object_method_emit_signal:
  593. .. rst-class:: classref-method
  594. :ref:`Error<enum_@GlobalScope_Error>` **emit_signal** **(** :ref:`StringName<class_StringName>` signal, ... **)** |vararg|
  595. Emits the given ``signal`` by name. The signal must exist, so it should be a built-in signal of this class or one of its inherited classes, or a user-defined signal (see :ref:`add_user_signal<class_Object_method_add_user_signal>`). This method supports a variable number of arguments, so parameters can be passed as a comma separated list.
  596. Returns :ref:`@GlobalScope.ERR_UNAVAILABLE<class_@GlobalScope_constant_ERR_UNAVAILABLE>` if ``signal`` does not exist or the parameters are invalid.
  597. .. tabs::
  598. .. code-tab:: gdscript
  599. emit_signal("hit", "sword", 100)
  600. emit_signal("game_over")
  601. .. code-tab:: csharp
  602. EmitSignal(SignalName.Hit, "sword", 100);
  603. EmitSignal(SignalName.GameOver);
  604. \ **Note:** In C#, ``signal`` must be in snake_case when referring to built-in Godot signals. Prefer using the names exposed in the ``SignalName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  605. .. rst-class:: classref-item-separator
  606. ----
  607. .. _class_Object_method_free:
  608. .. rst-class:: classref-method
  609. void **free** **(** **)**
  610. Deletes the object from memory. Pre-existing references to the object become invalid, and any attempt to access them will result in a run-time error. Checking the references with :ref:`@GlobalScope.is_instance_valid<class_@GlobalScope_method_is_instance_valid>` will return ``false``.
  611. .. rst-class:: classref-item-separator
  612. ----
  613. .. _class_Object_method_get:
  614. .. rst-class:: classref-method
  615. :ref:`Variant<class_Variant>` **get** **(** :ref:`StringName<class_StringName>` property **)** |const|
  616. Returns the :ref:`Variant<class_Variant>` value of the given ``property``. If the ``property`` does not exist, this method returns ``null``.
  617. .. tabs::
  618. .. code-tab:: gdscript
  619. var node = Node2D.new()
  620. node.rotation = 1.5
  621. var a = node.get("rotation") # a is 1.5
  622. .. code-tab:: csharp
  623. var node = new Node2D();
  624. node.Rotation = 1.5f;
  625. var a = node.Get("rotation"); // a is 1.5
  626. \ **Note:** In C#, ``property`` must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the ``PropertyName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  627. .. rst-class:: classref-item-separator
  628. ----
  629. .. _class_Object_method_get_class:
  630. .. rst-class:: classref-method
  631. :ref:`String<class_String>` **get_class** **(** **)** |const|
  632. Returns the object's built-in class name, as a :ref:`String<class_String>`. See also :ref:`is_class<class_Object_method_is_class>`.
  633. \ **Note:** This method ignores ``class_name`` declarations. If this object's script has defined a ``class_name``, the base, built-in class name is returned instead.
  634. .. rst-class:: classref-item-separator
  635. ----
  636. .. _class_Object_method_get_incoming_connections:
  637. .. rst-class:: classref-method
  638. :ref:`Dictionary[]<class_Dictionary>` **get_incoming_connections** **(** **)** |const|
  639. Returns an :ref:`Array<class_Array>` of signal connections received by this object. Each connection is represented as a :ref:`Dictionary<class_Dictionary>` that contains three entries:
  640. - ``signal`` is a reference to the :ref:`Signal<class_Signal>`;
  641. - ``callable`` is a reference to the :ref:`Callable<class_Callable>`;
  642. - ``flags`` is a combination of :ref:`ConnectFlags<enum_Object_ConnectFlags>`.
  643. .. rst-class:: classref-item-separator
  644. ----
  645. .. _class_Object_method_get_indexed:
  646. .. rst-class:: classref-method
  647. :ref:`Variant<class_Variant>` **get_indexed** **(** :ref:`NodePath<class_NodePath>` property_path **)** |const|
  648. Gets the object's property indexed by the given ``property_path``. The path should be a :ref:`NodePath<class_NodePath>` relative to the current object and can use the colon character (``:``) to access nested properties.
  649. \ **Examples:** ``"position:x"`` or ``"material:next_pass:blend_mode"``.
  650. .. tabs::
  651. .. code-tab:: gdscript
  652. var node = Node2D.new()
  653. node.position = Vector2(5, -10)
  654. var a = node.get_indexed("position") # a is Vector2(5, -10)
  655. var b = node.get_indexed("position:y") # b is -10
  656. .. code-tab:: csharp
  657. var node = new Node2D();
  658. node.Position = new Vector2(5, -10);
  659. var a = node.GetIndexed("position"); // a is Vector2(5, -10)
  660. var b = node.GetIndexed("position:y"); // b is -10
  661. \ **Note:** In C#, ``property_path`` must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the ``PropertyName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  662. \ **Note:** This method does not support actual paths to nodes in the :ref:`SceneTree<class_SceneTree>`, only sub-property paths. In the context of nodes, use :ref:`Node.get_node_and_resource<class_Node_method_get_node_and_resource>` instead.
  663. .. rst-class:: classref-item-separator
  664. ----
  665. .. _class_Object_method_get_instance_id:
  666. .. rst-class:: classref-method
  667. :ref:`int<class_int>` **get_instance_id** **(** **)** |const|
  668. Returns the object's unique instance ID. This ID can be saved in :ref:`EncodedObjectAsID<class_EncodedObjectAsID>`, and can be used to retrieve this object instance with :ref:`@GlobalScope.instance_from_id<class_@GlobalScope_method_instance_from_id>`.
  669. .. rst-class:: classref-item-separator
  670. ----
  671. .. _class_Object_method_get_meta:
  672. .. rst-class:: classref-method
  673. :ref:`Variant<class_Variant>` **get_meta** **(** :ref:`StringName<class_StringName>` name, :ref:`Variant<class_Variant>` default=null **)** |const|
  674. Returns the object's metadata value for the given entry ``name``. If the entry does not exist, returns ``default``. If ``default`` is ``null``, an error is also generated.
  675. \ **Note:** A metadata's ``name`` must be a valid identifier as per :ref:`StringName.is_valid_identifier<class_StringName_method_is_valid_identifier>` method.
  676. \ **Note:** Metadata that has a ``name`` starting with an underscore (``_``) is considered editor-only. Editor-only metadata is not displayed in the Inspector and should not be edited, although it can still be found by this method.
  677. .. rst-class:: classref-item-separator
  678. ----
  679. .. _class_Object_method_get_meta_list:
  680. .. rst-class:: classref-method
  681. :ref:`PackedStringArray<class_PackedStringArray>` **get_meta_list** **(** **)** |const|
  682. Returns the object's metadata entry names as a :ref:`PackedStringArray<class_PackedStringArray>`.
  683. .. rst-class:: classref-item-separator
  684. ----
  685. .. _class_Object_method_get_method_list:
  686. .. rst-class:: classref-method
  687. :ref:`Dictionary[]<class_Dictionary>` **get_method_list** **(** **)** |const|
  688. Returns this object's methods and their signatures as an :ref:`Array<class_Array>` of dictionaries. Each :ref:`Dictionary<class_Dictionary>` contains the following entries:
  689. - ``name`` is the name of the method, as a :ref:`String<class_String>`;
  690. - ``args`` is an :ref:`Array<class_Array>` of dictionaries representing the arguments;
  691. - ``default_args`` is the default arguments as an :ref:`Array<class_Array>` of variants;
  692. - ``flags`` is a combination of :ref:`MethodFlags<enum_@GlobalScope_MethodFlags>`;
  693. - ``id`` is the method's internal identifier :ref:`int<class_int>`;
  694. - ``return`` is the returned value, as a :ref:`Dictionary<class_Dictionary>`;
  695. \ **Note:** The dictionaries of ``args`` and ``return`` are formatted identically to the results of :ref:`get_property_list<class_Object_method_get_property_list>`, although not all entries are used.
  696. .. rst-class:: classref-item-separator
  697. ----
  698. .. _class_Object_method_get_property_list:
  699. .. rst-class:: classref-method
  700. :ref:`Dictionary[]<class_Dictionary>` **get_property_list** **(** **)** |const|
  701. Returns the object's property list as an :ref:`Array<class_Array>` of dictionaries. Each :ref:`Dictionary<class_Dictionary>` contains the following entries:
  702. - ``name`` is the property's name, as a :ref:`String<class_String>`;
  703. - ``class_name`` is an empty :ref:`StringName<class_StringName>`, unless the property is :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>` and it inherits from a class;
  704. - ``type`` is the property's type, as an :ref:`int<class_int>` (see :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>`);
  705. - ``hint`` is *how* the property is meant to be edited (see :ref:`PropertyHint<enum_@GlobalScope_PropertyHint>`);
  706. - ``hint_string`` depends on the hint (see :ref:`PropertyHint<enum_@GlobalScope_PropertyHint>`);
  707. - ``usage`` is a combination of :ref:`PropertyUsageFlags<enum_@GlobalScope_PropertyUsageFlags>`.
  708. .. rst-class:: classref-item-separator
  709. ----
  710. .. _class_Object_method_get_script:
  711. .. rst-class:: classref-method
  712. :ref:`Variant<class_Variant>` **get_script** **(** **)** |const|
  713. Returns the object's :ref:`Script<class_Script>` instance, or ``null`` if no script is attached.
  714. .. rst-class:: classref-item-separator
  715. ----
  716. .. _class_Object_method_get_signal_connection_list:
  717. .. rst-class:: classref-method
  718. :ref:`Dictionary[]<class_Dictionary>` **get_signal_connection_list** **(** :ref:`StringName<class_StringName>` signal **)** |const|
  719. Returns an :ref:`Array<class_Array>` of connections for the given ``signal`` name. Each connection is represented as a :ref:`Dictionary<class_Dictionary>` that contains three entries:
  720. - ``signal`` is a reference to the :ref:`Signal<class_Signal>`;
  721. - ``callable`` is a reference to the connected :ref:`Callable<class_Callable>`;
  722. - ``flags`` is a combination of :ref:`ConnectFlags<enum_Object_ConnectFlags>`.
  723. .. rst-class:: classref-item-separator
  724. ----
  725. .. _class_Object_method_get_signal_list:
  726. .. rst-class:: classref-method
  727. :ref:`Dictionary[]<class_Dictionary>` **get_signal_list** **(** **)** |const|
  728. Returns the list of existing signals as an :ref:`Array<class_Array>` of dictionaries.
  729. \ **Note:** Due of the implementation, each :ref:`Dictionary<class_Dictionary>` is formatted very similarly to the returned values of :ref:`get_method_list<class_Object_method_get_method_list>`.
  730. .. rst-class:: classref-item-separator
  731. ----
  732. .. _class_Object_method_has_meta:
  733. .. rst-class:: classref-method
  734. :ref:`bool<class_bool>` **has_meta** **(** :ref:`StringName<class_StringName>` name **)** |const|
  735. Returns ``true`` if a metadata entry is found with the given ``name``. See also :ref:`get_meta<class_Object_method_get_meta>`, :ref:`set_meta<class_Object_method_set_meta>` and :ref:`remove_meta<class_Object_method_remove_meta>`.
  736. \ **Note:** A metadata's ``name`` must be a valid identifier as per :ref:`StringName.is_valid_identifier<class_StringName_method_is_valid_identifier>` method.
  737. \ **Note:** Metadata that has a ``name`` starting with an underscore (``_``) is considered editor-only. Editor-only metadata is not displayed in the Inspector and should not be edited, although it can still be found by this method.
  738. .. rst-class:: classref-item-separator
  739. ----
  740. .. _class_Object_method_has_method:
  741. .. rst-class:: classref-method
  742. :ref:`bool<class_bool>` **has_method** **(** :ref:`StringName<class_StringName>` method **)** |const|
  743. Returns ``true`` if the given ``method`` name exists in the object.
  744. \ **Note:** In C#, ``method`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``MethodName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  745. .. rst-class:: classref-item-separator
  746. ----
  747. .. _class_Object_method_has_signal:
  748. .. rst-class:: classref-method
  749. :ref:`bool<class_bool>` **has_signal** **(** :ref:`StringName<class_StringName>` signal **)** |const|
  750. Returns ``true`` if the given ``signal`` name exists in the object.
  751. \ **Note:** In C#, ``signal`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``SignalName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  752. .. rst-class:: classref-item-separator
  753. ----
  754. .. _class_Object_method_has_user_signal:
  755. .. rst-class:: classref-method
  756. :ref:`bool<class_bool>` **has_user_signal** **(** :ref:`StringName<class_StringName>` signal **)** |const|
  757. Returns ``true`` if the given user-defined ``signal`` name exists. Only signals added with :ref:`add_user_signal<class_Object_method_add_user_signal>` are included.
  758. .. rst-class:: classref-item-separator
  759. ----
  760. .. _class_Object_method_is_blocking_signals:
  761. .. rst-class:: classref-method
  762. :ref:`bool<class_bool>` **is_blocking_signals** **(** **)** |const|
  763. Returns ``true`` if the object is blocking its signals from being emitted. See :ref:`set_block_signals<class_Object_method_set_block_signals>`.
  764. .. rst-class:: classref-item-separator
  765. ----
  766. .. _class_Object_method_is_class:
  767. .. rst-class:: classref-method
  768. :ref:`bool<class_bool>` **is_class** **(** :ref:`String<class_String>` class **)** |const|
  769. Returns ``true`` if the object inherits from the given ``class``. See also :ref:`get_class<class_Object_method_get_class>`.
  770. .. tabs::
  771. .. code-tab:: gdscript
  772. var sprite2d = Sprite2D.new()
  773. sprite2d.is_class("Sprite2D") # Returns true
  774. sprite2d.is_class("Node") # Returns true
  775. sprite2d.is_class("Node3D") # Returns false
  776. .. code-tab:: csharp
  777. var sprite2D = new Sprite2D();
  778. sprite2D.IsClass("Sprite2D"); // Returns true
  779. sprite2D.IsClass("Node"); // Returns true
  780. sprite2D.IsClass("Node3D"); // Returns false
  781. \ **Note:** This method ignores ``class_name`` declarations in the object's script.
  782. .. rst-class:: classref-item-separator
  783. ----
  784. .. _class_Object_method_is_connected:
  785. .. rst-class:: classref-method
  786. :ref:`bool<class_bool>` **is_connected** **(** :ref:`StringName<class_StringName>` signal, :ref:`Callable<class_Callable>` callable **)** |const|
  787. Returns ``true`` if a connection exists between the given ``signal`` name and ``callable``.
  788. \ **Note:** In C#, ``signal`` must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the ``SignalName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  789. .. rst-class:: classref-item-separator
  790. ----
  791. .. _class_Object_method_is_queued_for_deletion:
  792. .. rst-class:: classref-method
  793. :ref:`bool<class_bool>` **is_queued_for_deletion** **(** **)** |const|
  794. Returns ``true`` if the :ref:`Node.queue_free<class_Node_method_queue_free>` method was called for the object.
  795. .. rst-class:: classref-item-separator
  796. ----
  797. .. _class_Object_method_notification:
  798. .. rst-class:: classref-method
  799. void **notification** **(** :ref:`int<class_int>` what, :ref:`bool<class_bool>` reversed=false **)**
  800. Sends the given ``what`` notification to all classes inherited by the object, triggering calls to :ref:`_notification<class_Object_method__notification>`, starting from the highest ancestor (the **Object** class) and going down to the object's script.
  801. If ``reversed`` is ``true``, the call order is reversed.
  802. .. tabs::
  803. .. code-tab:: gdscript
  804. var player = Node2D.new()
  805. player.set_script(load("res://player.gd"))
  806. player.notification(NOTIFICATION_ENTER_TREE)
  807. # The call order is Object -> Node -> Node2D -> player.gd.
  808. player.notification(NOTIFICATION_ENTER_TREE, true)
  809. # The call order is player.gd -> Node2D -> Node -> Object.
  810. .. code-tab:: csharp
  811. var player = new Node2D();
  812. player.SetScript(GD.Load("res://player.gd"));
  813. player.Notification(NotificationEnterTree);
  814. // The call order is GodotObject -> Node -> Node2D -> player.gd.
  815. player.Notification(NotificationEnterTree, true);
  816. // The call order is player.gd -> Node2D -> Node -> GodotObject.
  817. .. rst-class:: classref-item-separator
  818. ----
  819. .. _class_Object_method_notify_property_list_changed:
  820. .. rst-class:: classref-method
  821. void **notify_property_list_changed** **(** **)**
  822. Emits the :ref:`property_list_changed<class_Object_signal_property_list_changed>` signal. This is mainly used to refresh the editor, so that the Inspector and editor plugins are properly updated.
  823. .. rst-class:: classref-item-separator
  824. ----
  825. .. _class_Object_method_property_can_revert:
  826. .. rst-class:: classref-method
  827. :ref:`bool<class_bool>` **property_can_revert** **(** :ref:`StringName<class_StringName>` property **)** |const|
  828. Returns ``true`` if the given ``property`` has a custom default value. Use :ref:`property_get_revert<class_Object_method_property_get_revert>` to get the ``property``'s default value.
  829. \ **Note:** This method is used by the Inspector dock to display a revert icon. The object must implement :ref:`_property_can_revert<class_Object_method__property_can_revert>` to customize the default value. If :ref:`_property_can_revert<class_Object_method__property_can_revert>` is not implemented, this method returns ``false``.
  830. .. rst-class:: classref-item-separator
  831. ----
  832. .. _class_Object_method_property_get_revert:
  833. .. rst-class:: classref-method
  834. :ref:`Variant<class_Variant>` **property_get_revert** **(** :ref:`StringName<class_StringName>` property **)** |const|
  835. Returns the custom default value of the given ``property``. Use :ref:`property_can_revert<class_Object_method_property_can_revert>` to check if the ``property`` has a custom default value.
  836. \ **Note:** This method is used by the Inspector dock to display a revert icon. The object must implement :ref:`_property_get_revert<class_Object_method__property_get_revert>` to customize the default value. If :ref:`_property_get_revert<class_Object_method__property_get_revert>` is not implemented, this method returns ``null``.
  837. .. rst-class:: classref-item-separator
  838. ----
  839. .. _class_Object_method_remove_meta:
  840. .. rst-class:: classref-method
  841. void **remove_meta** **(** :ref:`StringName<class_StringName>` name **)**
  842. Removes the given entry ``name`` from the object's metadata. See also :ref:`has_meta<class_Object_method_has_meta>`, :ref:`get_meta<class_Object_method_get_meta>` and :ref:`set_meta<class_Object_method_set_meta>`.
  843. \ **Note:** A metadata's ``name`` must be a valid identifier as per :ref:`StringName.is_valid_identifier<class_StringName_method_is_valid_identifier>` method.
  844. \ **Note:** Metadata that has a ``name`` starting with an underscore (``_``) is considered editor-only. Editor-only metadata is not displayed in the Inspector and should not be edited, although it can still be found by this method.
  845. .. rst-class:: classref-item-separator
  846. ----
  847. .. _class_Object_method_set:
  848. .. rst-class:: classref-method
  849. void **set** **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)**
  850. Assigns ``value`` to the given ``property``. If the property does not exist or the given ``value``'s type doesn't match, nothing happens.
  851. .. tabs::
  852. .. code-tab:: gdscript
  853. var node = Node2D.new()
  854. node.set("global_scale", Vector2(8, 2.5))
  855. print(node.global_scale) # Prints (8, 2.5)
  856. .. code-tab:: csharp
  857. var node = new Node2D();
  858. node.Set("global_scale", new Vector2(8, 2.5));
  859. GD.Print(node.GlobalScale); // Prints Vector2(8, 2.5)
  860. \ **Note:** In C#, ``property`` must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the ``PropertyName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  861. .. rst-class:: classref-item-separator
  862. ----
  863. .. _class_Object_method_set_block_signals:
  864. .. rst-class:: classref-method
  865. void **set_block_signals** **(** :ref:`bool<class_bool>` enable **)**
  866. If set to ``true``, the object becomes unable to emit signals. As such, :ref:`emit_signal<class_Object_method_emit_signal>` and signal connections will not work, until it is set to ``false``.
  867. .. rst-class:: classref-item-separator
  868. ----
  869. .. _class_Object_method_set_deferred:
  870. .. rst-class:: classref-method
  871. void **set_deferred** **(** :ref:`StringName<class_StringName>` property, :ref:`Variant<class_Variant>` value **)**
  872. Assigns ``value`` to the given ``property``, after the current frame's physics step. This is equivalent to calling :ref:`set<class_Object_method_set>` through :ref:`call_deferred<class_Object_method_call_deferred>`.
  873. .. tabs::
  874. .. code-tab:: gdscript
  875. var node = Node2D.new()
  876. add_child(node)
  877. node.rotation = 45.0
  878. node.set_deferred("rotation", 90.0)
  879. print(node.rotation) # Prints 45.0
  880. await get_tree().process_frame
  881. print(node.rotation) # Prints 90.0
  882. .. code-tab:: csharp
  883. var node = new Node2D();
  884. node.Rotation = 45f;
  885. node.SetDeferred("rotation", 90f);
  886. GD.Print(node.Rotation); // Prints 45.0
  887. await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
  888. GD.Print(node.Rotation); // Prints 90.0
  889. \ **Note:** In C#, ``property`` must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the ``PropertyName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  890. .. rst-class:: classref-item-separator
  891. ----
  892. .. _class_Object_method_set_indexed:
  893. .. rst-class:: classref-method
  894. void **set_indexed** **(** :ref:`NodePath<class_NodePath>` property_path, :ref:`Variant<class_Variant>` value **)**
  895. Assigns a new ``value`` to the property identified by the ``property_path``. The path should be a :ref:`NodePath<class_NodePath>` relative to this object, and can use the colon character (``:``) to access nested properties.
  896. .. tabs::
  897. .. code-tab:: gdscript
  898. var node = Node2D.new()
  899. node.set_indexed("position", Vector2(42, 0))
  900. node.set_indexed("position:y", -10)
  901. print(node.position) # Prints (42, -10)
  902. .. code-tab:: csharp
  903. var node = new Node2D();
  904. node.SetIndexed("position", new Vector2(42, 0));
  905. node.SetIndexed("position:y", -10);
  906. GD.Print(node.Position); // Prints (42, -10)
  907. \ **Note:** In C#, ``property_path`` must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the ``PropertyName`` class to avoid allocating a new :ref:`StringName<class_StringName>` on each call.
  908. .. rst-class:: classref-item-separator
  909. ----
  910. .. _class_Object_method_set_message_translation:
  911. .. rst-class:: classref-method
  912. void **set_message_translation** **(** :ref:`bool<class_bool>` enable **)**
  913. If set to ``true``, allows the object to translate messages with :ref:`tr<class_Object_method_tr>` and :ref:`tr_n<class_Object_method_tr_n>`. Enabled by default. See also :ref:`can_translate_messages<class_Object_method_can_translate_messages>`.
  914. .. rst-class:: classref-item-separator
  915. ----
  916. .. _class_Object_method_set_meta:
  917. .. rst-class:: classref-method
  918. void **set_meta** **(** :ref:`StringName<class_StringName>` name, :ref:`Variant<class_Variant>` value **)**
  919. Adds or changes the entry ``name`` inside the object's metadata. The metadata ``value`` can be any :ref:`Variant<class_Variant>`, although some types cannot be serialized correctly.
  920. If ``value`` is ``null``, the entry is removed. This is the equivalent of using :ref:`remove_meta<class_Object_method_remove_meta>`. See also :ref:`has_meta<class_Object_method_has_meta>` and :ref:`get_meta<class_Object_method_get_meta>`.
  921. \ **Note:** A metadata's ``name`` must be a valid identifier as per :ref:`StringName.is_valid_identifier<class_StringName_method_is_valid_identifier>` method.
  922. \ **Note:** Metadata that has a ``name`` starting with an underscore (``_``) is considered editor-only. Editor-only metadata is not displayed in the Inspector and should not be edited, although it can still be found by this method.
  923. .. rst-class:: classref-item-separator
  924. ----
  925. .. _class_Object_method_set_script:
  926. .. rst-class:: classref-method
  927. void **set_script** **(** :ref:`Variant<class_Variant>` script **)**
  928. Attaches ``script`` to the object, and instantiates it. As a result, the script's :ref:`_init<class_Object_method__init>` is called. A :ref:`Script<class_Script>` is used to extend the object's functionality.
  929. If a script already exists, its instance is detached, and its property values and state are lost. Built-in property values are still kept.
  930. .. rst-class:: classref-item-separator
  931. ----
  932. .. _class_Object_method_to_string:
  933. .. rst-class:: classref-method
  934. :ref:`String<class_String>` **to_string** **(** **)**
  935. Returns a :ref:`String<class_String>` representing the object. Defaults to ``"<ClassName#RID>"``. Override :ref:`_to_string<class_Object_method__to_string>` to customize the string representation of the object.
  936. .. rst-class:: classref-item-separator
  937. ----
  938. .. _class_Object_method_tr:
  939. .. rst-class:: classref-method
  940. :ref:`String<class_String>` **tr** **(** :ref:`StringName<class_StringName>` message, :ref:`StringName<class_StringName>` context="" **)** |const|
  941. Translates a ``message``, using the translation catalogs configured in the Project Settings. Further ``context`` can be specified to help with the translation.
  942. If :ref:`can_translate_messages<class_Object_method_can_translate_messages>` is ``false``, or no translation is available, this method returns the ``message`` without changes. See :ref:`set_message_translation<class_Object_method_set_message_translation>`.
  943. For detailed examples, see :doc:`Internationalizing games <../tutorials/i18n/internationalizing_games>`.
  944. .. rst-class:: classref-item-separator
  945. ----
  946. .. _class_Object_method_tr_n:
  947. .. rst-class:: classref-method
  948. :ref:`String<class_String>` **tr_n** **(** :ref:`StringName<class_StringName>` message, :ref:`StringName<class_StringName>` plural_message, :ref:`int<class_int>` n, :ref:`StringName<class_StringName>` context="" **)** |const|
  949. Translates a ``message`` or ``plural_message``, using the translation catalogs configured in the Project Settings. Further ``context`` can be specified to help with the translation.
  950. If :ref:`can_translate_messages<class_Object_method_can_translate_messages>` is ``false``, or no translation is available, this method returns ``message`` or ``plural_message``, without changes. See :ref:`set_message_translation<class_Object_method_set_message_translation>`.
  951. The ``n`` is the number, or amount, of the message's subject. It is used by the translation system to fetch the correct plural form for the current language.
  952. For detailed examples, see :doc:`Localization using gettext <../tutorials/i18n/localization_using_gettext>`.
  953. \ **Note:** Negative and :ref:`float<class_float>` numbers may not properly apply to some countable subjects. It's recommended handling these cases with :ref:`tr<class_Object_method_tr>`.
  954. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  955. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  956. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  957. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  958. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  959. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`