node.h 36 KB

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