node.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /**************************************************************************/
  2. /* node.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/input/input_event.h"
  32. #include "core/io/resource.h"
  33. #include "core/string/node_path.h"
  34. #include "core/templates/iterable.h"
  35. #include "core/variant/typed_array.h"
  36. #include "scene/main/scene_tree.h"
  37. #include "scene/scene_string_names.h"
  38. class Viewport;
  39. class Window;
  40. class SceneState;
  41. class Tween;
  42. class PropertyTweener;
  43. SAFE_FLAG_TYPE_PUN_GUARANTEES
  44. SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t)
  45. class Node : public Object {
  46. GDCLASS(Node, Object);
  47. protected:
  48. // During group processing, these are thread-safe.
  49. // Outside group processing, these avoid the cost of sync by working as plain primitive types.
  50. union MTFlag {
  51. SafeFlag mt;
  52. bool st;
  53. MTFlag() :
  54. mt{} {}
  55. };
  56. template <typename T>
  57. union MTNumeric {
  58. SafeNumeric<T> mt;
  59. T st;
  60. MTNumeric() :
  61. mt{} {}
  62. };
  63. public:
  64. static constexpr AncestralClass static_ancestral_class = AncestralClass::NODE;
  65. // N.B. Any enum stored as a bitfield should be specified as UNSIGNED to work around
  66. // some compilers trying to store it as signed, and requiring 1 more bit than necessary.
  67. enum ProcessMode : unsigned int {
  68. PROCESS_MODE_INHERIT, // same as parent node
  69. PROCESS_MODE_PAUSABLE, // process only if not paused
  70. PROCESS_MODE_WHEN_PAUSED, // process only if paused
  71. PROCESS_MODE_ALWAYS, // process always
  72. PROCESS_MODE_DISABLED, // never process
  73. };
  74. enum ProcessThreadGroup {
  75. PROCESS_THREAD_GROUP_INHERIT,
  76. PROCESS_THREAD_GROUP_MAIN_THREAD,
  77. PROCESS_THREAD_GROUP_SUB_THREAD,
  78. };
  79. enum ProcessThreadMessages {
  80. FLAG_PROCESS_THREAD_MESSAGES = 1,
  81. FLAG_PROCESS_THREAD_MESSAGES_PHYSICS = 2,
  82. FLAG_PROCESS_THREAD_MESSAGES_ALL = 3,
  83. };
  84. enum PhysicsInterpolationMode : unsigned int {
  85. PHYSICS_INTERPOLATION_MODE_INHERIT,
  86. PHYSICS_INTERPOLATION_MODE_ON,
  87. PHYSICS_INTERPOLATION_MODE_OFF,
  88. };
  89. enum DuplicateFlags {
  90. DUPLICATE_SIGNALS = 1,
  91. DUPLICATE_GROUPS = 2,
  92. DUPLICATE_SCRIPTS = 4,
  93. DUPLICATE_USE_INSTANTIATION = 8,
  94. DUPLICATE_INTERNAL_STATE = 16,
  95. DUPLICATE_DEFAULT = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION,
  96. #ifdef TOOLS_ENABLED
  97. DUPLICATE_FROM_EDITOR = 32,
  98. #endif
  99. };
  100. enum NameCasing {
  101. NAME_CASING_PASCAL_CASE,
  102. NAME_CASING_CAMEL_CASE,
  103. NAME_CASING_SNAKE_CASE,
  104. NAME_CASING_KEBAB_CASE,
  105. };
  106. enum InternalMode {
  107. INTERNAL_MODE_DISABLED,
  108. INTERNAL_MODE_FRONT,
  109. INTERNAL_MODE_BACK,
  110. };
  111. enum AutoTranslateMode : unsigned int {
  112. AUTO_TRANSLATE_MODE_INHERIT,
  113. AUTO_TRANSLATE_MODE_ALWAYS,
  114. AUTO_TRANSLATE_MODE_DISABLED,
  115. };
  116. struct Comparator {
  117. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
  118. };
  119. #ifdef DEBUG_ENABLED
  120. static SafeNumeric<uint64_t> total_node_count;
  121. #endif
  122. enum {
  123. UNIQUE_SCENE_ID_UNASSIGNED = 0
  124. };
  125. void _update_process(bool p_enable, bool p_for_children);
  126. struct ChildrenIterator {
  127. _FORCE_INLINE_ Node *&operator*() const { return *_ptr; }
  128. _FORCE_INLINE_ Node **operator->() const { return _ptr; }
  129. _FORCE_INLINE_ ChildrenIterator &operator++() {
  130. _ptr++;
  131. return *this;
  132. }
  133. _FORCE_INLINE_ ChildrenIterator &operator--() {
  134. _ptr--;
  135. return *this;
  136. }
  137. _FORCE_INLINE_ bool operator==(const ChildrenIterator &b) const { return _ptr == b._ptr; }
  138. _FORCE_INLINE_ bool operator!=(const ChildrenIterator &b) const { return _ptr != b._ptr; }
  139. ChildrenIterator(Node **p_ptr) { _ptr = p_ptr; }
  140. ChildrenIterator() {}
  141. ChildrenIterator(const ChildrenIterator &p_it) { _ptr = p_it._ptr; }
  142. private:
  143. Node **_ptr = nullptr;
  144. };
  145. private:
  146. struct GroupData {
  147. bool persistent = false;
  148. SceneTree::Group *group = nullptr;
  149. };
  150. struct ComparatorByIndex {
  151. bool operator()(const Node *p_left, const Node *p_right) const {
  152. static const uint32_t order[3] = { 1, 0, 2 };
  153. uint32_t order_left = order[p_left->data.internal_mode];
  154. uint32_t order_right = order[p_right->data.internal_mode];
  155. if (order_left == order_right) {
  156. return p_left->data.index < p_right->data.index;
  157. }
  158. return order_left < order_right;
  159. }
  160. };
  161. struct ComparatorWithPriority {
  162. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->data.process_priority == p_a->data.process_priority ? p_b->is_greater_than(p_a) : p_b->data.process_priority > p_a->data.process_priority; }
  163. };
  164. struct ComparatorWithPhysicsPriority {
  165. bool operator()(const Node *p_a, const Node *p_b) const { return p_b->data.physics_process_priority == p_a->data.physics_process_priority ? p_b->is_greater_than(p_a) : p_b->data.physics_process_priority > p_a->data.physics_process_priority; }
  166. };
  167. // This Data struct is to avoid namespace pollution in derived classes.
  168. struct Data {
  169. String scene_file_path;
  170. Ref<SceneState> instance_state;
  171. Ref<SceneState> inherited_state;
  172. Node *parent = nullptr;
  173. Node *owner = nullptr;
  174. HashMap<StringName, Node *> children;
  175. mutable bool children_cache_dirty = false;
  176. mutable LocalVector<Node *> children_cache;
  177. HashMap<StringName, Node *> owned_unique_nodes;
  178. bool unique_name_in_owner = false;
  179. InternalMode internal_mode = INTERNAL_MODE_DISABLED;
  180. mutable int internal_children_front_count_cache = 0;
  181. mutable int internal_children_back_count_cache = 0;
  182. mutable int external_children_count_cache = 0;
  183. mutable int index = -1; // relative to front, normal or back.
  184. int32_t depth = -1;
  185. int blocked = 0; // Safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
  186. StringName name;
  187. SceneTree *tree = nullptr;
  188. String editor_description;
  189. Viewport *viewport = nullptr;
  190. mutable RID accessibility_element;
  191. HashMap<StringName, GroupData> grouped;
  192. List<Node *>::Element *OW = nullptr; // Owned element.
  193. List<Node *> owned;
  194. Node *process_owner = nullptr;
  195. ProcessThreadGroup process_thread_group = PROCESS_THREAD_GROUP_INHERIT;
  196. Node *process_thread_group_owner = nullptr;
  197. int process_thread_group_order = 0;
  198. BitField<ProcessThreadMessages> process_thread_messages = {};
  199. void *process_group = nullptr; // to avoid cyclic dependency
  200. int multiplayer_authority = 1; // Server by default.
  201. Variant rpc_config;
  202. // Variables used to properly sort the node when processing, ignored otherwise.
  203. int process_priority = 0;
  204. int physics_process_priority = 0;
  205. // Keep bitpacked values together to get better packing.
  206. ProcessMode process_mode : 3;
  207. PhysicsInterpolationMode physics_interpolation_mode : 2;
  208. AutoTranslateMode auto_translate_mode : 2;
  209. bool physics_process : 1;
  210. bool process : 1;
  211. bool physics_process_internal : 1;
  212. bool process_internal : 1;
  213. bool input : 1;
  214. bool shortcut_input : 1;
  215. bool unhandled_input : 1;
  216. bool unhandled_key_input : 1;
  217. // Physics interpolation can be turned on and off on a per node basis.
  218. // This only takes effect when the SceneTree (or project setting) physics interpolation
  219. // is switched on.
  220. bool physics_interpolated : 1;
  221. // We can auto-reset physics interpolation when e.g. adding a node for the first time.
  222. bool physics_interpolation_reset_requested : 1;
  223. // Most nodes need not be interpolated in the scene tree, physics interpolation
  224. // is normally only needed in the RenderingServer. However if we need to read the
  225. // interpolated transform of a node in the SceneTree, it is necessary to duplicate
  226. // the interpolation logic client side, in order to prevent stalling the RenderingServer
  227. // by reading back.
  228. bool physics_interpolated_client_side : 1;
  229. // For certain nodes (e.g. CPU particles in global mode)
  230. // it can be useful to not send the instance transform to the
  231. // RenderingServer, and specify the mesh in world space.
  232. bool use_identity_transform : 1;
  233. bool use_placeholder : 1;
  234. bool display_folded : 1;
  235. bool editable_instance : 1;
  236. bool ready_notified : 1;
  237. bool ready_first : 1;
  238. mutable bool is_auto_translating : 1;
  239. mutable bool is_auto_translate_dirty : 1;
  240. mutable bool is_translation_domain_inherited : 1;
  241. mutable bool is_translation_domain_dirty : 1;
  242. int32_t unique_scene_id = UNIQUE_SCENE_ID_UNASSIGNED;
  243. mutable NodePath *path_cache = nullptr;
  244. } data;
  245. String _get_tree_string_pretty(const String &p_prefix, bool p_last);
  246. String _get_tree_string(const Node *p_node);
  247. Node *_get_child_by_name(const StringName &p_name) const;
  248. void _replace_connections_target(Node *p_new_target);
  249. void _validate_child_name(Node *p_child, bool p_force_human_readable = false);
  250. void _generate_serial_child_name(const Node *p_child, StringName &name) const;
  251. void _propagate_reverse_notification(int p_notification);
  252. void _propagate_deferred_notification(int p_notification, bool p_reverse);
  253. void _propagate_enter_tree();
  254. void _propagate_ready();
  255. void _propagate_exit_tree();
  256. void _propagate_after_exit_tree();
  257. void _propagate_physics_interpolated(bool p_interpolated);
  258. void _propagate_physics_interpolation_reset_requested(bool p_requested);
  259. void _propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification);
  260. void _propagate_groups_dirty();
  261. void _propagate_translation_domain_dirty();
  262. Array _get_node_and_resource(const NodePath &p_path);
  263. void _duplicate_scripts(const Node *p_original, Node *p_copy) const;
  264. void _duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const;
  265. void _duplicate_signals(const Node *p_original, Node *p_copy) const;
  266. Node *_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap = nullptr) const;
  267. TypedArray<StringName> _get_groups() const;
  268. Error _rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  269. Error _rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  270. friend class SceneTree;
  271. void _set_tree(SceneTree *p_tree);
  272. void _propagate_pause_notification(bool p_enable);
  273. void _propagate_suspend_notification(bool p_enable);
  274. _FORCE_INLINE_ bool _can_process(bool p_paused) const;
  275. _FORCE_INLINE_ bool _is_enabled() const;
  276. void _release_unique_name_in_owner();
  277. void _acquire_unique_name_in_owner();
  278. void _clean_up_owner();
  279. _FORCE_INLINE_ void _update_children_cache() const {
  280. if (unlikely(data.children_cache_dirty)) {
  281. _update_children_cache_impl();
  282. }
  283. }
  284. void _update_children_cache_impl() const;
  285. // Process group management
  286. void _add_process_group();
  287. void _remove_process_group();
  288. void _add_to_process_thread_group();
  289. void _remove_from_process_thread_group();
  290. void _remove_tree_from_process_thread_group();
  291. void _add_tree_to_process_thread_group(Node *p_owner);
  292. static thread_local Node *current_process_thread_group;
  293. Variant _call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  294. Variant _call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  295. // Editor only signal to keep the SceneTreeEditor in sync.
  296. #ifdef TOOLS_ENABLED
  297. void _emit_editor_state_changed();
  298. #else
  299. void _emit_editor_state_changed() {}
  300. #endif
  301. protected:
  302. void _block() { data.blocked++; }
  303. void _unblock() { data.blocked--; }
  304. void _notification(int p_notification);
  305. virtual void _physics_interpolated_changed();
  306. virtual void add_child_notify(Node *p_child);
  307. virtual void remove_child_notify(Node *p_child);
  308. virtual void move_child_notify(Node *p_child);
  309. virtual void owner_changed_notify();
  310. void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
  311. static void _bind_methods();
  312. static String _get_name_num_separator();
  313. friend class SceneState;
  314. void _add_child_nocheck(Node *p_child, const StringName &p_name, InternalMode p_internal_mode = INTERNAL_MODE_DISABLED);
  315. void _set_owner_nocheck(Node *p_owner);
  316. void _set_name_nocheck(const StringName &p_name);
  317. void _set_physics_interpolated_client_side(bool p_enable) { data.physics_interpolated_client_side = p_enable; }
  318. bool _is_physics_interpolated_client_side() const { return data.physics_interpolated_client_side; }
  319. void _set_physics_interpolation_reset_requested(bool p_enable) { data.physics_interpolation_reset_requested = p_enable; }
  320. bool _is_physics_interpolation_reset_requested() const { return data.physics_interpolation_reset_requested; }
  321. void _set_use_identity_transform(bool p_enable) { data.use_identity_transform = p_enable; }
  322. bool _is_using_identity_transform() const { return data.use_identity_transform; }
  323. int32_t _get_scene_tree_depth() const { return data.depth; }
  324. //call from SceneTree
  325. void _call_input(const Ref<InputEvent> &p_event);
  326. void _call_shortcut_input(const Ref<InputEvent> &p_event);
  327. void _call_unhandled_input(const Ref<InputEvent> &p_event);
  328. void _call_unhandled_key_input(const Ref<InputEvent> &p_event);
  329. void _validate_property(PropertyInfo &p_property) const;
  330. virtual String _to_string() override;
  331. Variant _get_node_rpc_config_bind() const {
  332. return get_node_rpc_config().duplicate(true);
  333. }
  334. protected:
  335. virtual bool _uses_signal_mutex() const override { return false; } // Node uses thread guards instead.
  336. virtual void input(const Ref<InputEvent> &p_event);
  337. virtual void shortcut_input(const Ref<InputEvent> &p_key_event);
  338. virtual void unhandled_input(const Ref<InputEvent> &p_event);
  339. virtual void unhandled_key_input(const Ref<InputEvent> &p_key_event);
  340. GDVIRTUAL1(_process, double)
  341. GDVIRTUAL1(_physics_process, double)
  342. GDVIRTUAL0(_enter_tree)
  343. GDVIRTUAL0(_exit_tree)
  344. GDVIRTUAL0(_ready)
  345. GDVIRTUAL0RC(Vector<String>, _get_accessibility_configuration_warnings)
  346. GDVIRTUAL0RC(Vector<String>, _get_configuration_warnings)
  347. GDVIRTUAL1(_input, RequiredParam<InputEvent>)
  348. GDVIRTUAL1(_shortcut_input, RequiredParam<InputEvent>)
  349. GDVIRTUAL1(_unhandled_input, RequiredParam<InputEvent>)
  350. GDVIRTUAL1(_unhandled_key_input, RequiredParam<InputEvent>)
  351. GDVIRTUAL0RC(RID, _get_focused_accessibility_element)
  352. #ifndef DISABLE_DEPRECATED
  353. void _set_name_bind_compat_76560(const String &p_name);
  354. Variant _get_rpc_config_bind_compat_106848() const;
  355. static void _bind_compatibility_methods();
  356. #endif
  357. public:
  358. enum {
  359. // You can make your own, but don't use the same numbers as other notifications in other nodes.
  360. NOTIFICATION_ENTER_TREE = 10,
  361. NOTIFICATION_EXIT_TREE = 11,
  362. NOTIFICATION_MOVED_IN_PARENT = 12,
  363. NOTIFICATION_READY = 13,
  364. NOTIFICATION_PAUSED = 14,
  365. NOTIFICATION_UNPAUSED = 15,
  366. NOTIFICATION_PHYSICS_PROCESS = 16,
  367. NOTIFICATION_PROCESS = 17,
  368. NOTIFICATION_PARENTED = 18,
  369. NOTIFICATION_UNPARENTED = 19,
  370. NOTIFICATION_SCENE_INSTANTIATED = 20,
  371. NOTIFICATION_DRAG_BEGIN = 21,
  372. NOTIFICATION_DRAG_END = 22,
  373. NOTIFICATION_PATH_RENAMED = 23,
  374. NOTIFICATION_CHILD_ORDER_CHANGED = 24,
  375. NOTIFICATION_INTERNAL_PROCESS = 25,
  376. NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
  377. NOTIFICATION_POST_ENTER_TREE = 27,
  378. NOTIFICATION_DISABLED = 28,
  379. NOTIFICATION_ENABLED = 29,
  380. NOTIFICATION_RESET_PHYSICS_INTERPOLATION = 2001, // A GodotSpace Odyssey.
  381. // Keep these linked to Node.
  382. NOTIFICATION_ACCESSIBILITY_UPDATE = 3000,
  383. NOTIFICATION_ACCESSIBILITY_INVALIDATE = 3001,
  384. NOTIFICATION_WM_MOUSE_ENTER = 1002,
  385. NOTIFICATION_WM_MOUSE_EXIT = 1003,
  386. NOTIFICATION_WM_WINDOW_FOCUS_IN = 1004,
  387. NOTIFICATION_WM_WINDOW_FOCUS_OUT = 1005,
  388. NOTIFICATION_WM_CLOSE_REQUEST = 1006,
  389. NOTIFICATION_WM_GO_BACK_REQUEST = 1007,
  390. NOTIFICATION_WM_SIZE_CHANGED = 1008,
  391. NOTIFICATION_WM_DPI_CHANGE = 1009,
  392. NOTIFICATION_VP_MOUSE_ENTER = 1010,
  393. NOTIFICATION_VP_MOUSE_EXIT = 1011,
  394. NOTIFICATION_WM_POSITION_CHANGED = 1012,
  395. NOTIFICATION_OS_MEMORY_WARNING = MainLoop::NOTIFICATION_OS_MEMORY_WARNING,
  396. NOTIFICATION_TRANSLATION_CHANGED = MainLoop::NOTIFICATION_TRANSLATION_CHANGED,
  397. NOTIFICATION_WM_ABOUT = MainLoop::NOTIFICATION_WM_ABOUT,
  398. NOTIFICATION_CRASH = MainLoop::NOTIFICATION_CRASH,
  399. NOTIFICATION_OS_IME_UPDATE = MainLoop::NOTIFICATION_OS_IME_UPDATE,
  400. NOTIFICATION_APPLICATION_RESUMED = MainLoop::NOTIFICATION_APPLICATION_RESUMED,
  401. NOTIFICATION_APPLICATION_PAUSED = MainLoop::NOTIFICATION_APPLICATION_PAUSED,
  402. NOTIFICATION_APPLICATION_FOCUS_IN = MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN,
  403. NOTIFICATION_APPLICATION_FOCUS_OUT = MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT,
  404. NOTIFICATION_TEXT_SERVER_CHANGED = MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED,
  405. // Editor specific node notifications
  406. NOTIFICATION_EDITOR_PRE_SAVE = 9001,
  407. NOTIFICATION_EDITOR_POST_SAVE = 9002,
  408. NOTIFICATION_SUSPENDED = 9003,
  409. NOTIFICATION_UNSUSPENDED = 9004
  410. };
  411. /* NODE/TREE */
  412. StringName get_name() const;
  413. String get_description() const;
  414. void set_name(const StringName &p_name);
  415. InternalMode get_internal_mode() const;
  416. void add_child(RequiredParam<Node> rp_child, bool p_force_readable_name = false, InternalMode p_internal = INTERNAL_MODE_DISABLED);
  417. void add_sibling(RequiredParam<Node> rp_sibling, bool p_force_readable_name = false);
  418. void remove_child(RequiredParam<Node> rp_child);
  419. /// Optimal way to iterate the children of this node.
  420. /// The caller is responsible to ensure:
  421. /// - The thread has the rights to access the node (is_accessible_from_caller_thread() == true).
  422. /// - No children are inserted, removed, or have their index changed during iteration.
  423. template <bool p_include_internal = true>
  424. Iterable<ChildrenIterator> iterate_children() const;
  425. int get_child_count(bool p_include_internal = true) const;
  426. Node *get_child(int p_index, bool p_include_internal = true) const;
  427. TypedArray<Node> get_children(bool p_include_internal = true) const;
  428. bool has_node(const NodePath &p_path) const;
  429. Node *get_node(const NodePath &p_path) const;
  430. Node *get_node_or_null(const NodePath &p_path) const;
  431. Node *find_child(const String &p_pattern, bool p_recursive = true, bool p_owned = true) const;
  432. TypedArray<Node> find_children(const String &p_pattern, const String &p_type = "", bool p_recursive = true, bool p_owned = true) const;
  433. bool has_node_and_resource(const NodePath &p_path) const;
  434. Node *get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const;
  435. virtual void reparent(RequiredParam<Node> rp_parent, bool p_keep_global_transform = true);
  436. Node *get_parent() const;
  437. Node *find_parent(const String &p_pattern) const;
  438. void set_unique_scene_id(int32_t p_unique_id);
  439. int32_t get_unique_scene_id() const;
  440. Window *get_window() const;
  441. Window *get_non_popup_window() const;
  442. Window *get_last_exclusive_window() const;
  443. _FORCE_INLINE_ SceneTree *get_tree() const {
  444. ERR_FAIL_NULL_V(data.tree, nullptr);
  445. return data.tree;
  446. }
  447. _FORCE_INLINE_ bool is_inside_tree() const { return data.tree; }
  448. bool is_internal() const { return data.internal_mode != INTERNAL_MODE_DISABLED; }
  449. bool is_ancestor_of(RequiredParam<const Node> rp_node) const;
  450. bool is_greater_than(RequiredParam<const Node> rp_node) const;
  451. NodePath get_path() const;
  452. NodePath get_path_to(RequiredParam<const Node> rp_node, bool p_use_unique_path = false) const;
  453. Node *find_common_parent_with(const Node *p_node) const;
  454. void add_to_group(const StringName &p_identifier, bool p_persistent = false);
  455. void remove_from_group(const StringName &p_identifier);
  456. bool is_in_group(const StringName &p_identifier) const;
  457. struct GroupInfo {
  458. StringName name;
  459. bool persistent = false;
  460. };
  461. void get_groups(List<GroupInfo> *p_groups) const;
  462. int get_persistent_group_count() const;
  463. void move_child(RequiredParam<Node> rp_child, int p_index);
  464. void _move_child(Node *p_child, int p_index, bool p_ignore_end = false);
  465. void set_owner(Node *p_owner);
  466. Node *get_owner() const;
  467. void get_owned_by(Node *p_by, List<Node *> *p_owned);
  468. void set_unique_name_in_owner(bool p_enabled);
  469. bool is_unique_name_in_owner() const;
  470. _FORCE_INLINE_ int get_index(bool p_include_internal = true) const {
  471. // p_include_internal = false doesn't make sense if the node is internal.
  472. ERR_FAIL_COND_V_MSG(!p_include_internal && data.internal_mode != INTERNAL_MODE_DISABLED, -1, "Node is internal. Can't get index with 'include_internal' being false.");
  473. if (!data.parent) {
  474. return data.index;
  475. }
  476. data.parent->_update_children_cache();
  477. if (!p_include_internal) {
  478. return data.index;
  479. } else {
  480. switch (data.internal_mode) {
  481. case INTERNAL_MODE_DISABLED: {
  482. return data.parent->data.internal_children_front_count_cache + data.index;
  483. } break;
  484. case INTERNAL_MODE_FRONT: {
  485. return data.index;
  486. } break;
  487. case INTERNAL_MODE_BACK: {
  488. return data.parent->data.internal_children_front_count_cache + data.parent->data.external_children_count_cache + data.index;
  489. } break;
  490. }
  491. return -1;
  492. }
  493. }
  494. RequiredResult<Tween> create_tween();
  495. void print_tree();
  496. void print_tree_pretty();
  497. String get_tree_string();
  498. String get_tree_string_pretty();
  499. void set_scene_file_path(const String &p_scene_file_path);
  500. String get_scene_file_path() const;
  501. void set_editor_description(const String &p_editor_description);
  502. String get_editor_description() const;
  503. void set_editable_instance(RequiredParam<Node> rp_node, bool p_editable);
  504. bool is_editable_instance(const Node *p_node) const;
  505. Node *get_deepest_editable_node(Node *p_start_node) const;
  506. #ifdef TOOLS_ENABLED
  507. void set_property_pinned(const String &p_property, bool p_pinned);
  508. bool is_property_pinned(const StringName &p_property) const;
  509. virtual StringName get_property_store_alias(const StringName &p_property) const;
  510. bool is_part_of_edited_scene() const;
  511. #else
  512. bool is_part_of_edited_scene() const { return false; }
  513. #endif
  514. void get_storable_properties(HashSet<StringName> &r_storable_properties) const;
  515. /* NOTIFICATIONS */
  516. void propagate_notification(int p_notification);
  517. void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
  518. /* PROCESSING */
  519. void set_physics_process(bool p_process);
  520. double get_physics_process_delta_time() const;
  521. bool is_physics_processing() const;
  522. void set_process(bool p_process);
  523. double get_process_delta_time() const;
  524. bool is_processing() const;
  525. void set_physics_process_internal(bool p_process_internal);
  526. bool is_physics_processing_internal() const;
  527. void set_process_internal(bool p_process_internal);
  528. bool is_processing_internal() const;
  529. void set_process_priority(int p_priority);
  530. int get_process_priority() const;
  531. void set_process_thread_group_order(int p_order);
  532. int get_process_thread_group_order() const;
  533. void set_physics_process_priority(int p_priority);
  534. int get_physics_process_priority() const;
  535. void set_process_input(bool p_enable);
  536. bool is_processing_input() const;
  537. void set_process_shortcut_input(bool p_enable);
  538. bool is_processing_shortcut_input() const;
  539. void set_process_unhandled_input(bool p_enable);
  540. bool is_processing_unhandled_input() const;
  541. void set_process_unhandled_key_input(bool p_enable);
  542. bool is_processing_unhandled_key_input() const;
  543. _FORCE_INLINE_ bool _is_any_processing() const {
  544. return data.process || data.process_internal || data.physics_process || data.physics_process_internal;
  545. }
  546. _FORCE_INLINE_ bool is_accessible_from_caller_thread() const {
  547. if (current_process_thread_group == nullptr) {
  548. // No thread processing.
  549. // Only accessible if node is outside the scene tree
  550. // or access will happen from a node-safe thread.
  551. return !data.tree || is_current_thread_safe_for_nodes();
  552. } else {
  553. // Thread processing.
  554. return current_process_thread_group == data.process_thread_group_owner;
  555. }
  556. }
  557. _FORCE_INLINE_ bool is_readable_from_caller_thread() const {
  558. if (current_process_thread_group == nullptr) {
  559. // No thread processing.
  560. // Only accessible if node is outside the scene tree
  561. // or access will happen from a node-safe thread.
  562. return is_current_thread_safe_for_nodes() || unlikely(!data.tree);
  563. } else {
  564. // Thread processing.
  565. return true;
  566. }
  567. }
  568. _FORCE_INLINE_ static bool is_group_processing() { return current_process_thread_group; }
  569. void set_process_thread_messages(BitField<ProcessThreadMessages> p_flags);
  570. BitField<ProcessThreadMessages> get_process_thread_messages() const;
  571. void queue_accessibility_update();
  572. virtual RID get_accessibility_element() const;
  573. virtual RID get_focused_accessibility_element() const;
  574. virtual bool accessibility_override_tree_hierarchy() const { return false; }
  575. virtual PackedStringArray get_accessibility_configuration_warnings() const;
  576. Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
  577. #ifdef TOOLS_ENABLED
  578. Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const;
  579. Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, Node *p_scene_root, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resource_remap) const;
  580. void remap_node_resources(Node *p_node, Node *p_scene_root, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resource_remap) const;
  581. void remap_nested_resources(Ref<Resource> p_resource, HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
  582. #endif
  583. // used by editors, to save what has changed only
  584. void set_scene_instance_state(const Ref<SceneState> &p_state);
  585. Ref<SceneState> get_scene_instance_state() const;
  586. void set_scene_inherited_state(const Ref<SceneState> &p_state);
  587. Ref<SceneState> get_scene_inherited_state() const;
  588. void set_scene_instance_load_placeholder(bool p_enable);
  589. bool get_scene_instance_load_placeholder() const;
  590. template <typename... VarArgs>
  591. Vector<Variant> make_binds(VarArgs... p_args) {
  592. Vector<Variant> binds = { p_args... };
  593. return binds;
  594. }
  595. void replace_by(RequiredParam<Node> rp_node, bool p_keep_groups = false);
  596. void set_process_mode(ProcessMode p_mode);
  597. ProcessMode get_process_mode() const;
  598. bool can_process() const;
  599. bool can_process_notification(int p_what) const;
  600. void set_physics_interpolation_mode(PhysicsInterpolationMode p_mode);
  601. PhysicsInterpolationMode get_physics_interpolation_mode() const { return data.physics_interpolation_mode; }
  602. _FORCE_INLINE_ bool is_physics_interpolated() const { return data.physics_interpolated; }
  603. _FORCE_INLINE_ bool is_physics_interpolated_and_enabled() const { return SceneTree::is_fti_enabled() && is_physics_interpolated(); }
  604. void reset_physics_interpolation();
  605. bool is_enabled() const;
  606. bool is_ready() const;
  607. void request_ready();
  608. void set_process_thread_group(ProcessThreadGroup p_mode);
  609. ProcessThreadGroup get_process_thread_group() const;
  610. static void print_orphan_nodes();
  611. static TypedArray<int> get_orphan_node_ids();
  612. #ifdef TOOLS_ENABLED
  613. String validate_child_name(Node *p_child);
  614. String prevalidate_child_name(Node *p_child, StringName p_name);
  615. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  616. #endif
  617. static String adjust_name_casing(const String &p_name);
  618. void queue_free();
  619. //hacks for speed
  620. static void init_node_hrcr();
  621. bool is_owned_by_parent() const;
  622. void clear_internal_tree_resource_paths();
  623. _FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
  624. virtual PackedStringArray get_configuration_warnings() const;
  625. void update_configuration_warnings();
  626. void set_display_folded(bool p_folded);
  627. bool is_displayed_folded() const;
  628. /* NETWORK */
  629. virtual void set_multiplayer_authority(int p_peer_id, bool p_recursive = true);
  630. int get_multiplayer_authority() const;
  631. bool is_multiplayer_authority() const;
  632. void rpc_config(const StringName &p_method, const Variant &p_config); // config a local method for RPC
  633. const Variant get_node_rpc_config() const;
  634. template <typename... VarArgs>
  635. Error rpc(const StringName &p_method, VarArgs... p_args);
  636. template <typename... VarArgs>
  637. Error rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args);
  638. Error rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
  639. Ref<MultiplayerAPI> get_multiplayer() const;
  640. /* INTERNATIONALIZATION */
  641. void set_auto_translate_mode(AutoTranslateMode p_mode);
  642. AutoTranslateMode get_auto_translate_mode() const;
  643. bool can_auto_translate() const;
  644. virtual StringName get_translation_domain() const override;
  645. virtual void set_translation_domain(const StringName &p_domain) override;
  646. void set_translation_domain_inherited();
  647. _FORCE_INLINE_ String atr(const String &p_message, const StringName &p_context = "") const { return can_auto_translate() ? tr(p_message, p_context) : p_message; }
  648. _FORCE_INLINE_ String atr_n(const String &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const {
  649. if (can_auto_translate()) {
  650. return tr_n(p_message, p_message_plural, p_n, p_context);
  651. }
  652. return p_n == 1 ? p_message : String(p_message_plural);
  653. }
  654. /* THREADING */
  655. void call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  656. template <typename... VarArgs>
  657. void call_deferred_thread_group(const StringName &p_method, VarArgs... p_args) {
  658. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  659. const Variant *argptrs[sizeof...(p_args) + 1];
  660. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  661. argptrs[i] = &args[i];
  662. }
  663. call_deferred_thread_groupp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  664. }
  665. void set_deferred_thread_group(const StringName &p_property, const Variant &p_value);
  666. void notify_deferred_thread_group(int p_notification);
  667. void call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  668. template <typename... VarArgs>
  669. void call_thread_safe(const StringName &p_method, VarArgs... p_args) {
  670. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  671. const Variant *argptrs[sizeof...(p_args) + 1];
  672. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  673. argptrs[i] = &args[i];
  674. }
  675. call_deferred_thread_groupp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  676. }
  677. void set_thread_safe(const StringName &p_property, const Variant &p_value);
  678. void notify_thread_safe(int p_notification);
  679. /* HELPER */
  680. bool is_instance() const { return !data.scene_file_path.is_empty(); }
  681. // These inherited functions need proper multithread locking when overridden in Node.
  682. #ifdef DEBUG_ENABLED
  683. virtual void set_script(const Variant &p_script) override;
  684. virtual Variant get_script() const override;
  685. virtual bool has_meta(const StringName &p_name) const override;
  686. virtual void set_meta(const StringName &p_name, const Variant &p_value) override;
  687. virtual void remove_meta(const StringName &p_name) override;
  688. virtual Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const override;
  689. virtual void get_meta_list(List<StringName> *p_list) const override;
  690. virtual Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) override;
  691. virtual bool has_signal(const StringName &p_name) const override;
  692. virtual void get_signal_list(List<MethodInfo> *p_signals) const override;
  693. virtual void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const override;
  694. virtual void get_all_signal_connections(List<Connection> *p_connections) const override;
  695. virtual int get_persistent_signal_connection_count() const override;
  696. virtual uint32_t get_signal_connection_flags(const StringName &p_name, const Callable &p_callable) const override;
  697. virtual void get_signals_connected_to_this(List<Connection> *p_connections) const override;
  698. virtual Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0) override;
  699. virtual void disconnect(const StringName &p_signal, const Callable &p_callable) override;
  700. virtual bool is_connected(const StringName &p_signal, const Callable &p_callable) const override;
  701. virtual bool has_connections(const StringName &p_signal) const override;
  702. #endif
  703. Node();
  704. ~Node();
  705. };
  706. VARIANT_ENUM_CAST(Node::DuplicateFlags);
  707. VARIANT_ENUM_CAST(Node::ProcessMode);
  708. VARIANT_ENUM_CAST(Node::ProcessThreadGroup);
  709. VARIANT_BITFIELD_CAST(Node::ProcessThreadMessages);
  710. VARIANT_ENUM_CAST(Node::InternalMode);
  711. VARIANT_ENUM_CAST(Node::PhysicsInterpolationMode);
  712. VARIANT_ENUM_CAST(Node::AutoTranslateMode);
  713. typedef HashSet<Node *, Node::Comparator> NodeSet;
  714. // Template definitions must be in the header so they are always fully initialized before their usage.
  715. // See this StackOverflow question for more information: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
  716. template <typename... VarArgs>
  717. Error Node::rpc(const StringName &p_method, VarArgs... p_args) {
  718. return rpc_id(0, p_method, p_args...);
  719. }
  720. template <typename... VarArgs>
  721. Error Node::rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args) {
  722. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  723. const Variant *argptrs[sizeof...(p_args) + 1];
  724. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  725. argptrs[i] = &args[i];
  726. }
  727. return rpcp(p_peer_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  728. }
  729. #ifdef DEBUG_ENABLED
  730. #define ERR_THREAD_GUARD ERR_FAIL_COND_MSG(!is_accessible_from_caller_thread(), vformat("%s: The caller thread can't call the function `%s()` on this node. Use `call_deferred()` or `call_deferred_thread_group()` instead.", get_description(), FUNCTION_STR));
  731. #define ERR_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_accessible_from_caller_thread(), (m_ret), vformat("%s: The caller thread can't call the function `%s()` on this node. Use `call_deferred()` or `call_deferred_thread_group()` instead.", get_description(), FUNCTION_STR));
  732. #define ERR_MAIN_THREAD_GUARD ERR_FAIL_COND_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), vformat("%s: The function `%s()` on this node can only be accessed from the main thread. Use `call_deferred()` instead.", get_description(), FUNCTION_STR));
  733. #define ERR_MAIN_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), (m_ret), vformat("%s: The function `%s()` on this node can only be accessed from the main thread. Use `call_deferred()` instead.", get_description(), FUNCTION_STR));
  734. #define ERR_READ_THREAD_GUARD ERR_FAIL_COND_MSG(!is_readable_from_caller_thread(), vformat("%s: The function `%s()` on this node can only be accessed from either the main thread or a thread group. Use `call_deferred()` instead.", get_description(), FUNCTION_STR));
  735. #define ERR_READ_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_readable_from_caller_thread(), (m_ret), vformat("%s: The function `%s()` on this node can only be accessed from either the main thread or a thread group. Use `call_deferred()` instead.", get_description(), FUNCTION_STR));
  736. #else
  737. #define ERR_THREAD_GUARD
  738. #define ERR_THREAD_GUARD_V(m_ret)
  739. #define ERR_MAIN_THREAD_GUARD
  740. #define ERR_MAIN_THREAD_GUARD_V(m_ret)
  741. #define ERR_READ_THREAD_GUARD
  742. #define ERR_READ_THREAD_GUARD_V(m_ret)
  743. #endif
  744. // Add these macro to your class's 'get_configuration_warnings' function to have warnings show up in the scene tree inspector.
  745. #define DEPRECATED_NODE_WARNING warnings.push_back(RTR("This node is marked as deprecated and will be removed in future versions.\nPlease check the Godot documentation for information about migration."));
  746. #define EXPERIMENTAL_NODE_WARNING warnings.push_back(RTR("This node is marked as experimental and may be subject to removal or major changes in future versions."));