node.h 34 KB

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