node.h 29 KB

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