node.h 32 KB

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