node.h 34 KB

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