node.h 35 KB

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