Debian Codegraph @

scene/main/node.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* node.cpp */
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
31 #include "node.h"
32 #include "node.compat.inc"
33
34 STATIC_ASSERT_INCOMPLETE_TYPE(class, Mesh);
35 STATIC_ASSERT_INCOMPLETE_TYPE(class, RenderingServer);
36 STATIC_ASSERT_INCOMPLETE_TYPE(class, DisplayServer);
37 STATIC_ASSERT_INCOMPLETE_TYPE(class, Shader);
38 STATIC_ASSERT_INCOMPLETE_TYPE(class, OS);
39 STATIC_ASSERT_INCOMPLETE_TYPE(class, Engine);
40
41 #include "core/config/project_settings.h"
42 #include "core/io/resource_loader.h"
43 #include "core/object/message_queue.h"
44 #include "core/object/script_language.h"
45 #include "core/string/print_string.h"
46 #include "instance_placeholder.h"
47 #include "scene/animation/tween.h"
48 #include "scene/debugger/scene_debugger.h"
49 #include "scene/main/multiplayer_api.h"
50 #include "scene/main/window.h"
51 #include "scene/resources/packed_scene.h"
52 #include "viewport.h"
53
54 #ifdef DEBUG_ENABLED
55 SafeNumeric<uint64_t> Node::total_node_count{ 0 };
56 #endif
57
58 thread_local Node *Node::current_process_thread_group = nullptr;
59
60 void Node::_notification(int p_notification) {
61 switch (p_notification) {
62 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
63 if (data.accessibility_element.is_valid()) {
64 DisplayServer::get_singleton()->accessibility_free_element(data.accessibility_element);
65 data.accessibility_element = RID();
66 }
67 } break;
68
69 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
70 RID ae = get_accessibility_element();
71 ERR_FAIL_COND(ae.is_null());
72
73 DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_name());
74
75 // Node children.
76 if (!accessibility_override_tree_hierarchy()) {
77 for (int i = 0; i < get_child_count(); i++) {
78 Node *child_node = get_child(i);
79 Window *child_wnd = Object::cast_to<Window>(child_node);
80 if (child_wnd && !(child_wnd->is_visible() && (child_wnd->is_embedded() || child_wnd->is_popup()))) {
81 continue;
82 }
83 if (child_node->is_part_of_edited_scene()) {
84 continue;
85 }
86 DisplayServer::get_singleton()->accessibility_update_add_child(ae, child_node->get_accessibility_element());
87 }
88 }
89 } break;
90
91 case NOTIFICATION_PROCESS: {
92 GDVIRTUAL_CALL(_process, get_process_delta_time());
93 } break;
94
95 case NOTIFICATION_PHYSICS_PROCESS: {
96 GDVIRTUAL_CALL(_physics_process, get_physics_process_delta_time());
97 } break;
98
99 case NOTIFICATION_ENTER_TREE: {
100 ERR_FAIL_NULL(get_viewport());
101 ERR_FAIL_NULL(data.tree);
102
103 if (data.tree->is_accessibility_supported() && !is_part_of_edited_scene()) {
104 data.tree->_accessibility_force_update();
105 data.tree->_accessibility_notify_change(this);
106 if (data.parent) {
107 data.tree->_accessibility_notify_change(data.parent);
108 } else {
109 data.tree->_accessibility_notify_change(get_window()); // Root node.
110 }
111 }
112
113 // Update process mode.
114 if (data.process_mode == PROCESS_MODE_INHERIT) {
115 if (data.parent) {
116 data.process_owner = data.parent->data.process_owner;
117 } else {
118 ERR_PRINT("The root node can't be set to Inherit process mode, reverting to Pausable instead.");
119 data.process_mode = PROCESS_MODE_PAUSABLE;
120 data.process_owner = this;
121 }
122 } else {
123 data.process_owner = this;
124 }
125
126 { // Update threaded process mode.
127 if (data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
128 if (data.parent) {
129 data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
130 }
131
132 if (data.process_thread_group_owner) {
133 data.process_group = data.process_thread_group_owner->data.process_group;
134 } else {
135 data.process_group = &data.tree->default_process_group;
136 }
137 } else {
138 data.process_thread_group_owner = this;
139 _add_process_group();
140 }
141
142 if (_is_any_processing()) {
143 _add_to_process_thread_group();
144 }
145 }
146
147 if (data.physics_interpolation_mode == PHYSICS_INTERPOLATION_MODE_INHERIT) {
148 bool interpolate = true; // Root node default is for interpolation to be on.
149 if (data.parent) {
150 interpolate = data.parent->is_physics_interpolated();
151 }
152 _propagate_physics_interpolated(interpolate);
153 }
154
155 // Update auto translate mode.
156 if (data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT && !data.parent) {
157 ERR_PRINT("The root node can't be set to Inherit auto translate mode, reverting to Always instead.");
158 data.auto_translate_mode = AUTO_TRANSLATE_MODE_ALWAYS;
159 }
160 data.is_auto_translate_dirty = true;
161 data.is_translation_domain_dirty = true;
162
163 if (data.input) {
164 add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
165 }
166 if (data.shortcut_input) {
167 add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
168 }
169 if (data.unhandled_input) {
170 add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
171 }
172 if (data.unhandled_key_input) {
173 add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
174 }
175
176 data.tree->nodes_in_tree_count++;
177
178 } break;
179
180 case NOTIFICATION_POST_ENTER_TREE: {
181 if (data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
182 notification(NOTIFICATION_TRANSLATION_CHANGED);
183 }
184 } break;
185
186 case NOTIFICATION_EXIT_TREE: {
187 ERR_FAIL_NULL(get_viewport());
188 ERR_FAIL_NULL(data.tree);
189
190 if (data.tree->is_accessibility_supported() && !is_part_of_edited_scene()) {
191 if (data.accessibility_element.is_valid()) {
192 DisplayServer::get_singleton()->accessibility_free_element(data.accessibility_element);
193 data.accessibility_element = RID();
194 }
195 data.tree->_accessibility_notify_change(this, true);
196 if (data.parent) {
197 data.tree->_accessibility_notify_change(data.parent);
198 } else {
199 data.tree->_accessibility_notify_change(get_window()); // Root node.
200 }
201 }
202
203 data.tree->nodes_in_tree_count--;
204
205 if (data.input) {
206 remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
207 }
208 if (data.shortcut_input) {
209 remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
210 }
211 if (data.unhandled_input) {
212 remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
213 }
214 if (data.unhandled_key_input) {
215 remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
216 }
217
218 // Remove from processing first.
219 if (_is_any_processing()) {
220 _remove_from_process_thread_group();
221 }
222 // Remove the process group.
223 if (data.process_thread_group_owner == this) {
224 _remove_process_group();
225 }
226 data.process_thread_group_owner = nullptr;
227 data.process_owner = nullptr;
228
229 if (data.path_cache) {
230 memdelete(data.path_cache);
231 data.path_cache = nullptr;
232 }
233 } break;
234
235 case NOTIFICATION_SUSPENDED:
236 case NOTIFICATION_PAUSED: {
237 if (is_physics_interpolated_and_enabled() && is_inside_tree()) {
238 reset_physics_interpolation();
239 }
240 } break;
241
242 case NOTIFICATION_PATH_RENAMED: {
243 if (data.path_cache) {
244 memdelete(data.path_cache);
245 data.path_cache = nullptr;
246 }
247 } break;
248
249 case NOTIFICATION_READY: {
250 if (GDVIRTUAL_IS_OVERRIDDEN(_input)) {
251 set_process_input(true);
252 }
253
254 if (GDVIRTUAL_IS_OVERRIDDEN(_shortcut_input)) {
255 set_process_shortcut_input(true);
256 }
257
258 if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_input)) {
259 set_process_unhandled_input(true);
260 }
261
262 if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_key_input)) {
263 set_process_unhandled_key_input(true);
264 }
265
266 if (GDVIRTUAL_IS_OVERRIDDEN(_process)) {
267 set_process(true);
268 }
269 if (GDVIRTUAL_IS_OVERRIDDEN(_physics_process)) {
270 set_physics_process(true);
271 }
272
273 GDVIRTUAL_CALL(_ready);
274 } break;
275
276 case NOTIFICATION_PREDELETE: {
277 if (data.tree && !Thread::is_main_thread()) {
278 cancel_free();
279 ERR_PRINT("Attempted to free a node that is currently added to the SceneTree from a thread. This is not permitted, use queue_free() instead. Node has not been freed.");
280 return;
281 }
282 #ifdef TOOLS_ENABLED
283 if (Engine::get_singleton()->is_editor_hint() && data.tree && this == data.tree->get_edited_scene_root()) {
284 cancel_free();
285 ERR_PRINT(vformat("Something attempted to free the root Node of a scene (\"%s\"). This is not supported inside the editor, so the Node was not freed.", get_name()));
286 return;
287 }
288 #endif
289 if (data.owner) {
290 _clean_up_owner();
291 }
292
293 while (!data.owned.is_empty()) {
294 Node *n = data.owned.back()->get();
295 n->_clean_up_owner(); // This will change data.owned. So it's impossible to loop over the list in the usual manner.
296 }
297
298 if (data.parent) {
299 data.parent->remove_child(this);
300 }
301
302 // kill children as cleanly as possible
303 while (data.children.size()) {
304 Node *child = data.children.last()->value; // begin from the end because its faster and more consistent with creation
305 memdelete(child);
306 }
307 } break;
308
309 case NOTIFICATION_TRANSLATION_CHANGED: {
310 if (data.tree) {
311 data.is_auto_translate_dirty = true;
312 }
313 } break;
314 }
315 }
316
317 void Node::_propagate_ready() {
318 data.ready_notified = true;
319 data.blocked++;
320 for (KeyValue<StringName, Node *> &K : data.children) {
321 K.value->_propagate_ready();
322 }
323
324 data.blocked--;
325
326 notification(NOTIFICATION_POST_ENTER_TREE);
327
328 if (data.ready_first) {
329 data.ready_first = false;
330 notification(NOTIFICATION_READY);
331 emit_signal(SceneStringName(ready));
332 }
333 }
334
335 void Node::_propagate_enter_tree() {
336 // this needs to happen to all children before any enter_tree
337
338 if (data.parent) {
339 data.tree = data.parent->data.tree;
340 data.depth = data.parent->data.depth + 1;
341 } else {
342 data.depth = 1;
343 }
344
345 data.viewport = Object::cast_to<Viewport>(this);
346 if (!data.viewport && data.parent) {
347 data.viewport = data.parent->data.viewport;
348 }
349
350 for (KeyValue<StringName, GroupData> &E : data.grouped) {
351 E.value.group = data.tree->add_to_group(E.key, this);
352 }
353
354 notification(NOTIFICATION_ENTER_TREE);
355
356 GDVIRTUAL_CALL(_enter_tree);
357
358 emit_signal(SceneStringName(tree_entered));
359
360 data.tree->node_added(this);
361
362 if (data.parent) {
363 Variant c = this;
364 const Variant *cptr = &c;
365 data.parent->emit_signalp(SNAME("child_entered_tree"), &cptr, 1);
366 }
367
368 data.blocked++;
369 //block while adding children
370
371 for (KeyValue<StringName, Node *> &K : data.children) {
372 if (!K.value->is_inside_tree()) { // could have been added in enter_tree
373 K.value->_propagate_enter_tree();
374 }
375 }
376
377 data.blocked--;
378
379 #ifdef DEBUG_ENABLED
380 SceneDebugger::add_to_cache(data.scene_file_path, this);
381 #endif
382 // enter groups
383 }
384
385 void Node::_propagate_after_exit_tree() {
386 // Clear owner if it was not part of the pruned branch
387 if (data.owner) {
388 if (!data.owner->is_ancestor_of(this)) {
389 _clean_up_owner();
390 }
391 }
392
393 data.blocked++;
394
395 for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
396 I->value->_propagate_after_exit_tree();
397 }
398
399 data.blocked--;
400
401 emit_signal(SceneStringName(tree_exited));
402 }
403
404 void Node::_propagate_exit_tree() {
405 //block while removing children
406
407 #ifdef DEBUG_ENABLED
408 if (!data.scene_file_path.is_empty()) {
409 // Only remove if file path is set (optimization).
410 SceneDebugger::remove_from_cache(data.scene_file_path, this);
411 }
412 #endif
413 data.blocked++;
414
415 for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
416 I->value->_propagate_exit_tree();
417 }
418
419 data.blocked--;
420
421 GDVIRTUAL_CALL(_exit_tree);
422
423 emit_signal(SceneStringName(tree_exiting));
424
425 notification(NOTIFICATION_EXIT_TREE, true);
426 if (data.tree) {
427 data.tree->node_removed(this);
428 }
429
430 if (data.parent) {
431 Variant c = this;
432 const Variant *cptr = &c;
433 data.parent->emit_signalp(SNAME("child_exiting_tree"), &cptr, 1);
434 }
435
436 // exit groups
437 for (KeyValue<StringName, GroupData> &E : data.grouped) {
438 data.tree->remove_from_group(E.key, this);
439 E.value.group = nullptr;
440 }
441
442 data.viewport = nullptr;
443
444 if (data.tree) {
445 data.tree->tree_changed();
446 }
447
448 data.ready_notified = false;
449 data.tree = nullptr;
450 data.depth = -1;
451 }
452
453 void Node::_propagate_physics_interpolated(bool p_interpolated) {
454 switch (data.physics_interpolation_mode) {
455 case PHYSICS_INTERPOLATION_MODE_INHERIT:
456 // Keep the parent p_interpolated.
457 break;
458 case PHYSICS_INTERPOLATION_MODE_OFF: {
459 p_interpolated = false;
460 } break;
461 case PHYSICS_INTERPOLATION_MODE_ON: {
462 p_interpolated = true;
463 } break;
464 }
465
466 // No change? No need to propagate further.
467 if (data.physics_interpolated == p_interpolated) {
468 return;
469 }
470
471 data.physics_interpolated = p_interpolated;
472
473 // Allow a call to the RenderingServer etc. in derived classes.
474 _physics_interpolated_changed();
475
476 update_configuration_warnings();
477
478 data.blocked++;
479 for (KeyValue<StringName, Node *> &K : data.children) {
480 K.value->_propagate_physics_interpolated(p_interpolated);
481 }
482 data.blocked--;
483 }
484
485 void Node::_propagate_physics_interpolation_reset_requested(bool p_requested) {
486 if (is_physics_interpolated()) {
487 data.physics_interpolation_reset_requested = p_requested;
488 }
489
490 data.blocked++;
491 for (KeyValue<StringName, Node *> &K : data.children) {
492 K.value->_propagate_physics_interpolation_reset_requested(p_requested);
493 }
494 data.blocked--;
495 }
496
497 void Node::move_child(RequiredParam<Node> rp_child, int p_index) {
498 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Moving child node positions inside the SceneTree is only allowed from the main thread. Use call_deferred(\"move_child\",child,index).");
499 EXTRACT_PARAM_OR_FAIL(p_child, rp_child);
500 ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
501
502 _update_children_cache();
503 // We need to check whether node is internal and move it only in the relevant node range.
504 if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
505 if (p_index < 0) {
506 p_index += data.internal_children_front_count_cache;
507 }
508 ERR_FAIL_INDEX_MSG(p_index, data.internal_children_front_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
509 _move_child(p_child, p_index);
510 } else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
511 if (p_index < 0) {
512 p_index += data.internal_children_back_count_cache;
513 }
514 ERR_FAIL_INDEX_MSG(p_index, data.internal_children_back_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
515 _move_child(p_child, (int)data.children_cache.size() - data.internal_children_back_count_cache + p_index);
516 } else {
517 if (p_index < 0) {
518 p_index += get_child_count(false);
519 }
520 ERR_FAIL_INDEX_MSG(p_index, (int)data.children_cache.size() + 1 - data.internal_children_front_count_cache - data.internal_children_back_count_cache, vformat("Invalid new child index: %d.", p_index));
521 _move_child(p_child, p_index + data.internal_children_front_count_cache);
522 }
523 }
524
525 void Node::_move_child(Node *p_child, int p_index, bool p_ignore_end) {
526 ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `move_child()` failed. Consider using `move_child.call_deferred(child, index)` instead (or `popup.call_deferred()` if this is from a popup).");
527
528 // Specifying one place beyond the end
529 // means the same as moving to the last index
530 if (!p_ignore_end) { // p_ignore_end is a little hack to make back internal children work properly.
531 if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
532 if (p_index == data.internal_children_front_count_cache) {
533 p_index--;
534 }
535 } else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
536 if (p_index == (int)data.children_cache.size()) {
537 p_index--;
538 }
539 } else {
540 if (p_index == (int)data.children_cache.size() - data.internal_children_back_count_cache) {
541 p_index--;
542 }
543 }
544 }
545
546 int child_index = p_child->get_index();
547
548 if (child_index == p_index) {
549 return; //do nothing
550 }
551
552 int motion_from = MIN(p_index, child_index);
553 int motion_to = MAX(p_index, child_index);
554
555 data.children_cache.remove_at(child_index);
556 data.children_cache.insert(p_index, p_child);
557
558 if (data.tree) {
559 data.tree->tree_changed();
560 }
561
562 data.blocked++;
563 //new pos first
564 for (int i = motion_from; i <= motion_to; i++) {
565 if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_DISABLED) {
566 data.children_cache[i]->data.index = i - data.internal_children_front_count_cache;
567 } else if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_BACK) {
568 data.children_cache[i]->data.index = i - data.internal_children_front_count_cache - data.external_children_count_cache;
569 } else {
570 data.children_cache[i]->data.index = i;
571 }
572 }
573 // notification second
574 move_child_notify(p_child);
575 notification(NOTIFICATION_CHILD_ORDER_CHANGED);
576 emit_signal(SNAME("child_order_changed"));
577 p_child->_propagate_groups_dirty();
578
579 data.blocked--;
580 }
581
582 void Node::_propagate_groups_dirty() {
583 for (const KeyValue<StringName, GroupData> &E : data.grouped) {
584 if (E.value.group) {
585 E.value.group->changed = true;
586 }
587 }
588
589 for (KeyValue<StringName, Node *> &K : data.children) {
590 K.value->_propagate_groups_dirty();
591 }
592 }
593
594 void Node::add_child_notify(Node *p_child) {
595 // to be used when not wanted
596 }
597
598 void Node::remove_child_notify(Node *p_child) {
599 // to be used when not wanted
600 }
601
602 void Node::move_child_notify(Node *p_child) {
603 // to be used when not wanted
604 }
605
606 void Node::owner_changed_notify() {
607 }
608
609 void Node::_physics_interpolated_changed() {}
610
611 void Node::set_physics_process(bool p_process) {
612 ERR_THREAD_GUARD
613 if (data.physics_process == p_process) {
614 return;
615 }
616
617 if (!is_inside_tree()) {
618 data.physics_process = p_process;
619 return;
620 }
621
622 if (_is_any_processing()) {
623 _remove_from_process_thread_group();
624 }
625
626 data.physics_process = p_process;
627
628 if (_is_any_processing()) {
629 _add_to_process_thread_group();
630 }
631 }
632
633 bool Node::is_physics_processing() const {
634 return data.physics_process;
635 }
636
637 void Node::set_physics_process_internal(bool p_process_internal) {
638 ERR_THREAD_GUARD
639 if (data.physics_process_internal == p_process_internal) {
640 return;
641 }
642
643 if (!is_inside_tree()) {
644 data.physics_process_internal = p_process_internal;
645 return;
646 }
647
648 if (_is_any_processing()) {
649 _remove_from_process_thread_group();
650 }
651
652 data.physics_process_internal = p_process_internal;
653
654 if (_is_any_processing()) {
655 _add_to_process_thread_group();
656 }
657 }
658
659 bool Node::is_physics_processing_internal() const {
660 return data.physics_process_internal;
661 }
662
663 void Node::set_process_mode(ProcessMode p_mode) {
664 ERR_THREAD_GUARD
665 if (data.process_mode == p_mode) {
666 return;
667 }
668
669 if (!is_inside_tree()) {
670 data.process_mode = p_mode;
671 return;
672 }
673
674 bool prev_can_process = can_process();
675 bool prev_enabled = _is_enabled();
676
677 if (p_mode == PROCESS_MODE_INHERIT) {
678 if (data.parent) {
679 data.process_owner = data.parent->data.process_owner;
680 } else {
681 ERR_FAIL_MSG("The root node can't be set to Inherit process mode.");
682 }
683 } else {
684 data.process_owner = this;
685 }
686
687 data.process_mode = p_mode;
688
689 bool next_can_process = can_process();
690 bool next_enabled = _is_enabled();
691
692 int pause_notification = 0;
693
694 if (prev_can_process && !next_can_process) {
695 pause_notification = NOTIFICATION_PAUSED;
696 } else if (!prev_can_process && next_can_process) {
697 pause_notification = NOTIFICATION_UNPAUSED;
698 }
699
700 int enabled_notification = 0;
701
702 if (prev_enabled && !next_enabled) {
703 enabled_notification = NOTIFICATION_DISABLED;
704 } else if (!prev_enabled && next_enabled) {
705 enabled_notification = NOTIFICATION_ENABLED;
706 }
707
708 _propagate_process_owner(data.process_owner, pause_notification, enabled_notification);
709
710 #ifdef TOOLS_ENABLED
711 // This is required for the editor to update the visibility of disabled nodes
712 // It's very expensive during runtime to change, so editor-only
713 if (Engine::get_singleton()->is_editor_hint()) {
714 data.tree->emit_signal(SNAME("tree_process_mode_changed"));
715 }
716
717 _emit_editor_state_changed();
718 #endif
719 }
720
721 void Node::_propagate_pause_notification(bool p_enable) {
722 bool prev_can_process = _can_process(!p_enable);
723 bool next_can_process = _can_process(p_enable);
724
725 if (prev_can_process && !next_can_process) {
726 notification(NOTIFICATION_PAUSED);
727 } else if (!prev_can_process && next_can_process) {
728 notification(NOTIFICATION_UNPAUSED);
729 }
730
731 data.blocked++;
732 for (KeyValue<StringName, Node *> &K : data.children) {
733 K.value->_propagate_pause_notification(p_enable);
734 }
735 data.blocked--;
736 }
737
738 void Node::_propagate_suspend_notification(bool p_enable) {
739 notification(p_enable ? NOTIFICATION_SUSPENDED : NOTIFICATION_UNSUSPENDED);
740
741 data.blocked++;
742 for (KeyValue<StringName, Node *> &KV : data.children) {
743 KV.value->_propagate_suspend_notification(p_enable);
744 }
745 data.blocked--;
746 }
747
748 Node::ProcessMode Node::get_process_mode() const {
749 return data.process_mode;
750 }
751
752 void Node::_propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification) {
753 data.process_owner = p_owner;
754
755 if (p_pause_notification != 0) {
756 notification(p_pause_notification);
757 }
758
759 if (p_enabled_notification != 0) {
760 notification(p_enabled_notification);
761 }
762
763 data.blocked++;
764 for (KeyValue<StringName, Node *> &K : data.children) {
765 Node *c = K.value;
766 if (c->data.process_mode == PROCESS_MODE_INHERIT) {
767 c->_propagate_process_owner(p_owner, p_pause_notification, p_enabled_notification);
768 }
769 }
770 data.blocked--;
771 }
772
773 void Node::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
774 ERR_THREAD_GUARD
775 data.multiplayer_authority = p_peer_id;
776
777 if (p_recursive) {
778 for (KeyValue<StringName, Node *> &K : data.children) {
779 K.value->set_multiplayer_authority(p_peer_id, true);
780 }
781 }
782 }
783
784 int Node::get_multiplayer_authority() const {
785 return data.multiplayer_authority;
786 }
787
788 bool Node::is_multiplayer_authority() const {
789 ERR_FAIL_COND_V(!is_inside_tree(), false);
790
791 Ref<MultiplayerAPI> api = get_multiplayer();
792 return api.is_valid() && (api->get_unique_id() == data.multiplayer_authority);
793 }
794
795 /***** RPC CONFIG ********/
796
797 void Node::rpc_config(const StringName &p_method, const Variant &p_config) {
798 ERR_THREAD_GUARD
799 if (data.rpc_config.get_type() != Variant::DICTIONARY) {
800 data.rpc_config = Dictionary();
801 }
802 Dictionary node_config = data.rpc_config;
803 if (p_config.get_type() == Variant::NIL) {
804 node_config.erase(p_method);
805 } else {
806 ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
807 node_config[p_method] = p_config;
808 }
809 }
810
811 const Variant Node::get_node_rpc_config() const {
812 return data.rpc_config;
813 }
814
815 /***** RPC FUNCTIONS ********/
816
817 Error Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
818 if (p_argcount < 1) {
819 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
820 r_error.expected = 1;
821 return ERR_INVALID_PARAMETER;
822 }
823
824 if (!p_args[0]->is_string()) {
825 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
826 r_error.argument = 0;
827 r_error.expected = Variant::STRING_NAME;
828 return ERR_INVALID_PARAMETER;
829 }
830
831 StringName method = (*p_args[0]).operator StringName();
832
833 Error err = rpcp(0, method, &p_args[1], p_argcount - 1);
834 r_error.error = Callable::CallError::CALL_OK;
835 return err;
836 }
837
838 Error Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
839 if (p_argcount < 2) {
840 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
841 r_error.expected = 2;
842 return ERR_INVALID_PARAMETER;
843 }
844
845 if (p_args[0]->get_type() != Variant::INT) {
846 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
847 r_error.argument = 0;
848 r_error.expected = Variant::INT;
849 return ERR_INVALID_PARAMETER;
850 }
851
852 if (!p_args[1]->is_string()) {
853 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
854 r_error.argument = 1;
855 r_error.expected = Variant::STRING_NAME;
856 return ERR_INVALID_PARAMETER;
857 }
858
859 int peer_id = *p_args[0];
860 StringName method = (*p_args[1]).operator StringName();
861
862 Error err = rpcp(peer_id, method, &p_args[2], p_argcount - 2);
863 r_error.error = Callable::CallError::CALL_OK;
864 return err;
865 }
866
867 Error Node::rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
868 ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
869
870 Ref<MultiplayerAPI> api = get_multiplayer();
871 if (api.is_null()) {
872 return ERR_UNCONFIGURED;
873 }
874 return api->rpcp(this, p_peer_id, p_method, p_arg, p_argcount);
875 }
876
877 Ref<MultiplayerAPI> Node::get_multiplayer() const {
878 if (!is_inside_tree()) {
879 return Ref<MultiplayerAPI>();
880 }
881 return data.tree->get_multiplayer(get_path());
882 }
883
884 //////////// end of rpc
885
886 bool Node::can_process_notification(int p_what) const {
887 switch (p_what) {
888 case NOTIFICATION_PHYSICS_PROCESS:
889 return data.physics_process;
890 case NOTIFICATION_PROCESS:
891 return data.process;
892 case NOTIFICATION_INTERNAL_PROCESS:
893 return data.process_internal;
894 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
895 return data.physics_process_internal;
896 }
897
898 return true;
899 }
900
901 bool Node::can_process() const {
902 ERR_FAIL_COND_V(!is_inside_tree(), false);
903 return !data.tree->is_suspended() && _can_process(data.tree->is_paused());
904 }
905
906 bool Node::_can_process(bool p_paused) const {
907 ProcessMode process_mode;
908
909 if (data.process_mode == PROCESS_MODE_INHERIT) {
910 if (!data.process_owner) {
911 process_mode = PROCESS_MODE_PAUSABLE;
912 } else {
913 process_mode = data.process_owner->data.process_mode;
914 }
915 } else {
916 process_mode = data.process_mode;
917 }
918
919 // The owner can't be set to inherit, must be a bug.
920 ERR_FAIL_COND_V(process_mode == PROCESS_MODE_INHERIT, false);
921
922 if (process_mode == PROCESS_MODE_DISABLED) {
923 return false;
924 } else if (process_mode == PROCESS_MODE_ALWAYS) {
925 return true;
926 }
927
928 if (p_paused) {
929 return process_mode == PROCESS_MODE_WHEN_PAUSED;
930 } else {
931 return process_mode == PROCESS_MODE_PAUSABLE;
932 }
933 }
934
935 void Node::set_physics_interpolation_mode(PhysicsInterpolationMode p_mode) {
936 ERR_THREAD_GUARD
937 if (data.physics_interpolation_mode == p_mode) {
938 return;
939 }
940
941 data.physics_interpolation_mode = p_mode;
942
943 bool interpolate = true; // Default for root node.
944
945 switch (p_mode) {
946 case PHYSICS_INTERPOLATION_MODE_INHERIT: {
947 if (is_inside_tree() && data.parent) {
948 interpolate = data.parent->is_physics_interpolated();
949 }
950 } break;
951 case PHYSICS_INTERPOLATION_MODE_OFF: {
952 interpolate = false;
953 } break;
954 case PHYSICS_INTERPOLATION_MODE_ON: {
955 interpolate = true;
956 } break;
957 }
958
959 _propagate_physics_interpolated(interpolate);
960
961 // Auto-reset on changing interpolation mode.
962 if (is_physics_interpolated() && is_inside_tree()) {
963 propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
964 }
965 }
966
967 void Node::reset_physics_interpolation() {
968 if (SceneTree::is_fti_enabled() && is_inside_tree()) {
969 propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
970
971 // If `reset_physics_interpolation()` is called explicitly by the user
972 // (e.g. from scripts) then we prevent deferred auto-resets taking place.
973 // The user is trusted to call reset in the right order, and auto-reset
974 // will interfere with their control of prev / curr, so should be turned off.
975 _propagate_physics_interpolation_reset_requested(false);
976 }
977 }
978
979 bool Node::_is_enabled() const {
980 ProcessMode process_mode;
981
982 if (data.process_mode == PROCESS_MODE_INHERIT) {
983 if (!data.process_owner) {
984 process_mode = PROCESS_MODE_PAUSABLE;
985 } else {
986 process_mode = data.process_owner->data.process_mode;
987 }
988 } else {
989 process_mode = data.process_mode;
990 }
991
992 return (process_mode != PROCESS_MODE_DISABLED);
993 }
994
995 bool Node::is_enabled() const {
996 ERR_FAIL_COND_V(!is_inside_tree(), false);
997 return _is_enabled();
998 }
999
1000 double Node::get_physics_process_delta_time() const {
1001 if (data.tree) {
1002 return data.tree->get_physics_process_time();
1003 } else {
1004 return 0;
1005 }
1006 }
1007
1008 double Node::get_process_delta_time() const {
1009 if (data.tree) {
1010 return data.tree->get_process_time();
1011 } else {
1012 return 0;
1013 }
1014 }
1015
1016 void Node::set_process(bool p_process) {
1017 ERR_THREAD_GUARD
1018 if (data.process == p_process) {
1019 return;
1020 }
1021
1022 if (!is_inside_tree()) {
1023 data.process = p_process;
1024 return;
1025 }
1026
1027 if (_is_any_processing()) {
1028 _remove_from_process_thread_group();
1029 }
1030
1031 data.process = p_process;
1032
1033 if (_is_any_processing()) {
1034 _add_to_process_thread_group();
1035 }
1036 }
1037
1038 bool Node::is_processing() const {
1039 return data.process;
1040 }
1041
1042 void Node::set_process_internal(bool p_process_internal) {
1043 ERR_THREAD_GUARD
1044 if (data.process_internal == p_process_internal) {
1045 return;
1046 }
1047
1048 if (!is_inside_tree()) {
1049 data.process_internal = p_process_internal;
1050 return;
1051 }
1052
1053 if (_is_any_processing()) {
1054 _remove_from_process_thread_group();
1055 }
1056
1057 data.process_internal = p_process_internal;
1058
1059 if (_is_any_processing()) {
1060 _add_to_process_thread_group();
1061 }
1062 }
1063
1064 void Node::_add_process_group() {
1065 data.tree->_add_process_group(this);
1066 }
1067
1068 void Node::_remove_process_group() {
1069 data.tree->_remove_process_group(this);
1070 }
1071
1072 void Node::_remove_from_process_thread_group() {
1073 data.tree->_remove_node_from_process_group(this, data.process_thread_group_owner);
1074 }
1075
1076 void Node::_add_to_process_thread_group() {
1077 data.tree->_add_node_to_process_group(this, data.process_thread_group_owner);
1078 }
1079
1080 void Node::_remove_tree_from_process_thread_group() {
1081 if (!is_inside_tree()) {
1082 return; // May not be initialized yet.
1083 }
1084
1085 for (KeyValue<StringName, Node *> &K : data.children) {
1086 if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1087 continue;
1088 }
1089
1090 K.value->_remove_tree_from_process_thread_group();
1091 }
1092
1093 if (_is_any_processing()) {
1094 _remove_from_process_thread_group();
1095 }
1096 }
1097
1098 void Node::_add_tree_to_process_thread_group(Node *p_owner) {
1099 if (_is_any_processing()) {
1100 _add_to_process_thread_group();
1101 }
1102
1103 data.process_thread_group_owner = p_owner;
1104 if (p_owner != nullptr) {
1105 data.process_group = p_owner->data.process_group;
1106 } else {
1107 data.process_group = &data.tree->default_process_group;
1108 }
1109
1110 for (KeyValue<StringName, Node *> &K : data.children) {
1111 if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1112 continue;
1113 }
1114
1115 K.value->_add_to_process_thread_group();
1116 }
1117 }
1118 bool Node::is_processing_internal() const {
1119 return data.process_internal;
1120 }
1121
1122 void Node::set_process_thread_group_order(int p_order) {
1123 ERR_THREAD_GUARD
1124 if (data.process_thread_group_order == p_order) {
1125 return;
1126 }
1127
1128 data.process_thread_group_order = p_order;
1129
1130 // Not yet in the tree (or not a group owner, in whose case this is pointless but harmless); trivial update.
1131 if (!is_inside_tree() || data.process_thread_group_owner != this) {
1132 return;
1133 }
1134
1135 data.tree->process_groups_dirty = true;
1136 }
1137
1138 int Node::get_process_thread_group_order() const {
1139 return data.process_thread_group_order;
1140 }
1141
1142 void Node::set_process_priority(int p_priority) {
1143 ERR_THREAD_GUARD
1144 if (data.process_priority == p_priority) {
1145 return;
1146 }
1147 if (!is_inside_tree()) {
1148 // Not yet in the tree; trivial update.
1149 data.process_priority = p_priority;
1150 return;
1151 }
1152
1153 if (_is_any_processing()) {
1154 _remove_from_process_thread_group();
1155 }
1156
1157 data.process_priority = p_priority;
1158
1159 if (_is_any_processing()) {
1160 _add_to_process_thread_group();
1161 }
1162 }
1163
1164 int Node::get_process_priority() const {
1165 return data.process_priority;
1166 }
1167
1168 void Node::set_physics_process_priority(int p_priority) {
1169 ERR_THREAD_GUARD
1170 if (data.physics_process_priority == p_priority) {
1171 return;
1172 }
1173 if (!is_inside_tree()) {
1174 // Not yet in the tree; trivial update.
1175 data.physics_process_priority = p_priority;
1176 return;
1177 }
1178
1179 if (_is_any_processing()) {
1180 _remove_from_process_thread_group();
1181 }
1182
1183 data.physics_process_priority = p_priority;
1184
1185 if (_is_any_processing()) {
1186 _add_to_process_thread_group();
1187 }
1188 }
1189
1190 int Node::get_physics_process_priority() const {
1191 return data.physics_process_priority;
1192 }
1193
1194 void Node::set_process_thread_group(ProcessThreadGroup p_mode) {
1195 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Changing the process thread group can only be done from the main thread. Use call_deferred(\"set_process_thread_group\",mode).");
1196 if (data.process_thread_group == p_mode) {
1197 return;
1198 }
1199
1200 if (!is_inside_tree()) {
1201 // Not yet in the tree; trivial update.
1202 data.process_thread_group = p_mode;
1203 return;
1204 }
1205
1206 _remove_tree_from_process_thread_group();
1207 if (data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
1208 _remove_process_group();
1209 }
1210
1211 data.process_thread_group = p_mode;
1212
1213 if (p_mode == PROCESS_THREAD_GROUP_INHERIT) {
1214 if (data.parent) {
1215 data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
1216 } else {
1217 data.process_thread_group_owner = nullptr;
1218 }
1219 } else {
1220 data.process_thread_group_owner = this;
1221 _add_process_group();
1222 }
1223
1224 _add_tree_to_process_thread_group(data.process_thread_group_owner);
1225
1226 notify_property_list_changed();
1227 }
1228
1229 Node::ProcessThreadGroup Node::get_process_thread_group() const {
1230 return data.process_thread_group;
1231 }
1232
1233 void Node::set_process_thread_messages(BitField<ProcessThreadMessages> p_flags) {
1234 ERR_THREAD_GUARD
1235 if (data.process_thread_messages == p_flags) {
1236 return;
1237 }
1238
1239 data.process_thread_messages = p_flags;
1240 }
1241
1242 BitField<Node::ProcessThreadMessages> Node::get_process_thread_messages() const {
1243 return data.process_thread_messages;
1244 }
1245
1246 void Node::set_process_input(bool p_enable) {
1247 ERR_THREAD_GUARD
1248 if (p_enable == data.input) {
1249 return;
1250 }
1251
1252 data.input = p_enable;
1253 if (!is_inside_tree()) {
1254 return;
1255 }
1256
1257 if (p_enable) {
1258 add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
1259 } else {
1260 remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
1261 }
1262 }
1263
1264 bool Node::is_processing_input() const {
1265 return data.input;
1266 }
1267
1268 void Node::set_process_shortcut_input(bool p_enable) {
1269 ERR_THREAD_GUARD
1270 if (p_enable == data.shortcut_input) {
1271 return;
1272 }
1273 data.shortcut_input = p_enable;
1274 if (!is_inside_tree()) {
1275 return;
1276 }
1277
1278 if (p_enable) {
1279 add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
1280 } else {
1281 remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
1282 }
1283 }
1284
1285 bool Node::is_processing_shortcut_input() const {
1286 return data.shortcut_input;
1287 }
1288
1289 void Node::set_process_unhandled_input(bool p_enable) {
1290 ERR_THREAD_GUARD
1291 if (p_enable == data.unhandled_input) {
1292 return;
1293 }
1294 data.unhandled_input = p_enable;
1295 if (!is_inside_tree()) {
1296 return;
1297 }
1298
1299 if (p_enable) {
1300 add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
1301 } else {
1302 remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
1303 }
1304 }
1305
1306 bool Node::is_processing_unhandled_input() const {
1307 return data.unhandled_input;
1308 }
1309
1310 void Node::set_process_unhandled_key_input(bool p_enable) {
1311 ERR_THREAD_GUARD
1312 if (p_enable == data.unhandled_key_input) {
1313 return;
1314 }
1315 data.unhandled_key_input = p_enable;
1316 if (!is_inside_tree()) {
1317 return;
1318 }
1319
1320 if (p_enable) {
1321 add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
1322 } else {
1323 remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
1324 }
1325 }
1326
1327 bool Node::is_processing_unhandled_key_input() const {
1328 return data.unhandled_key_input;
1329 }
1330
1331 void Node::set_auto_translate_mode(AutoTranslateMode p_mode) {
1332 ERR_THREAD_GUARD
1333 if (data.auto_translate_mode == p_mode) {
1334 return;
1335 }
1336
1337 if (p_mode == AUTO_TRANSLATE_MODE_INHERIT && data.tree && !data.parent) {
1338 ERR_FAIL_MSG("The root node can't be set to Inherit auto translate mode.");
1339 }
1340
1341 data.auto_translate_mode = p_mode;
1342 data.is_auto_translating = p_mode != AUTO_TRANSLATE_MODE_DISABLED;
1343 data.is_auto_translate_dirty = true;
1344
1345 propagate_notification(NOTIFICATION_TRANSLATION_CHANGED);
1346 }
1347
1348 Node::AutoTranslateMode Node::get_auto_translate_mode() const {
1349 return data.auto_translate_mode;
1350 }
1351
1352 bool Node::can_auto_translate() const {
1353 ERR_READ_THREAD_GUARD_V(false);
1354 if (!data.is_auto_translate_dirty || data.auto_translate_mode != AUTO_TRANSLATE_MODE_INHERIT) {
1355 return data.is_auto_translating;
1356 }
1357
1358 data.is_auto_translate_dirty = false;
1359
1360 Node *parent = data.parent;
1361 while (parent) {
1362 if (parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT) {
1363 parent = parent->data.parent;
1364 continue;
1365 }
1366
1367 data.is_auto_translating = parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_ALWAYS;
1368 break;
1369 }
1370
1371 return data.is_auto_translating;
1372 }
1373
1374 StringName Node::get_translation_domain() const {
1375 ERR_READ_THREAD_GUARD_V(StringName());
1376
1377 if (data.is_translation_domain_inherited && data.is_translation_domain_dirty) {
1378 const_cast<Node *>(this)->_translation_domain = data.parent ? data.parent->get_translation_domain() : StringName();
1379 data.is_translation_domain_dirty = false;
1380 }
1381 return _translation_domain;
1382 }
1383
1384 void Node::set_translation_domain(const StringName &p_domain) {
1385 ERR_THREAD_GUARD
1386
1387 if (!data.is_translation_domain_inherited && _translation_domain == p_domain) {
1388 return;
1389 }
1390
1391 _translation_domain = p_domain;
1392 data.is_translation_domain_inherited = false;
1393 data.is_translation_domain_dirty = false;
1394 _propagate_translation_domain_dirty();
1395 }
1396
1397 void Node::set_translation_domain_inherited() {
1398 ERR_THREAD_GUARD
1399
1400 if (data.is_translation_domain_inherited) {
1401 return;
1402 }
1403 data.is_translation_domain_inherited = true;
1404 data.is_translation_domain_dirty = true;
1405 _propagate_translation_domain_dirty();
1406 }
1407
1408 void Node::_propagate_translation_domain_dirty() {
1409 for (KeyValue<StringName, Node *> &K : data.children) {
1410 Node *child = K.value;
1411 if (child->data.is_translation_domain_inherited) {
1412 child->data.is_translation_domain_dirty = true;
1413 child->_propagate_translation_domain_dirty();
1414 }
1415 }
1416
1417 if (is_inside_tree() && data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
1418 notification(NOTIFICATION_TRANSLATION_CHANGED);
1419 }
1420 }
1421
1422 StringName Node::get_name() const {
1423 return data.name;
1424 }
1425
1426 void Node::_set_name_nocheck(const StringName &p_name) {
1427 data.name = p_name;
1428 }
1429
1430 void Node::set_name(const StringName &p_name) {
1431 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Changing the name to nodes inside the SceneTree is only allowed from the main thread. Use `set_name.call_deferred(new_name)`.");
1432 ERR_FAIL_COND(p_name.is_empty());
1433
1434 const StringName old_name = data.name;
1435 if (data.unique_name_in_owner && data.owner) {
1436 _release_unique_name_in_owner();
1437 }
1438
1439 {
1440 const String input_name_str = String(p_name);
1441 const String validated_node_name_string = input_name_str.validate_node_name();
1442 if (input_name_str == validated_node_name_string) {
1443 data.name = p_name;
1444 } else {
1445 data.name = StringName(validated_node_name_string);
1446 }
1447 }
1448
1449 if (data.parent) {
1450 data.parent->_validate_child_name(this, true);
1451 bool success = data.parent->data.children.replace_key(old_name, data.name);
1452 ERR_FAIL_COND_MSG(!success, "Renaming child in hashtable failed, this is a bug.");
1453 }
1454
1455 if (data.unique_name_in_owner && data.owner) {
1456 _acquire_unique_name_in_owner();
1457 }
1458
1459 propagate_notification(NOTIFICATION_PATH_RENAMED);
1460
1461 if (is_inside_tree()) {
1462 emit_signal(SNAME("renamed"));
1463 data.tree->node_renamed(this);
1464 data.tree->tree_changed();
1465 }
1466 }
1467
1468 // Returns a clear description of this node depending on what is available. Useful for error messages.
1469 String Node::get_description() const {
1470 String description;
1471 if (is_inside_tree()) {
1472 description = String(get_path());
1473 } else {
1474 description = get_name();
1475 if (description.is_empty()) {
1476 description = get_class();
1477 }
1478 }
1479 return description;
1480 }
1481
1482 static SafeRefCount node_hrcr_count;
1483
1484 void Node::init_node_hrcr() {
1485 node_hrcr_count.init(1);
1486 }
1487
1488 #ifdef TOOLS_ENABLED
1489 String Node::validate_child_name(Node *p_child) {
1490 StringName name = p_child->data.name;
1491 _generate_serial_child_name(p_child, name);
1492 return name;
1493 }
1494
1495 String Node::prevalidate_child_name(Node *p_child, StringName p_name) {
1496 _generate_serial_child_name(p_child, p_name);
1497 return p_name;
1498 }
1499 #endif
1500
1501 String Node::adjust_name_casing(const String &p_name) {
1502 switch (GLOBAL_GET("editor/naming/node_name_casing").operator int()) {
1503 case NAME_CASING_PASCAL_CASE:
1504 return p_name.to_pascal_case();
1505 case NAME_CASING_CAMEL_CASE:
1506 return p_name.to_camel_case();
1507 case NAME_CASING_SNAKE_CASE:
1508 return p_name.to_snake_case();
1509 case NAME_CASING_KEBAB_CASE:
1510 return p_name.to_kebab_case();
1511 }
1512 return p_name;
1513 }
1514
1515 void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
1516 /* Make sure the name is unique */
1517
1518 if (p_force_human_readable) {
1519 //this approach to autoset node names is human readable but very slow
1520
1521 StringName name = p_child->data.name;
1522 _generate_serial_child_name(p_child, name);
1523 p_child->data.name = name;
1524
1525 } else {
1526 //this approach to autoset node names is fast but not as readable
1527 //it's the default and reserves the '@' character for unique names.
1528
1529 bool unique = true;
1530
1531 if (p_child->data.name == StringName()) {
1532 //new unique name must be assigned
1533 unique = false;
1534 } else {
1535 const Node *const *existing = data.children.getptr(p_child->data.name);
1536 unique = !existing || *existing == p_child;
1537 }
1538
1539 if (!unique) {
1540 ERR_FAIL_COND(!node_hrcr_count.ref());
1541 // Optimized version of the code below:
1542 // String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
1543 uint32_t c = node_hrcr_count.get();
1544 String cn = p_child->get_class_name().operator String();
1545 const char32_t *cn_ptr = cn.ptr();
1546 uint32_t cn_length = cn.length();
1547 uint32_t c_chars = String::num_characters(c);
1548 uint32_t len = 2 + cn_length + c_chars;
1549 char32_t *str = (char32_t *)alloca(sizeof(char32_t) * (len + 1));
1550 uint32_t idx = 0;
1551 str[idx++] = '@';
1552 for (uint32_t i = 0; i < cn_length; i++) {
1553 str[idx++] = cn_ptr[i];
1554 }
1555 str[idx++] = '@';
1556 idx += c_chars;
1557 ERR_FAIL_COND(idx != len);
1558 str[idx] = 0;
1559 while (c) {
1560 str[--idx] = '0' + (c % 10);
1561 c /= 10;
1562 }
1563 p_child->data.name = String(str);
1564 }
1565 }
1566 }
1567
1568 // Return s + 1 as if it were an integer
1569 String increase_numeric_string(const String &s) {
1570 String res = s;
1571 bool carry = res.length() > 0;
1572
1573 for (int i = res.length() - 1; i >= 0; i--) {
1574 if (!carry) {
1575 break;
1576 }
1577 char32_t n = s[i];
1578 if (n == '9') { // keep carry as true: 9 + 1
1579 res[i] = '0';
1580 } else {
1581 res[i] = s[i] + 1;
1582 carry = false;
1583 }
1584 }
1585
1586 if (carry) {
1587 res = "1" + res;
1588 }
1589
1590 return res;
1591 }
1592
1593 void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const {
1594 if (name == StringName()) {
1595 // No name and a new name is needed, create one.
1596
1597 name = p_child->get_class();
1598 }
1599
1600 const Node *const *existing = data.children.getptr(name);
1601 if (!existing || *existing == p_child) { // Unused, or is current node.
1602 return;
1603 }
1604
1605 // Extract trailing number
1606 String name_string = name;
1607 String nums;
1608 for (int i = name_string.length() - 1; i >= 0; i--) {
1609 char32_t n = name_string[i];
1610 if (is_digit(n)) {
1611 nums = String::chr(name_string[i]) + nums;
1612 } else {
1613 break;
1614 }
1615 }
1616
1617 String nnsep = _get_name_num_separator();
1618 int name_last_index = name_string.length() - nnsep.length() - nums.length();
1619
1620 // Assign the base name + separator to name if we have numbers preceded by a separator
1621 if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) {
1622 name_string = name_string.substr(0, name_last_index + nnsep.length());
1623 } else {
1624 nums = "";
1625 }
1626
1627 for (;;) {
1628 StringName attempt = name_string + nums;
1629
1630 existing = data.children.getptr(attempt);
1631 bool exists = existing != nullptr && *existing != p_child;
1632
1633 if (!exists) {
1634 name = attempt;
1635 return;
1636 } else {
1637 if (nums.length() == 0) {
1638 // Name was undecorated so skip to 2 for a more natural result
1639 nums = "2";
1640 name_string += nnsep; // Add separator because nums.length() > 0 was false
1641 } else {
1642 nums = increase_numeric_string(nums);
1643 }
1644 }
1645 }
1646 }
1647
1648 Node::InternalMode Node::get_internal_mode() const {
1649 return data.internal_mode;
1650 }
1651
1652 void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalMode p_internal_mode) {
1653 //add a child node quickly, without name validation
1654
1655 p_child->data.name = p_name;
1656 data.children.insert(p_name, p_child);
1657
1658 p_child->data.internal_mode = p_internal_mode;
1659
1660 bool can_push_back = false;
1661 switch (p_internal_mode) {
1662 case INTERNAL_MODE_FRONT: {
1663 p_child->data.index = data.internal_children_front_count_cache++;
1664 // Safe to push back when ordinary and back children are empty.
1665 can_push_back = (data.external_children_count_cache + data.internal_children_back_count_cache) == 0;
1666 } break;
1667 case INTERNAL_MODE_BACK: {
1668 p_child->data.index = data.internal_children_back_count_cache++;
1669 // Safe to push back when cache is valid.
1670 can_push_back = true;
1671 } break;
1672 case INTERNAL_MODE_DISABLED: {
1673 p_child->data.index = data.external_children_count_cache++;
1674 // Safe to push back when back children are empty.
1675 can_push_back = data.internal_children_back_count_cache == 0;
1676 } break;
1677 }
1678
1679 p_child->data.parent = this;
1680
1681 if (!data.children_cache_dirty && can_push_back) {
1682 data.children_cache.push_back(p_child);
1683 } else {
1684 data.children_cache_dirty = true;
1685 }
1686
1687 p_child->notification(NOTIFICATION_PARENTED);
1688
1689 if (data.tree) {
1690 p_child->_set_tree(data.tree);
1691 }
1692
1693 /* Notify */
1694 add_child_notify(p_child);
1695 notification(NOTIFICATION_CHILD_ORDER_CHANGED);
1696 emit_signal(SNAME("child_order_changed"));
1697 }
1698
1699 void Node::add_child(RequiredParam<Node> rp_child, bool p_force_readable_name, InternalMode p_internal) {
1700 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Adding children to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_child\",node).");
1701
1702 ERR_THREAD_GUARD
1703 EXTRACT_PARAM_OR_FAIL(p_child, rp_child);
1704 ERR_FAIL_COND_MSG(p_child == this, vformat("Can't add child '%s' to itself.", p_child->get_name())); // adding to itself!
1705 ERR_FAIL_COND_MSG(p_child->data.parent, vformat("Can't add child '%s' to '%s', already has a parent '%s'.", p_child->get_name(), get_name(), p_child->data.parent->get_name())); //Fail if node has a parent
1706 #ifdef DEBUG_ENABLED
1707 ERR_FAIL_COND_MSG(p_child->is_ancestor_of(this), vformat("Can't add child '%s' to '%s' as it would result in a cyclic dependency since '%s' is already a parent of '%s'.", p_child->get_name(), get_name(), p_child->get_name(), get_name()));
1708 #endif
1709 ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.");
1710
1711 _validate_child_name(p_child, p_force_readable_name);
1712
1713 #ifdef DEBUG_ENABLED
1714 if (p_child->data.owner && !p_child->data.owner->is_ancestor_of(p_child)) {
1715 // Owner of p_child should be ancestor of p_child.
1716 WARN_PRINT(vformat("Adding '%s' as child to '%s' will make owner '%s' inconsistent. Consider unsetting the owner beforehand.", p_child->get_name(), get_name(), p_child->data.owner->get_name()));
1717 }
1718 #endif // DEBUG_ENABLED
1719
1720 _add_child_nocheck(p_child, p_child->data.name, p_internal);
1721 }
1722
1723 void Node::add_sibling(RequiredParam<Node> rp_sibling, bool p_force_readable_name) {
1724 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Adding a sibling to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_sibling\",node).");
1725 EXTRACT_PARAM_OR_FAIL(p_sibling, rp_sibling);
1726 ERR_FAIL_COND_MSG(p_sibling == this, vformat("Can't add sibling '%s' to itself.", p_sibling->get_name())); // adding to itself!
1727 ERR_FAIL_NULL(data.parent);
1728 ERR_FAIL_COND_MSG(data.parent->data.blocked > 0, "Parent node is busy setting up children, `add_sibling()` failed. Consider using `add_sibling.call_deferred(sibling)` instead.");
1729
1730 data.parent->add_child(p_sibling, p_force_readable_name, data.internal_mode);
1731 data.parent->_update_children_cache();
1732 data.parent->_move_child(p_sibling, get_index() + 1);
1733 }
1734
1735 void Node::remove_child(RequiredParam<Node> rp_child) {
1736 ERR_FAIL_COND_MSG(data.tree && !Thread::is_main_thread(), "Removing children from a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"remove_child\",node).");
1737 EXTRACT_PARAM_OR_FAIL(p_child, rp_child);
1738 ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy adding/removing children, `remove_child()` can't be called at this time. Consider using `remove_child.call_deferred(child)` instead.");
1739 ERR_FAIL_COND(p_child->data.parent != this);
1740
1741 /**
1742 * Do not change the data.internal_children*cache counters here.
1743 * Because if nodes are re-added, the indices can remain
1744 * greater-than-everything indices and children added remain
1745 * properly ordered.
1746 *
1747 * All children indices and counters will be updated next time the
1748 * cache is re-generated.
1749 */
1750
1751 data.blocked++;
1752 p_child->_set_tree(nullptr);
1753
1754 remove_child_notify(p_child);
1755 p_child->notification(NOTIFICATION_UNPARENTED);
1756
1757 data.blocked--;
1758
1759 data.children_cache_dirty = true;
1760 bool success = data.children.erase(p_child->data.name);
1761 ERR_FAIL_COND_MSG(!success, "Children name does not match parent name in hashtable, this is a bug.");
1762
1763 p_child->data.parent = nullptr;
1764 p_child->data.index = -1;
1765
1766 notification(NOTIFICATION_CHILD_ORDER_CHANGED);
1767 emit_signal(SNAME("child_order_changed"));
1768
1769 if (data.tree) {
1770 p_child->_propagate_after_exit_tree();
1771 }
1772 }
1773
1774 void Node::_update_children_cache_impl() const {
1775 // Assign children
1776 data.children_cache.resize(data.children.size());
1777 int idx = 0;
1778 for (const KeyValue<StringName, Node *> &K : data.children) {
1779 data.children_cache[idx] = K.value;
1780 idx++;
1781 }
1782 // Sort them
1783 data.children_cache.sort_custom<ComparatorByIndex>();
1784 // Update indices
1785 data.external_children_count_cache = 0;
1786 data.internal_children_back_count_cache = 0;
1787 data.internal_children_front_count_cache = 0;
1788
1789 for (uint32_t i = 0; i < data.children_cache.size(); i++) {
1790 switch (data.children_cache[i]->data.internal_mode) {
1791 case INTERNAL_MODE_DISABLED: {
1792 data.children_cache[i]->data.index = data.external_children_count_cache++;
1793 } break;
1794 case INTERNAL_MODE_FRONT: {
1795 data.children_cache[i]->data.index = data.internal_children_front_count_cache++;
1796 } break;
1797 case INTERNAL_MODE_BACK: {
1798 data.children_cache[i]->data.index = data.internal_children_back_count_cache++;
1799 } break;
1800 }
1801 }
1802 data.children_cache_dirty = false;
1803 }
1804
1805 template <bool p_include_internal>
1806 Iterable<Node::ChildrenIterator> Node::iterate_children() const {
1807 // The thread guard is omitted for performance reasons.
1808 // ERR_THREAD_GUARD_V(Iterable<ChildrenIterator>(nullptr, nullptr));
1809
1810 _update_children_cache();
1811 const uint32_t size = data.children_cache.size();
1812 // Might be null, but then size and internal counts are also 0.
1813 Node **ptr = data.children_cache.ptr();
1814
1815 if constexpr (p_include_internal) {
1816 return Iterable(ChildrenIterator(ptr), ChildrenIterator(ptr + size));
1817 } else {
1818 return Iterable(ChildrenIterator(ptr + data.internal_children_front_count_cache), ChildrenIterator(ptr + size - data.internal_children_back_count_cache));
1819 }
1820 }
1821
1822 template Iterable<Node::ChildrenIterator> Node::iterate_children<true>() const;
1823 template Iterable<Node::ChildrenIterator> Node::iterate_children<false>() const;
1824
1825 int Node::get_child_count(bool p_include_internal) const {
1826 ERR_THREAD_GUARD_V(0);
1827 if (p_include_internal) {
1828 return data.children.size();
1829 }
1830
1831 _update_children_cache();
1832 return data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
1833 }
1834
1835 Node *Node::get_child(int p_index, bool p_include_internal) const {
1836 ERR_THREAD_GUARD_V(nullptr);
1837 _update_children_cache();
1838
1839 if (p_include_internal) {
1840 if (p_index < 0) {
1841 p_index += data.children_cache.size();
1842 }
1843 ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size(), nullptr);
1844 return data.children_cache[p_index];
1845 } else {
1846 if (p_index < 0) {
1847 p_index += (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
1848 }
1849 ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache, nullptr);
1850 p_index += data.internal_children_front_count_cache;
1851 return data.children_cache[p_index];
1852 }
1853 }
1854
1855 TypedArray<Node> Node::get_children(bool p_include_internal) const {
1856 ERR_THREAD_GUARD_V(TypedArray<Node>());
1857 _update_children_cache();
1858
1859 TypedArray<Node> children;
1860
1861 if (p_include_internal) {
1862 children.resize(data.children_cache.size());
1863
1864 Array::Iterator itr = children.begin();
1865 for (const Node *child : data.children_cache) {
1866 *itr = child;
1867 ++itr;
1868 }
1869 } else {
1870 const int size = data.children_cache.size() - data.internal_children_back_count_cache;
1871 children.resize(size - data.internal_children_front_count_cache);
1872
1873 Array::Iterator itr = children.begin();
1874 for (int i = data.internal_children_front_count_cache; i < size; i++) {
1875 *itr = data.children_cache[i];
1876 ++itr;
1877 }
1878 }
1879
1880 return children;
1881 }
1882
1883 Node *Node::_get_child_by_name(const StringName &p_name) const {
1884 const Node *const *node = data.children.getptr(p_name);
1885 if (node) {
1886 return const_cast<Node *>(*node);
1887 } else {
1888 return nullptr;
1889 }
1890 }
1891
1892 Node *Node::get_node_or_null(const NodePath &p_path) const {
1893 ERR_THREAD_GUARD_V(nullptr);
1894 if (p_path.is_empty()) {
1895 return nullptr;
1896 }
1897
1898 ERR_FAIL_COND_V_MSG(!data.tree && p_path.is_absolute(), nullptr, "Can't use get_node() with absolute paths from outside the active scene tree.");
1899
1900 Node *current = nullptr;
1901 Node *root = nullptr;
1902
1903 if (!p_path.is_absolute()) {
1904 current = const_cast<Node *>(this); //start from this
1905 } else {
1906 root = const_cast<Node *>(this);
1907 while (root->data.parent) {
1908 root = root->data.parent; //start from root
1909 }
1910 }
1911
1912 for (int i = 0; i < p_path.get_name_count(); i++) {
1913 StringName name = p_path.get_name(i);
1914 Node *next = nullptr;
1915
1916 if (name == SNAME(".")) {
1917 next = current;
1918
1919 } else if (name == SNAME("..")) {
1920 if (current == nullptr || !current->data.parent) {
1921 return nullptr;
1922 }
1923
1924 next = current->data.parent;
1925 } else if (current == nullptr) {
1926 if (name == root->get_name()) {
1927 next = root;
1928 }
1929
1930 } else if (name.is_node_unique_name()) {
1931 Node **unique = current->data.owned_unique_nodes.getptr(name);
1932 if (!unique && current->data.owner) {
1933 unique = current->data.owner->data.owned_unique_nodes.getptr(name);
1934 }
1935 if (!unique) {
1936 return nullptr;
1937 }
1938 next = *unique;
1939 } else {
1940 next = nullptr;
1941 const Node *const *node = current->data.children.getptr(name);
1942 if (node) {
1943 next = const_cast<Node *>(*node);
1944 } else {
1945 return nullptr;
1946 }
1947 }
1948 current = next;
1949 }
1950
1951 return current;
1952 }
1953
1954 Node *Node::get_node(const NodePath &p_path) const {
1955 Node *node = get_node_or_null(p_path);
1956
1957 if (unlikely(!node)) {
1958 const String desc = get_description();
1959 if (p_path.is_absolute()) {
1960 ERR_FAIL_V_MSG(nullptr,
1961 vformat(R"(Node not found: "%s" (absolute path attempted from "%s").)", p_path, desc));
1962 } else {
1963 ERR_FAIL_V_MSG(nullptr,
1964 vformat(R"(Node not found: "%s" (relative to "%s").)", p_path, desc));
1965 }
1966 }
1967
1968 return node;
1969 }
1970
1971 bool Node::has_node(const NodePath &p_path) const {
1972 return get_node_or_null(p_path) != nullptr;
1973 }
1974
1975 // Finds the first child node (in tree order) whose name matches the given pattern.
1976 // Can be recursive or not, and limited to owned nodes.
1977 Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned) const {
1978 ERR_THREAD_GUARD_V(nullptr);
1979 ERR_FAIL_COND_V(p_pattern.is_empty(), nullptr);
1980 _update_children_cache();
1981 Node *const *cptr = data.children_cache.ptr();
1982 int ccount = data.children_cache.size();
1983 for (int i = 0; i < ccount; i++) {
1984 if (p_owned && !cptr[i]->data.owner) {
1985 continue;
1986 }
1987 if (cptr[i]->data.name.operator String().match(p_pattern)) {
1988 return cptr[i];
1989 }
1990
1991 if (!p_recursive) {
1992 continue;
1993 }
1994
1995 Node *ret = cptr[i]->find_child(p_pattern, true, p_owned);
1996 if (ret) {
1997 return ret;
1998 }
1999 }
2000 return nullptr;
2001 }
2002
2003 // Finds child nodes based on their name using pattern matching, or class name,
2004 // or both (either pattern or type can be left empty).
2005 // Can be recursive or not, and limited to owned nodes.
2006 TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_type, bool p_recursive, bool p_owned) const {
2007 ERR_THREAD_GUARD_V(TypedArray<Node>());
2008 TypedArray<Node> ret;
2009 ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), ret);
2010 _update_children_cache();
2011 Node *const *cptr = data.children_cache.ptr();
2012 int ccount = data.children_cache.size();
2013 for (int i = 0; i < ccount; i++) {
2014 if (p_owned && !cptr[i]->data.owner) {
2015 continue;
2016 }
2017
2018 if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
2019 if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
2020 ret.append(cptr[i]);
2021 } else if (cptr[i]->get_script_instance()) {
2022 Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
2023 while (scr.is_valid()) {
2024 if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
2025 ret.append(cptr[i]);
2026 break;
2027 }
2028
2029 scr = scr->get_base_script();
2030 }
2031 }
2032 }
2033
2034 if (p_recursive) {
2035 ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));
2036 }
2037 }
2038
2039 return ret;
2040 }
2041
2042 void Node::reparent(RequiredParam<Node> rp_parent, bool p_keep_global_transform) {
2043 ERR_THREAD_GUARD
2044 EXTRACT_PARAM_OR_FAIL(p_parent, rp_parent);
2045 ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented.");
2046 ERR_FAIL_COND_MSG(p_parent == this, vformat("Can't reparent '%s' to itself.", p_parent->get_name()));
2047
2048 if (p_parent == data.parent) {
2049 return;
2050 }
2051
2052 bool preserve_owner = data.owner && (data.owner == p_parent || data.owner->is_ancestor_of(p_parent));
2053 Node *owner_temp = data.owner;
2054 LocalVector<Node *> common_parents;
2055
2056 // If the new parent is related to the owner, find all children of the reparented node who have the same owner so that we can reassign them.
2057 if (preserve_owner) {
2058 LocalVector<Node *> to_visit;
2059
2060 to_visit.push_back(this);
2061 common_parents.push_back(this);
2062
2063 while (to_visit.size() > 0) {
2064 Node *check = to_visit[to_visit.size() - 1];
2065 to_visit.resize(to_visit.size() - 1);
2066
2067 for (int i = 0; i < check->get_child_count(false); i++) {
2068 Node *child = check->get_child(i, false);
2069 to_visit.push_back(child);
2070 if (child->data.owner == owner_temp) {
2071 common_parents.push_back(child);
2072 }
2073 }
2074 }
2075 }
2076
2077 data.parent->remove_child(this);
2078 p_parent->add_child(this);
2079
2080 // Reassign the old owner to those found nodes.
2081 if (preserve_owner) {
2082 for (Node *E : common_parents) {
2083 E->set_owner(owner_temp);
2084 }
2085 }
2086 }
2087
2088 Node *Node::get_parent() const {
2089 return data.parent;
2090 }
2091
2092 Node *Node::find_parent(const String &p_pattern) const {
2093 ERR_THREAD_GUARD_V(nullptr);
2094 Node *p = data.parent;
2095 while (p) {
2096 if (p->data.name.operator String().match(p_pattern)) {
2097 return p;
2098 }
2099 p = p->data.parent;
2100 }
2101
2102 return nullptr;
2103 }
2104
2105 void Node::set_unique_scene_id(int32_t p_unique_id) {
2106 data.unique_scene_id = p_unique_id;
2107 }
2108
2109 int32_t Node::get_unique_scene_id() const {
2110 return data.unique_scene_id;
2111 }
2112
2113 Window *Node::get_window() const {
2114 ERR_THREAD_GUARD_V(nullptr);
2115 Viewport *vp = get_viewport();
2116 if (vp) {
2117 return vp->get_base_window();
2118 }
2119 return nullptr;
2120 }
2121
2122 Window *Node::get_non_popup_window() const {
2123 Window *w = get_window();
2124 while (w && w->is_popup()) {
2125 w = w->get_parent_visible_window();
2126 }
2127 return w;
2128 }
2129
2130 Window *Node::get_last_exclusive_window() const {
2131 Window *w = get_window();
2132 while (w && w->get_exclusive_child()) {
2133 w = w->get_exclusive_child();
2134 }
2135
2136 return w;
2137 }
2138
2139 bool Node::is_ancestor_of(RequiredParam<const Node> rp_node) const {
2140 EXTRACT_PARAM_OR_FAIL_V(p_node, rp_node, false);
2141 Node *p = p_node->data.parent;
2142 while (p) {
2143 if (p == this) {
2144 return true;
2145 }
2146 p = p->data.parent;
2147 }
2148
2149 return false;
2150 }
2151
2152 bool Node::is_greater_than(RequiredParam<const Node> rp_node) const {
2153 // parent->get_child(1) > parent->get_child(0) > parent
2154
2155 EXTRACT_PARAM_OR_FAIL_V(p_node, rp_node, false);
2156 ERR_FAIL_COND_V(!data.tree, false);
2157 ERR_FAIL_COND_V(p_node->data.tree != data.tree, false);
2158
2159 ERR_FAIL_COND_V(data.depth < 0, false);
2160 ERR_FAIL_COND_V(p_node->data.depth < 0, false);
2161
2162 _update_children_cache();
2163
2164 bool this_is_deeper = this->data.depth > p_node->data.depth;
2165
2166 const Node *deep = this;
2167 const Node *shallow = p_node;
2168 if (!this_is_deeper) {
2169 deep = p_node;
2170 shallow = this;
2171 }
2172
2173 while (deep->data.depth > shallow->data.depth) {
2174 deep = deep->data.parent;
2175 }
2176
2177 if (deep == shallow) { // Shallow is ancestor of deep.
2178 return this_is_deeper;
2179 }
2180
2181 while (deep->data.parent != shallow->data.parent) {
2182 deep = deep->data.parent;
2183 shallow = shallow->data.parent;
2184 }
2185
2186 return (deep->get_index() > shallow->get_index()) == this_is_deeper;
2187 }
2188
2189 void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
2190 if (data.owner == p_by) {
2191 p_owned->push_back(this);
2192 }
2193
2194 for (KeyValue<StringName, Node *> &K : data.children) {
2195 K.value->get_owned_by(p_by, p_owned);
2196 }
2197 }
2198
2199 void Node::_set_owner_nocheck(Node *p_owner) {
2200 if (data.owner == p_owner) {
2201 return;
2202 }
2203
2204 ERR_FAIL_COND(data.owner);
2205 data.owner = p_owner;
2206 data.owner->data.owned.push_back(this);
2207 data.OW = data.owner->data.owned.back();
2208
2209 owner_changed_notify();
2210 }
2211
2212 void Node::_release_unique_name_in_owner() {
2213 ERR_FAIL_NULL(data.owner); // Safety check.
2214 StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
2215 Node **which = data.owner->data.owned_unique_nodes.getptr(key);
2216 if (which == nullptr || *which != this) {
2217 return; // Ignore.
2218 }
2219 data.owner->data.owned_unique_nodes.erase(key);
2220 }
2221
2222 void Node::_acquire_unique_name_in_owner() {
2223 ERR_FAIL_NULL(data.owner); // Safety check.
2224 StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
2225 Node **which = data.owner->data.owned_unique_nodes.getptr(key);
2226 if (which != nullptr && *which != this) {
2227 String which_path = String(is_inside_tree() ? (*which)->get_path() : data.owner->get_path_to(*which));
2228 WARN_PRINT(vformat("Setting node name '%s' to be unique within scene for '%s', but it's already claimed by '%s'.\n'%s' is no longer set as having a unique name.",
2229 get_name(), is_inside_tree() ? get_path() : data.owner->get_path_to(this), which_path, which_path));
2230 data.unique_name_in_owner = false;
2231 return;
2232 }
2233 data.owner->data.owned_unique_nodes[key] = this;
2234 }
2235
2236 void Node::set_unique_name_in_owner(bool p_enabled) {
2237 ERR_MAIN_THREAD_GUARD
2238 if (data.unique_name_in_owner == p_enabled) {
2239 return;
2240 }
2241
2242 if (data.unique_name_in_owner && data.owner != nullptr) {
2243 _release_unique_name_in_owner();
2244 }
2245 data.unique_name_in_owner = p_enabled;
2246
2247 if (data.unique_name_in_owner && data.owner != nullptr) {
2248 _acquire_unique_name_in_owner();
2249 }
2250
2251 update_configuration_warnings();
2252 _emit_editor_state_changed();
2253 }
2254
2255 bool Node::is_unique_name_in_owner() const {
2256 return data.unique_name_in_owner;
2257 }
2258
2259 void Node::set_owner(Node *p_owner) {
2260 ERR_MAIN_THREAD_GUARD
2261 if (data.owner) {
2262 _clean_up_owner();
2263 }
2264
2265 ERR_FAIL_COND(p_owner == this);
2266
2267 if (!p_owner) {
2268 return;
2269 }
2270
2271 bool owner_valid = p_owner->is_ancestor_of(this);
2272
2273 ERR_FAIL_COND_MSG(!owner_valid, "Invalid owner. Owner must be an ancestor in the tree.");
2274
2275 _set_owner_nocheck(p_owner);
2276
2277 if (data.unique_name_in_owner) {
2278 _acquire_unique_name_in_owner();
2279 }
2280
2281 _emit_editor_state_changed();
2282 }
2283
2284 Node *Node::get_owner() const {
2285 return data.owner;
2286 }
2287
2288 void Node::_clean_up_owner() {
2289 ERR_FAIL_NULL(data.owner); // Safety check.
2290
2291 if (data.unique_name_in_owner) {
2292 _release_unique_name_in_owner();
2293 }
2294 data.owner->data.owned.erase(data.OW);
2295 data.owner = nullptr;
2296 data.OW = nullptr;
2297 }
2298
2299 Node *Node::find_common_parent_with(const Node *p_node) const {
2300 if (this == p_node) {
2301 return const_cast<Node *>(p_node);
2302 }
2303
2304 HashSet<const Node *> visited;
2305
2306 const Node *n = this;
2307
2308 while (n) {
2309 visited.insert(n);
2310 n = n->data.parent;
2311 }
2312
2313 const Node *common_parent = p_node;
2314
2315 while (common_parent) {
2316 if (visited.has(common_parent)) {
2317 break;
2318 }
2319 common_parent = common_parent->data.parent;
2320 }
2321
2322 if (!common_parent) {
2323 return nullptr;
2324 }
2325
2326 return const_cast<Node *>(common_parent);
2327 }
2328
2329 NodePath Node::get_path_to(RequiredParam<const Node> rp_node, bool p_use_unique_path) const {
2330 EXTRACT_PARAM_OR_FAIL_V(p_node, rp_node, NodePath());
2331
2332 if (this == p_node) {
2333 return NodePath(".");
2334 }
2335
2336 HashSet<const Node *> visited;
2337
2338 const Node *n = this;
2339
2340 while (n) {
2341 visited.insert(n);
2342 n = n->data.parent;
2343 }
2344
2345 const Node *common_parent = p_node;
2346
2347 while (common_parent) {
2348 if (visited.has(common_parent)) {
2349 break;
2350 }
2351 common_parent = common_parent->data.parent;
2352 }
2353
2354 ERR_FAIL_NULL_V(common_parent, NodePath()); //nodes not in the same tree
2355
2356 visited.clear();
2357
2358 Vector<StringName> path;
2359 StringName up = String("..");
2360
2361 if (p_use_unique_path) {
2362 n = p_node;
2363
2364 bool is_detected = false;
2365 while (n != common_parent) {
2366 if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
2367 path.push_back(UNIQUE_NODE_PREFIX + String(n->get_name()));
2368 is_detected = true;
2369 break;
2370 }
2371 path.push_back(n->get_name());
2372 n = n->data.parent;
2373 }
2374
2375 if (!is_detected) {
2376 n = this;
2377
2378 String detected_name;
2379 int up_count = 0;
2380 while (n != common_parent) {
2381 if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
2382 detected_name = n->get_name();
2383 up_count = 0;
2384 }
2385 up_count++;
2386 n = n->data.parent;
2387 }
2388
2389 for (int i = 0; i < up_count; i++) {
2390 path.push_back(up);
2391 }
2392
2393 if (!detected_name.is_empty()) {
2394 path.push_back(UNIQUE_NODE_PREFIX + detected_name);
2395 }
2396 }
2397 } else {
2398 n = p_node;
2399
2400 while (n != common_parent) {
2401 path.push_back(n->get_name());
2402 n = n->data.parent;
2403 }
2404
2405 n = this;
2406
2407 while (n != common_parent) {
2408 path.push_back(up);
2409 n = n->data.parent;
2410 }
2411 }
2412
2413 path.reverse();
2414
2415 return NodePath(path, false);
2416 }
2417
2418 NodePath Node::get_path() const {
2419 ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
2420
2421 if (data.path_cache) {
2422 return *data.path_cache;
2423 }
2424
2425 const Node *n = this;
2426
2427 Vector<StringName> path;
2428 path.resize(data.depth);
2429
2430 StringName *ptrw = path.ptrw();
2431 while (n) {
2432 ptrw[n->data.depth - 1] = n->get_name();
2433 n = n->data.parent;
2434 }
2435
2436 data.path_cache = memnew(NodePath(path, true));
2437
2438 return *data.path_cache;
2439 }
2440
2441 bool Node::is_in_group(const StringName &p_identifier) const {
2442 ERR_THREAD_GUARD_V(false);
2443 return data.grouped.has(p_identifier);
2444 }
2445
2446 void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
2447 ERR_THREAD_GUARD
2448 ERR_FAIL_COND_MSG(p_identifier.is_empty(), vformat("Cannot add node '%s' to a group with an empty name.", get_name()));
2449
2450 if (data.grouped.has(p_identifier)) {
2451 return;
2452 }
2453
2454 GroupData gd;
2455
2456 if (data.tree) {
2457 gd.group = data.tree->add_to_group(p_identifier, this);
2458 } else {
2459 gd.group = nullptr;
2460 }
2461
2462 gd.persistent = p_persistent;
2463
2464 data.grouped[p_identifier] = gd;
2465 if (p_persistent) {
2466 _emit_editor_state_changed();
2467 }
2468 }
2469
2470 void Node::remove_from_group(const StringName &p_identifier) {
2471 ERR_THREAD_GUARD
2472 HashMap<StringName, GroupData>::Iterator E = data.grouped.find(p_identifier);
2473
2474 if (!E) {
2475 return;
2476 }
2477
2478 #ifdef TOOLS_ENABLED
2479 bool persistent = E->value.persistent;
2480 #endif
2481
2482 if (data.tree) {
2483 data.tree->remove_from_group(E->key, this);
2484 }
2485
2486 data.grouped.remove(E);
2487
2488 #ifdef TOOLS_ENABLED
2489 if (persistent) {
2490 _emit_editor_state_changed();
2491 }
2492 #endif
2493 }
2494
2495 TypedArray<StringName> Node::_get_groups() const {
2496 TypedArray<StringName> groups;
2497 List<GroupInfo> gi;
2498 get_groups(&gi);
2499 for (const GroupInfo &E : gi) {
2500 groups.push_back(E.name);
2501 }
2502
2503 return groups;
2504 }
2505
2506 void Node::get_groups(List<GroupInfo> *p_groups) const {
2507 ERR_THREAD_GUARD
2508 for (const KeyValue<StringName, GroupData> &E : data.grouped) {
2509 GroupInfo gi;
2510 gi.name = E.key;
2511 gi.persistent = E.value.persistent;
2512 p_groups->push_back(gi);
2513 }
2514 }
2515
2516 int Node::get_persistent_group_count() const {
2517 ERR_THREAD_GUARD_V(0);
2518 int count = 0;
2519
2520 for (const KeyValue<StringName, GroupData> &E : data.grouped) {
2521 if (E.value.persistent) {
2522 count += 1;
2523 }
2524 }
2525
2526 return count;
2527 }
2528
2529 void Node::print_tree_pretty() {
2530 print_line(_get_tree_string_pretty("", true));
2531 }
2532
2533 void Node::print_tree() {
2534 print_line(_get_tree_string(this));
2535 }
2536
2537 String Node::_get_tree_string_pretty(const String &p_prefix, bool p_last) {
2538 String new_prefix = p_last ? String::utf8(" â”–â•´") : String::utf8(" â” â•´");
2539 _update_children_cache();
2540 String return_tree = p_prefix + new_prefix + String(get_name()) + "\n";
2541 for (uint32_t i = 0; i < data.children_cache.size(); i++) {
2542 new_prefix = p_last ? String::utf8(" ") : String::utf8(" ┃ ");
2543 return_tree += data.children_cache[i]->_get_tree_string_pretty(p_prefix + new_prefix, i == data.children_cache.size() - 1);
2544 }
2545 return return_tree;
2546 }
2547
2548 String Node::get_tree_string_pretty() {
2549 return _get_tree_string_pretty("", true);
2550 }
2551
2552 String Node::_get_tree_string(const Node *p_node) {
2553 _update_children_cache();
2554 String return_tree = String(p_node->get_path_to(this)) + "\n";
2555 for (uint32_t i = 0; i < data.children_cache.size(); i++) {
2556 return_tree += data.children_cache[i]->_get_tree_string(p_node);
2557 }
2558 return return_tree;
2559 }
2560
2561 String Node::get_tree_string() {
2562 return _get_tree_string(this);
2563 }
2564
2565 void Node::_propagate_reverse_notification(int p_notification) {
2566 data.blocked++;
2567
2568 for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
2569 I->value->_propagate_reverse_notification(p_notification);
2570 }
2571
2572 notification(p_notification, true);
2573 data.blocked--;
2574 }
2575
2576 void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
2577 ERR_FAIL_COND(!is_inside_tree());
2578
2579 data.blocked++;
2580
2581 if (!p_reverse) {
2582 MessageQueue::get_singleton()->push_notification(this, p_notification);
2583 }
2584
2585 for (KeyValue<StringName, Node *> &K : data.children) {
2586 K.value->_propagate_deferred_notification(p_notification, p_reverse);
2587 }
2588
2589 if (p_reverse) {
2590 MessageQueue::get_singleton()->push_notification(this, p_notification);
2591 }
2592
2593 data.blocked--;
2594 }
2595
2596 void Node::propagate_notification(int p_notification) {
2597 ERR_THREAD_GUARD
2598 data.blocked++;
2599 notification(p_notification);
2600
2601 for (KeyValue<StringName, Node *> &K : data.children) {
2602 K.value->propagate_notification(p_notification);
2603 }
2604 data.blocked--;
2605 }
2606
2607 void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
2608 ERR_THREAD_GUARD
2609 data.blocked++;
2610
2611 if (p_parent_first && has_method(p_method)) {
2612 callv(p_method, p_args);
2613 }
2614
2615 for (KeyValue<StringName, Node *> &K : data.children) {
2616 K.value->propagate_call(p_method, p_args, p_parent_first);
2617 }
2618
2619 if (!p_parent_first && has_method(p_method)) {
2620 callv(p_method, p_args);
2621 }
2622
2623 data.blocked--;
2624 }
2625
2626 void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
2627 if (get_owner() == p_owner) {
2628 set_owner(p_by_owner);
2629 }
2630
2631 data.blocked++;
2632 for (KeyValue<StringName, Node *> &K : data.children) {
2633 K.value->_propagate_replace_owner(p_owner, p_by_owner);
2634 }
2635 data.blocked--;
2636 }
2637
2638 RequiredResult<Tween> Node::create_tween() {
2639 ERR_THREAD_GUARD_V(Ref<Tween>());
2640
2641 SceneTree *tree = data.tree;
2642 if (!tree) {
2643 tree = SceneTree::get_singleton();
2644 }
2645 ERR_FAIL_NULL_V_MSG(tree, Ref<Tween>(), "No available SceneTree to create the Tween.");
2646
2647 Ref<Tween> tween = tree->create_tween();
2648 tween->bind_node(this);
2649 return tween;
2650 }
2651
2652 void Node::set_scene_file_path(const String &p_scene_file_path) {
2653 ERR_THREAD_GUARD
2654 data.scene_file_path = p_scene_file_path;
2655 _emit_editor_state_changed();
2656 }
2657
2658 String Node::get_scene_file_path() const {
2659 return data.scene_file_path;
2660 }
2661
2662 void Node::set_editor_description(const String &p_editor_description) {
2663 ERR_THREAD_GUARD
2664 if (data.editor_description == p_editor_description) {
2665 return;
2666 }
2667
2668 data.editor_description = p_editor_description;
2669 emit_signal(SNAME("editor_description_changed"), this);
2670 }
2671
2672 String Node::get_editor_description() const {
2673 return data.editor_description;
2674 }
2675
2676 void Node::set_editable_instance(RequiredParam<Node> rp_node, bool p_editable) {
2677 ERR_THREAD_GUARD
2678 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
2679 ERR_FAIL_COND(!is_ancestor_of(p_node));
2680 if (!p_editable) {
2681 p_node->data.editable_instance = false;
2682 // Avoid this flag being needlessly saved;
2683 // also give more visual feedback if editable children are re-enabled
2684 set_display_folded(false);
2685 } else {
2686 p_node->data.editable_instance = true;
2687 }
2688
2689 p_node->_emit_editor_state_changed();
2690 }
2691
2692 bool Node::is_editable_instance(const Node *p_node) const {
2693 if (!p_node) {
2694 return false; // Easier, null is never editable. :)
2695 }
2696 ERR_FAIL_COND_V(!is_ancestor_of(p_node), false);
2697 return p_node->data.editable_instance;
2698 }
2699
2700 Node *Node::get_deepest_editable_node(Node *p_start_node) const {
2701 ERR_THREAD_GUARD_V(nullptr);
2702 ERR_FAIL_NULL_V(p_start_node, nullptr);
2703 ERR_FAIL_COND_V(!is_ancestor_of(p_start_node), p_start_node);
2704
2705 Node const *iterated_item = p_start_node;
2706 Node *node = p_start_node;
2707
2708 while (iterated_item->get_owner() && iterated_item->get_owner() != this) {
2709 if (!is_editable_instance(iterated_item->get_owner())) {
2710 node = iterated_item->get_owner();
2711 }
2712
2713 iterated_item = iterated_item->get_owner();
2714 }
2715
2716 return node;
2717 }
2718
2719 #ifdef TOOLS_ENABLED
2720 void Node::set_property_pinned(const String &p_property, bool p_pinned) {
2721 ERR_THREAD_GUARD
2722 bool current_pinned = false;
2723 Array pinned = get_meta("_edit_pinned_properties_", Array());
2724 StringName psa = get_property_store_alias(p_property);
2725 current_pinned = pinned.has(psa);
2726
2727 if (current_pinned != p_pinned) {
2728 if (p_pinned) {
2729 pinned.append(psa);
2730 } else {
2731 pinned.erase(psa);
2732 }
2733 }
2734
2735 if (pinned.is_empty()) {
2736 remove_meta("_edit_pinned_properties_");
2737 } else {
2738 set_meta("_edit_pinned_properties_", pinned);
2739 }
2740 }
2741
2742 bool Node::is_property_pinned(const StringName &p_property) const {
2743 Array pinned = get_meta("_edit_pinned_properties_", Array());
2744 StringName psa = get_property_store_alias(p_property);
2745 return pinned.has(psa);
2746 }
2747
2748 StringName Node::get_property_store_alias(const StringName &p_property) const {
2749 return p_property;
2750 }
2751
2752 bool Node::is_part_of_edited_scene() const {
2753 return Engine::get_singleton()->is_editor_hint() && is_inside_tree() && data.tree->get_edited_scene_root() &&
2754 data.tree->get_edited_scene_root()->get_parent()->is_ancestor_of(this);
2755 }
2756 #endif
2757
2758 void Node::get_storable_properties(HashSet<StringName> &r_storable_properties) const {
2759 ERR_THREAD_GUARD
2760 List<PropertyInfo> property_list;
2761 get_property_list(&property_list);
2762 for (const PropertyInfo &pi : property_list) {
2763 if ((pi.usage & PROPERTY_USAGE_STORAGE)) {
2764 r_storable_properties.insert(pi.name);
2765 }
2766 }
2767 }
2768
2769 void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
2770 ERR_THREAD_GUARD
2771 data.instance_state = p_state;
2772 }
2773
2774 Ref<SceneState> Node::get_scene_instance_state() const {
2775 return data.instance_state;
2776 }
2777
2778 void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
2779 ERR_THREAD_GUARD
2780 data.inherited_state = p_state;
2781 _emit_editor_state_changed();
2782 }
2783
2784 Ref<SceneState> Node::get_scene_inherited_state() const {
2785 return data.inherited_state;
2786 }
2787
2788 void Node::set_scene_instance_load_placeholder(bool p_enable) {
2789 data.use_placeholder = p_enable;
2790 }
2791
2792 bool Node::get_scene_instance_load_placeholder() const {
2793 return data.use_placeholder;
2794 }
2795
2796 Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) const {
2797 ERR_THREAD_GUARD_V(nullptr);
2798 Node *node = nullptr;
2799
2800 bool instantiated = false;
2801
2802 if (Object::cast_to<InstancePlaceholder>(this)) {
2803 const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
2804 InstancePlaceholder *nip = memnew(InstancePlaceholder);
2805 nip->set_instance_path(ip->get_instance_path());
2806 node = nip;
2807
2808 } else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && is_instance()) {
2809 Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
2810 ERR_FAIL_COND_V(res.is_null(), nullptr);
2811 PackedScene::GenEditState edit_state = PackedScene::GEN_EDIT_STATE_DISABLED;
2812 #ifdef TOOLS_ENABLED
2813 if (p_flags & DUPLICATE_FROM_EDITOR) {
2814 edit_state = PackedScene::GEN_EDIT_STATE_INSTANCE;
2815 }
2816 #endif
2817 node = res->instantiate(edit_state);
2818 ERR_FAIL_NULL_V(node, nullptr);
2819 node->set_scene_instance_load_placeholder(get_scene_instance_load_placeholder());
2820
2821 instantiated = true;
2822
2823 } else {
2824 Object *obj = ClassDB::instantiate(get_class());
2825 ERR_FAIL_NULL_V(obj, nullptr);
2826 node = Object::cast_to<Node>(obj);
2827 if (!node) {
2828 memdelete(obj);
2829 }
2830 ERR_FAIL_NULL_V(node, nullptr);
2831 }
2832
2833 if (is_instance()) {
2834 node->set_scene_file_path(get_scene_file_path());
2835 node->data.editable_instance = data.editable_instance;
2836 }
2837
2838 List<const Node *> hidden_roots;
2839 List<const Node *> node_tree;
2840 node_tree.push_front(this);
2841
2842 if (instantiated) {
2843 // Since nodes in the instantiated hierarchy won't be duplicated explicitly, we need to make an inventory
2844 // of all the nodes in the tree of the instantiated scene in order to transfer the values of the properties
2845
2846 Vector<const Node *> instance_roots;
2847 instance_roots.push_back(this);
2848
2849 for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
2850 for (int i = 0; i < N->get()->get_child_count(false); ++i) {
2851 Node *descendant = N->get()->get_child(i, false);
2852
2853 // Skip nodes not really belonging to the instantiated hierarchy; they'll be processed normally later
2854 // but remember non-instantiated nodes that are hidden below instantiated ones
2855 if (!instance_roots.has(descendant->get_owner())) {
2856 if (descendant->get_parent() && descendant->get_parent() != this && descendant->data.owner != descendant->get_parent()) {
2857 hidden_roots.push_back(descendant);
2858 }
2859 continue;
2860 }
2861
2862 node_tree.push_back(descendant);
2863
2864 if (descendant->is_instance() && instance_roots.has(descendant->get_owner())) {
2865 instance_roots.push_back(descendant);
2866 }
2867 }
2868 }
2869
2870 for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
2871 const Node *source_node = N->get();
2872 if (source_node->get_scene_file_path().is_empty()) {
2873 continue;
2874 }
2875
2876 NodePath relative_path = get_path_to(source_node);
2877 Node *duplicated_node = node->get_node_or_null(relative_path);
2878 ERR_CONTINUE(!duplicated_node);
2879
2880 duplicated_node->data.editable_instance = source_node->data.editable_instance;
2881 }
2882 }
2883
2884 if (get_name() != String()) {
2885 node->set_name(get_name());
2886 }
2887
2888 #ifdef TOOLS_ENABLED
2889 if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
2890 r_duplimap->insert(this, node);
2891 }
2892 #endif
2893
2894 if (p_flags & DUPLICATE_GROUPS) {
2895 List<GroupInfo> gi;
2896 get_groups(&gi);
2897 for (const GroupInfo &E : gi) {
2898 #ifdef TOOLS_ENABLED
2899 if ((p_flags & DUPLICATE_FROM_EDITOR) && !E.persistent) {
2900 continue;
2901 }
2902 #endif
2903
2904 node->add_to_group(E.name, E.persistent);
2905 }
2906 }
2907
2908 for (int i = 0; i < get_child_count(false); i++) {
2909 if (instantiated && get_child(i, false)->data.owner == this) {
2910 continue; //part of instance
2911 }
2912
2913 Node *dup = get_child(i, false)->_duplicate(p_flags, r_duplimap);
2914 if (!dup) {
2915 memdelete(node);
2916 return nullptr;
2917 }
2918
2919 node->add_child(dup);
2920 if (i < node->get_child_count(false) - 1) {
2921 node->move_child(dup, i);
2922 }
2923 }
2924
2925 for (const Node *&E : hidden_roots) {
2926 Node *parent = node->get_node(get_path_to(E->data.parent));
2927 if (!parent) {
2928 memdelete(node);
2929 return nullptr;
2930 }
2931
2932 Node *dup = E->_duplicate(p_flags, r_duplimap);
2933 if (!dup) {
2934 memdelete(node);
2935 return nullptr;
2936 }
2937
2938 parent->add_child(dup);
2939 int pos = E->get_index(false);
2940
2941 if (pos < parent->get_child_count(false) - 1) {
2942 parent->move_child(dup, pos);
2943 }
2944 }
2945 return node;
2946 }
2947
2948 Node *Node::duplicate(int p_flags) const {
2949 ERR_THREAD_GUARD_V(nullptr);
2950 Node *dupe = _duplicate(p_flags);
2951
2952 ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2953
2954 if (p_flags & DUPLICATE_SCRIPTS) {
2955 _duplicate_scripts(this, dupe);
2956 }
2957
2958 _duplicate_properties(this, this, dupe, p_flags);
2959
2960 if (p_flags & DUPLICATE_SIGNALS) {
2961 _duplicate_signals(this, dupe);
2962 }
2963
2964 return dupe;
2965 }
2966
2967 #ifdef TOOLS_ENABLED
2968 Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const {
2969 HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> tmp;
2970 return duplicate_from_editor(r_duplimap, nullptr, tmp);
2971 }
2972
2973 Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, Node *p_scene_root, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resource_remap) const {
2974 int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR;
2975 Node *dupe = _duplicate(flags, &r_duplimap);
2976
2977 ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2978
2979 if (flags & DUPLICATE_SCRIPTS) {
2980 _duplicate_scripts(this, dupe);
2981 }
2982
2983 _duplicate_properties(this, this, dupe, flags);
2984
2985 // This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
2986 if (p_scene_root) {
2987 remap_node_resources(dupe, p_scene_root, p_resource_remap);
2988 }
2989
2990 // Duplication of signals must happen after all the node descendants have been copied,
2991 // because re-targeting of connections from some descendant to another is not possible
2992 // if the emitter node comes later in tree order than the receiver
2993 _duplicate_signals(this, dupe);
2994
2995 return dupe;
2996 }
2997
2998 void Node::remap_node_resources(Node *p_node, Node *p_scene_root, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resource_remap) const {
2999 Node *local_scene = p_node->is_instance() ? p_node : (p_node->get_owner() ? p_node->get_owner() : p_scene_root);
3000
3001 List<PropertyInfo> props;
3002 p_node->get_property_list(&props);
3003
3004 for (const PropertyInfo &E : props) {
3005 if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
3006 continue;
3007 }
3008
3009 Variant v = p_node->get(E.name);
3010 if (!v.is_ref_counted()) {
3011 continue;
3012 }
3013 Ref<Resource> res = v;
3014 if (res.is_null()) {
3015 continue;
3016 }
3017
3018 if (res->is_local_to_scene()) {
3019 if (local_scene == res->get_local_scene()) {
3020 continue;
3021 }
3022 Ref<Resource> dup = SceneState::get_remap_resource(res, p_resource_remap, nullptr, local_scene);
3023 p_node->set(E.name, dup);
3024 continue;
3025 }
3026
3027 if (res->is_built_in()) {
3028 // Use nullptr instead of a specific node (current scene root node) to represent the scene,
3029 // as the Make Scene Root operation may be executed.
3030 if (p_resource_remap[nullptr].has(res)) {
3031 p_node->set(E.name, p_resource_remap[nullptr][res]);
3032 remap_nested_resources(res, p_resource_remap[nullptr]);
3033 }
3034 }
3035 }
3036
3037 for (int i = 0; i < p_node->get_child_count(); i++) {
3038 remap_node_resources(p_node->get_child(i), p_scene_root, p_resource_remap);
3039 }
3040 }
3041
3042 void Node::remap_nested_resources(Ref<Resource> p_resource, HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
3043 List<PropertyInfo> props;
3044 p_resource->get_property_list(&props);
3045
3046 for (const PropertyInfo &E : props) {
3047 if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
3048 continue;
3049 }
3050
3051 Variant v = p_resource->get(E.name);
3052 if (v.is_ref_counted()) {
3053 Ref<Resource> res = v;
3054 if (res.is_valid()) {
3055 if (p_resource_remap.has(res)) {
3056 p_resource->set(E.name, p_resource_remap[res]);
3057 remap_nested_resources(res, p_resource_remap);
3058 }
3059 }
3060 }
3061 }
3062 }
3063
3064 void Node::_emit_editor_state_changed() {
3065 // This is required for the SceneTreeEditor to properly keep track of when an update is needed.
3066 // This signal might be expensive and not needed for anything outside of the editor.
3067 if (Engine::get_singleton()->is_editor_hint()) {
3068 emit_signal(SNAME("editor_state_changed"));
3069 }
3070 }
3071 #endif
3072
3073 void Node::_duplicate_scripts(const Node *p_original, Node *p_copy) const {
3074 bool is_valid = false;
3075 Variant scr = p_original->get(CoreStringName(script), &is_valid);
3076 if (is_valid) {
3077 p_copy->set(CoreStringName(script), scr);
3078 }
3079
3080 for (int i = 0; i < p_original->get_child_count(false); i++) {
3081 Node *copy_child = p_copy->get_child(i, false);
3082 ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
3083 _duplicate_scripts(p_original->get_child(i, false), copy_child);
3084 }
3085 }
3086
3087 // Duplicate node's properties.
3088 // This has to be called after nodes have been duplicated since there might be properties
3089 // of type Node that can be updated properly only if duplicated node tree is complete.
3090 void Node::_duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const {
3091 List<PropertyInfo> props;
3092 p_original->get_property_list(&props);
3093 const StringName &script_property_name = CoreStringName(script);
3094 for (const PropertyInfo &E : props) {
3095 if (!(p_flags & DUPLICATE_INTERNAL_STATE) && !(E.usage & PROPERTY_USAGE_STORAGE)) {
3096 continue;
3097 }
3098 const StringName name = E.name;
3099
3100 if (name == script_property_name) {
3101 continue;
3102 }
3103
3104 Variant value = p_original->get(name);
3105 // To keep classic behavior, because, in contrast, nowadays a resource would be duplicated.
3106 if (value.get_type() != Variant::OBJECT) {
3107 value = value.duplicate(true);
3108 }
3109
3110 if (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE) {
3111 Resource *res = Object::cast_to<Resource>(value);
3112 if (res) { // Duplicate only if it's a resource
3113 p_copy->set(name, res->duplicate());
3114 }
3115 } else {
3116 if (value.get_type() == Variant::OBJECT) {
3117 Node *property_node = Object::cast_to<Node>(value);
3118 Variant out_value = value;
3119 if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3120 out_value = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3121 }
3122 p_copy->set(name, out_value);
3123 } else if (value.get_type() == Variant::ARRAY) {
3124 Array arr = value;
3125 if (arr.get_typed_builtin() == Variant::OBJECT) {
3126 for (int i = 0; i < arr.size(); i++) {
3127 Node *property_node = Object::cast_to<Node>(arr[i]);
3128 if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3129 arr[i] = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3130 }
3131 }
3132 }
3133 p_copy->set(name, arr);
3134 } else {
3135 p_copy->set(name, value);
3136 }
3137 }
3138 }
3139
3140 for (int i = 0; i < p_original->get_child_count(false); i++) {
3141 Node *copy_child = p_copy->get_child(i, false);
3142 ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
3143 _duplicate_properties(p_root, p_original->get_child(i, false), copy_child, p_flags);
3144 }
3145 }
3146
3147 // Duplication of signals must happen after all the node descendants have been copied,
3148 // because re-targeting of connections from some descendant to another is not possible
3149 // if the emitter node comes later in tree order than the receiver
3150 void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
3151 if ((this != p_original) && !(p_original->is_ancestor_of(this))) {
3152 return;
3153 }
3154
3155 List<const Node *> process_list;
3156 process_list.push_back(this);
3157 while (!process_list.is_empty()) {
3158 const Node *n = process_list.front()->get();
3159 process_list.pop_front();
3160
3161 List<Connection> conns;
3162 n->get_all_signal_connections(&conns);
3163
3164 for (const Connection &E : conns) {
3165 if (E.flags & CONNECT_PERSIST) {
3166 //user connected
3167 NodePath p = p_original->get_path_to(n);
3168 Node *copy = p_copy->get_node(p);
3169
3170 Node *target = Object::cast_to<Node>(E.callable.get_object());
3171 if (!target) {
3172 continue;
3173 }
3174
3175 NodePath ptarget = p_original->get_path_to(target);
3176 if (ptarget.is_empty()) {
3177 continue;
3178 }
3179
3180 Node *copytarget = target;
3181
3182 // Attempt to find a path to the duplicate target, if it seems it's not part
3183 // of the duplicated and not yet parented hierarchy then at least try to connect
3184 // to the same target as the original
3185
3186 if (p_copy->has_node(ptarget)) {
3187 copytarget = p_copy->get_node(ptarget);
3188 }
3189
3190 if (copy && copytarget && E.callable.get_method() != StringName()) {
3191 Callable copy_callable = Callable(copytarget, E.callable.get_method());
3192 if (!copy->is_connected(E.signal.get_name(), copy_callable)) {
3193 int unbound_arg_count = E.callable.get_unbound_arguments_count();
3194 if (unbound_arg_count > 0) {
3195 copy_callable = copy_callable.unbind(unbound_arg_count);
3196 }
3197 if (E.callable.get_bound_arguments_count() > 0) {
3198 copy_callable = copy_callable.bindv(E.callable.get_bound_arguments());
3199 }
3200 copy->connect(E.signal.get_name(), copy_callable, E.flags);
3201 }
3202 }
3203 }
3204 }
3205
3206 for (int i = 0; i < n->get_child_count(); i++) {
3207 process_list.push_back(n->get_child(i));
3208 }
3209 }
3210 }
3211
3212 static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
3213 if (p_node->get_owner() == p_by) {
3214 p_owned->push_back(p_node);
3215 }
3216
3217 for (int i = 0; i < p_node->get_child_count(); i++) {
3218 find_owned_by(p_by, p_node->get_child(i), p_owned);
3219 }
3220 }
3221
3222 void Node::replace_by(RequiredParam<Node> rp_node, bool p_keep_groups) {
3223 ERR_THREAD_GUARD
3224 EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
3225 ERR_FAIL_COND(p_node->data.parent);
3226
3227 List<Node *> owned = data.owned;
3228 List<Node *> owned_by_owner;
3229 Node *owner = (data.owner == this) ? p_node : data.owner;
3230
3231 if (p_keep_groups) {
3232 List<GroupInfo> groups;
3233 get_groups(&groups);
3234
3235 for (const GroupInfo &E : groups) {
3236 p_node->add_to_group(E.name, E.persistent);
3237 }
3238 }
3239
3240 _replace_connections_target(p_node);
3241
3242 if (data.owner) {
3243 for (int i = 0; i < get_child_count(); i++) {
3244 find_owned_by(data.owner, get_child(i), &owned_by_owner);
3245 }
3246
3247 _clean_up_owner();
3248 }
3249
3250 Node *parent = data.parent;
3251 int index_in_parent = get_index(false);
3252
3253 if (data.parent) {
3254 parent->remove_child(this);
3255 parent->add_child(p_node);
3256 parent->move_child(p_node, index_in_parent);
3257 }
3258
3259 emit_signal(SNAME("replacing_by"), p_node);
3260
3261 while (get_child_count()) {
3262 Node *child = get_child(0);
3263 remove_child(child);
3264 if (!child->is_internal()) {
3265 // Add the custom children to the p_node.
3266 Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3267 child->set_owner(nullptr);
3268 p_node->add_child(child);
3269 child->set_owner(child_owner);
3270 }
3271 }
3272
3273 p_node->set_owner(owner);
3274 for (Node *E : owned) {
3275 if (E->data.owner != p_node) {
3276 E->set_owner(p_node);
3277 }
3278 }
3279
3280 for (Node *E : owned_by_owner) {
3281 if (E->data.owner != owner) {
3282 E->set_owner(owner);
3283 }
3284 }
3285
3286 p_node->set_scene_file_path(get_scene_file_path());
3287 }
3288
3289 void Node::_replace_connections_target(Node *p_new_target) {
3290 List<Connection> cl;
3291 get_signals_connected_to_this(&cl);
3292
3293 for (const Connection &c : cl) {
3294 if (c.flags & CONNECT_PERSIST) {
3295 c.signal.get_object()->disconnect(c.signal.get_name(), Callable(this, c.callable.get_method()));
3296 bool valid = p_new_target->has_method(c.callable.get_method()) || Ref<Script>(p_new_target->get_script()).is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.callable.get_method());
3297 ERR_CONTINUE_MSG(!valid, vformat("Attempt to connect signal '%s.%s' to nonexistent method '%s.%s'.", c.signal.get_object()->get_class(), c.signal.get_name(), c.callable.get_object()->get_class(), c.callable.get_method()));
3298 c.signal.get_object()->connect(c.signal.get_name(), Callable(p_new_target, c.callable.get_method()), c.flags);
3299 }
3300 }
3301 }
3302
3303 bool Node::has_node_and_resource(const NodePath &p_path) const {
3304 ERR_THREAD_GUARD_V(false);
3305 if (!has_node(p_path)) {
3306 return false;
3307 }
3308 Ref<Resource> res;
3309 Vector<StringName> leftover_path;
3310 Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3311
3312 return node;
3313 }
3314
3315 Array Node::_get_node_and_resource(const NodePath &p_path) {
3316 Ref<Resource> res;
3317 Vector<StringName> leftover_path;
3318 Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3319 Array result;
3320
3321 if (node) {
3322 result.push_back(node);
3323 } else {
3324 result.push_back(Variant());
3325 }
3326
3327 if (res.is_valid()) {
3328 result.push_back(res);
3329 } else {
3330 result.push_back(Variant());
3331 }
3332
3333 result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
3334
3335 return result;
3336 }
3337
3338 Node *Node::get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
3339 ERR_THREAD_GUARD_V(nullptr);
3340 r_res = Ref<Resource>();
3341 r_leftover_subpath = Vector<StringName>();
3342 Node *node = get_node_or_null(p_path);
3343 if (!node) {
3344 return nullptr;
3345 }
3346
3347 if (p_path.get_subname_count()) {
3348 int j = 0;
3349 // If not p_last_is_property, we shouldn't consider the last one as part of the resource
3350 for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
3351 bool is_valid = false;
3352 Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j), &is_valid) : r_res->get(p_path.get_subname(j), &is_valid);
3353
3354 if (!is_valid) { // Found nothing on that path
3355 return nullptr;
3356 }
3357
3358 Ref<Resource> new_res = new_res_v;
3359
3360 if (new_res.is_null()) { // No longer a resource, assume property
3361 break;
3362 }
3363
3364 r_res = new_res;
3365 }
3366 for (; j < p_path.get_subname_count(); j++) {
3367 // Put the rest of the subpath in the leftover path
3368 r_leftover_subpath.push_back(p_path.get_subname(j));
3369 }
3370 }
3371
3372 return node;
3373 }
3374
3375 void Node::_set_tree(SceneTree *p_tree) {
3376 SceneTree *tree_changed_a = nullptr;
3377 SceneTree *tree_changed_b = nullptr;
3378
3379 //ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
3380
3381 if (data.tree) {
3382 _propagate_exit_tree();
3383
3384 tree_changed_a = data.tree;
3385 }
3386
3387 data.tree = p_tree;
3388
3389 if (data.tree) {
3390 _propagate_enter_tree();
3391 if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
3392 _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
3393 }
3394
3395 tree_changed_b = data.tree;
3396 }
3397
3398 if (tree_changed_a) {
3399 tree_changed_a->tree_changed();
3400 }
3401 if (tree_changed_b) {
3402 tree_changed_b->tree_changed();
3403 }
3404 }
3405
3406 #ifdef DEBUG_ENABLED
3407 static HashMap<ObjectID, List<String>> _print_orphan_nodes_map;
3408
3409 static void _print_orphan_nodes_routine(Object *p_obj, void *p_user_data) {
3410 Node *n = Object::cast_to<Node>(p_obj);
3411 if (!n) {
3412 return;
3413 }
3414
3415 if (n->is_inside_tree()) {
3416 return;
3417 }
3418
3419 Node *p = n;
3420 while (p->get_parent()) {
3421 p = p->get_parent();
3422 }
3423
3424 String path;
3425 if (p == n) {
3426 path = n->get_name();
3427 } else {
3428 path = String(p->get_name()) + "/" + String(p->get_path_to(n));
3429 }
3430
3431 String source;
3432 Variant script = n->get_script();
3433 if (!script.is_null()) {
3434 Resource *obj = Object::cast_to<Resource>(script);
3435 source = obj->get_path();
3436 }
3437
3438 List<String> info_strings;
3439 info_strings.push_back(path);
3440 info_strings.push_back(n->get_class());
3441 info_strings.push_back(source);
3442
3443 _print_orphan_nodes_map[p_obj->get_instance_id()] = info_strings;
3444 }
3445 #endif // DEBUG_ENABLED
3446
3447 void Node::print_orphan_nodes() {
3448 #ifdef DEBUG_ENABLED
3449 // Make sure it's empty.
3450 _print_orphan_nodes_map.clear();
3451
3452 // Collect and print information about orphan nodes.
3453 ObjectDB::debug_objects(_print_orphan_nodes_routine, nullptr);
3454
3455 for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3456 print_line(itos(E.key) + " - Stray Node: " + E.value.get(0) + " (Type: " + E.value.get(1) + ") (Source:" + E.value.get(2) + ")");
3457 }
3458
3459 // Flush it after use.
3460 _print_orphan_nodes_map.clear();
3461 #endif
3462 }
3463 TypedArray<int> Node::get_orphan_node_ids() {
3464 TypedArray<int> ret;
3465 #ifdef DEBUG_ENABLED
3466 // Make sure it's empty.
3467 _print_orphan_nodes_map.clear();
3468
3469 // Collect and return information about orphan nodes.
3470 ObjectDB::debug_objects(_print_orphan_nodes_routine, nullptr);
3471
3472 for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3473 ret.push_back(E.key);
3474 }
3475
3476 // Flush it after use.
3477 _print_orphan_nodes_map.clear();
3478 #endif
3479 return ret;
3480 }
3481
3482 void Node::queue_free() {
3483 // There are users which instantiate multiple scene trees for their games.
3484 // Use the node's own tree to handle its deletion when relevant.
3485 if (data.tree) {
3486 data.tree->queue_delete(this);
3487 } else {
3488 SceneTree *tree = SceneTree::get_singleton();
3489 ERR_FAIL_NULL_MSG(tree, "Can't queue free a node when no SceneTree is available.");
3490 tree->queue_delete(this);
3491 }
3492 }
3493
3494 #ifdef TOOLS_ENABLED
3495 static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
3496 if (p_node != p_base && !p_node->get_owner()) {
3497 return;
3498 }
3499 if (p_node->is_unique_name_in_owner() && p_node->get_owner() == p_base) {
3500 String n = "%" + p_node->get_name();
3501 r_options->push_back(n.quote());
3502 }
3503 String n = String(p_base->get_path_to(p_node));
3504 r_options->push_back(n.quote());
3505 for (int i = 0; i < p_node->get_child_count(); i++) {
3506 _add_nodes_to_options(p_base, p_node->get_child(i), r_options);
3507 }
3508 }
3509
3510 void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
3511 const String pf = p_function;
3512 if (p_idx == 0 && (pf == "has_node" || pf == "get_node" || pf == "get_node_or_null")) {
3513 _add_nodes_to_options(this, this, r_options);
3514 } else if (p_idx == 0 && (pf == "add_to_group" || pf == "remove_from_group" || pf == "is_in_group")) {
3515 HashMap<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
3516 for (const KeyValue<StringName, String> &E : global_groups) {
3517 r_options->push_back(E.key.operator String().quote());
3518 }
3519 }
3520 Object::get_argument_options(p_function, p_idx, r_options);
3521 }
3522 #endif
3523
3524 void Node::clear_internal_tree_resource_paths() {
3525 clear_internal_resource_paths();
3526 for (KeyValue<StringName, Node *> &K : data.children) {
3527 K.value->clear_internal_tree_resource_paths();
3528 }
3529 }
3530
3531 PackedStringArray Node::get_accessibility_configuration_warnings() const {
3532 ERR_THREAD_GUARD_V(PackedStringArray());
3533 PackedStringArray ret;
3534
3535 Vector<String> warnings;
3536 if (GDVIRTUAL_CALL(_get_accessibility_configuration_warnings, warnings)) {
3537 ret.append_array(warnings);
3538 }
3539
3540 return ret;
3541 }
3542
3543 PackedStringArray Node::get_configuration_warnings() const {
3544 ERR_THREAD_GUARD_V(PackedStringArray());
3545 PackedStringArray ret;
3546
3547 Vector<String> warnings;
3548 if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
3549 ret.append_array(warnings);
3550 }
3551
3552 return ret;
3553 }
3554
3555 void Node::update_configuration_warnings() {
3556 ERR_THREAD_GUARD
3557 #ifdef TOOLS_ENABLED
3558 if (!data.tree) {
3559 return;
3560 }
3561 if (data.tree->get_edited_scene_root() && (data.tree->get_edited_scene_root() == this || data.tree->get_edited_scene_root()->is_ancestor_of(this))) {
3562 data.tree->emit_signal(SceneStringName(node_configuration_warning_changed), this);
3563 }
3564 #endif
3565 }
3566
3567 void Node::set_display_folded(bool p_folded) {
3568 ERR_THREAD_GUARD
3569 data.display_folded = p_folded;
3570 }
3571
3572 bool Node::is_displayed_folded() const {
3573 return data.display_folded;
3574 }
3575
3576 bool Node::is_ready() const {
3577 return !data.ready_first;
3578 }
3579
3580 void Node::request_ready() {
3581 ERR_THREAD_GUARD
3582 data.ready_first = true;
3583 }
3584
3585 void Node::_call_input(const Ref<InputEvent> &p_event) {
3586 if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3587 GDVIRTUAL_CALL(_input, p_event);
3588 }
3589 if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3590 return;
3591 }
3592 input(p_event);
3593 }
3594
3595 void Node::_call_shortcut_input(const Ref<InputEvent> &p_event) {
3596 if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3597 GDVIRTUAL_CALL(_shortcut_input, p_event);
3598 }
3599 if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3600 return;
3601 }
3602 shortcut_input(p_event);
3603 }
3604
3605 void Node::_call_unhandled_input(const Ref<InputEvent> &p_event) {
3606 if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3607 GDVIRTUAL_CALL(_unhandled_input, p_event);
3608 }
3609 if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3610 return;
3611 }
3612 unhandled_input(p_event);
3613 }
3614
3615 void Node::_call_unhandled_key_input(const Ref<InputEvent> &p_event) {
3616 if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3617 GDVIRTUAL_CALL(_unhandled_key_input, p_event);
3618 }
3619 if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3620 return;
3621 }
3622 unhandled_key_input(p_event);
3623 }
3624
3625 void Node::_validate_property(PropertyInfo &p_property) const {
3626 if ((p_property.name == "process_thread_group_order" || p_property.name == "process_thread_messages") && data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
3627 p_property.usage = 0;
3628 }
3629 }
3630
3631 String Node::_to_string() {
3632 ERR_THREAD_GUARD_V(String());
3633 return (get_name() ? String(get_name()) + ":" : "") + Object::_to_string();
3634 }
3635
3636 void Node::input(const Ref<InputEvent> &p_event) {
3637 }
3638
3639 void Node::shortcut_input(const Ref<InputEvent> &p_key_event) {
3640 }
3641
3642 void Node::unhandled_input(const Ref<InputEvent> &p_event) {
3643 }
3644
3645 void Node::unhandled_key_input(const Ref<InputEvent> &p_key_event) {
3646 }
3647
3648 Variant Node::_call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3649 if (p_argcount < 1) {
3650 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3651 r_error.expected = 1;
3652 return Variant();
3653 }
3654
3655 if (!p_args[0]->is_string()) {
3656 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3657 r_error.argument = 0;
3658 r_error.expected = Variant::STRING_NAME;
3659 return Variant();
3660 }
3661
3662 r_error.error = Callable::CallError::CALL_OK;
3663
3664 StringName method = *p_args[0];
3665
3666 call_deferred_thread_groupp(method, &p_args[1], p_argcount - 1, true);
3667
3668 return Variant();
3669 }
3670
3671 Variant Node::_call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3672 if (p_argcount < 1) {
3673 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3674 r_error.expected = 1;
3675 return Variant();
3676 }
3677
3678 if (!p_args[0]->is_string()) {
3679 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3680 r_error.argument = 0;
3681 r_error.expected = Variant::STRING_NAME;
3682 return Variant();
3683 }
3684
3685 r_error.error = Callable::CallError::CALL_OK;
3686
3687 StringName method = *p_args[0];
3688
3689 call_thread_safep(method, &p_args[1], p_argcount - 1, true);
3690
3691 return Variant();
3692 }
3693
3694 void Node::call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3695 ERR_FAIL_COND(!is_inside_tree());
3696 SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3697 pg->call_queue.push_callp(this, p_method, p_args, p_argcount, p_show_error);
3698 }
3699
3700 void Node::set_deferred_thread_group(const StringName &p_property, const Variant &p_value) {
3701 ERR_FAIL_COND(!is_inside_tree());
3702 SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3703 pg->call_queue.push_set(this, p_property, p_value);
3704 }
3705
3706 void Node::notify_deferred_thread_group(int p_notification) {
3707 ERR_FAIL_COND(!is_inside_tree());
3708 SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3709 pg->call_queue.push_notification(this, p_notification);
3710 }
3711
3712 void Node::call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3713 if (is_accessible_from_caller_thread()) {
3714 Callable::CallError ce;
3715 callp(p_method, p_args, p_argcount, ce);
3716 if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
3717 ERR_FAIL_MSG("Error calling method from 'call_threadp': " + Variant::get_call_error_text(this, p_method, p_args, p_argcount, ce) + ".");
3718 }
3719 } else {
3720 call_deferred_thread_groupp(p_method, p_args, p_argcount, p_show_error);
3721 }
3722 }
3723
3724 void Node::set_thread_safe(const StringName &p_property, const Variant &p_value) {
3725 if (is_accessible_from_caller_thread()) {
3726 set(p_property, p_value);
3727 } else {
3728 set_deferred_thread_group(p_property, p_value);
3729 }
3730 }
3731
3732 void Node::notify_thread_safe(int p_notification) {
3733 if (is_accessible_from_caller_thread()) {
3734 notification(p_notification);
3735 } else {
3736 notify_deferred_thread_group(p_notification);
3737 }
3738 }
3739
3740 RID Node::get_focused_accessibility_element() const {
3741 RID id;
3742 if (GDVIRTUAL_CALL(_get_focused_accessibility_element, id)) {
3743 return id;
3744 } else {
3745 return get_accessibility_element();
3746 }
3747 }
3748
3749 void Node::queue_accessibility_update() {
3750 if (is_inside_tree() && !is_part_of_edited_scene()) {
3751 data.tree->_accessibility_notify_change(this);
3752 }
3753 }
3754
3755 RID Node::get_accessibility_element() const {
3756 if (is_part_of_edited_scene()) {
3757 return RID();
3758 }
3759 if (unlikely(data.accessibility_element.is_null())) {
3760 Window *w = get_non_popup_window();
3761 if (w && w->get_window_id() != DisplayServer::INVALID_WINDOW_ID && get_window()->is_visible()) {
3762 data.accessibility_element = DisplayServer::get_singleton()->accessibility_create_element(w->get_window_id(), DisplayServer::ROLE_CONTAINER);
3763 }
3764 }
3765 return data.accessibility_element;
3766 }
3767
3768 void Node::_bind_methods() {
3769 GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"), 0);
3770 GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case,kebab-case"), NAME_CASING_PASCAL_CASE);
3771
3772 ClassDB::bind_static_method("Node", D_METHOD("print_orphan_nodes"), &Node::print_orphan_nodes);
3773 ClassDB::bind_static_method("Node", D_METHOD("get_orphan_node_ids"), &Node::get_orphan_node_ids);
3774 ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "force_readable_name"), &Node::add_sibling, DEFVAL(false));
3775
3776 ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
3777 ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
3778 ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
3779 ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
3780 ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true));
3781 ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript.
3782 ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::get_children, DEFVAL(false));
3783 ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));
3784 ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
3785 ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
3786 ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
3787 ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
3788 ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
3789 ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
3790 ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
3791 ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
3792 ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
3793
3794 ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
3795 ClassDB::bind_method(D_METHOD("is_part_of_edited_scene"), &Node::is_part_of_edited_scene);
3796 ClassDB::bind_method(D_METHOD("is_ancestor_of", "node"), &Node::is_ancestor_of);
3797 ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
3798 ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
3799 ClassDB::bind_method(D_METHOD("get_path_to", "node", "use_unique_path"), &Node::get_path_to, DEFVAL(false));
3800 ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
3801 ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
3802 ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
3803 ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_index"), &Node::move_child);
3804 ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
3805 ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
3806 ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
3807 ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false));
3808 ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
3809 ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
3810 ClassDB::bind_method(D_METHOD("get_tree_string"), &Node::get_tree_string);
3811 ClassDB::bind_method(D_METHOD("get_tree_string_pretty"), &Node::get_tree_string_pretty);
3812 ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path);
3813 ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path);
3814 ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
3815 ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
3816 ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
3817 ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
3818 ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
3819 ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
3820 ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
3821 ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
3822 ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
3823 ClassDB::bind_method(D_METHOD("set_physics_process_priority", "priority"), &Node::set_physics_process_priority);
3824 ClassDB::bind_method(D_METHOD("get_physics_process_priority"), &Node::get_physics_process_priority);
3825 ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
3826 ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
3827 ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
3828 ClassDB::bind_method(D_METHOD("set_process_shortcut_input", "enable"), &Node::set_process_shortcut_input);
3829 ClassDB::bind_method(D_METHOD("is_processing_shortcut_input"), &Node::is_processing_shortcut_input);
3830 ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
3831 ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
3832 ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
3833 ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
3834 ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
3835 ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
3836 ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
3837
3838 ClassDB::bind_method(D_METHOD("set_process_thread_group", "mode"), &Node::set_process_thread_group);
3839 ClassDB::bind_method(D_METHOD("get_process_thread_group"), &Node::get_process_thread_group);
3840
3841 ClassDB::bind_method(D_METHOD("set_process_thread_messages", "flags"), &Node::set_process_thread_messages);
3842 ClassDB::bind_method(D_METHOD("get_process_thread_messages"), &Node::get_process_thread_messages);
3843
3844 ClassDB::bind_method(D_METHOD("set_process_thread_group_order", "order"), &Node::set_process_thread_group_order);
3845 ClassDB::bind_method(D_METHOD("get_process_thread_group_order"), &Node::get_process_thread_group_order);
3846
3847 ClassDB::bind_method(D_METHOD("queue_accessibility_update"), &Node::queue_accessibility_update);
3848 ClassDB::bind_method(D_METHOD("get_accessibility_element"), &Node::get_accessibility_element);
3849
3850 ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
3851 ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
3852
3853 ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
3854 ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
3855
3856 ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
3857 ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
3858
3859 ClassDB::bind_method(D_METHOD("set_physics_interpolation_mode", "mode"), &Node::set_physics_interpolation_mode);
3860 ClassDB::bind_method(D_METHOD("get_physics_interpolation_mode"), &Node::get_physics_interpolation_mode);
3861 ClassDB::bind_method(D_METHOD("is_physics_interpolated"), &Node::is_physics_interpolated);
3862 ClassDB::bind_method(D_METHOD("is_physics_interpolated_and_enabled"), &Node::is_physics_interpolated_and_enabled);
3863 ClassDB::bind_method(D_METHOD("reset_physics_interpolation"), &Node::reset_physics_interpolation);
3864
3865 ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "mode"), &Node::set_auto_translate_mode);
3866 ClassDB::bind_method(D_METHOD("get_auto_translate_mode"), &Node::get_auto_translate_mode);
3867 ClassDB::bind_method(D_METHOD("can_auto_translate"), &Node::can_auto_translate);
3868 ClassDB::bind_method(D_METHOD("set_translation_domain_inherited"), &Node::set_translation_domain_inherited);
3869
3870 ClassDB::bind_method(D_METHOD("get_window"), &Node::get_window);
3871 ClassDB::bind_method(D_METHOD("get_last_exclusive_window"), &Node::get_last_exclusive_window);
3872 ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
3873 ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
3874
3875 ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_DEFAULT));
3876 ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
3877
3878 ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
3879 ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
3880 ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
3881 ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
3882
3883 ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
3884
3885 ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_free);
3886
3887 ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
3888 ClassDB::bind_method(D_METHOD("is_node_ready"), &Node::is_ready);
3889
3890 ClassDB::bind_method(D_METHOD("set_multiplayer_authority", "id", "recursive"), &Node::set_multiplayer_authority, DEFVAL(true));
3891 ClassDB::bind_method(D_METHOD("get_multiplayer_authority"), &Node::get_multiplayer_authority);
3892
3893 ClassDB::bind_method(D_METHOD("is_multiplayer_authority"), &Node::is_multiplayer_authority);
3894
3895 ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
3896 ClassDB::bind_method(D_METHOD("rpc_config", "method", "config"), &Node::rpc_config);
3897 ClassDB::bind_method(D_METHOD("get_node_rpc_config"), &Node::_get_node_rpc_config_bind);
3898
3899 ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
3900 ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);
3901
3902 ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
3903 ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
3904
3905 ClassDB::bind_method(D_METHOD("atr", "message", "context"), &Node::atr, DEFVAL(""));
3906 ClassDB::bind_method(D_METHOD("atr_n", "message", "plural_message", "n", "context"), &Node::atr_n, DEFVAL(""));
3907
3908 #ifdef TOOLS_ENABLED
3909 ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
3910 #endif
3911
3912 {
3913 MethodInfo mi;
3914
3915 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3916
3917 mi.name = "rpc";
3918 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
3919 }
3920
3921 {
3922 MethodInfo mi;
3923
3924 mi.arguments.push_back(PropertyInfo(Variant::INT, "peer_id"));
3925 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3926
3927 mi.name = "rpc_id";
3928 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
3929 }
3930
3931 ClassDB::bind_method(D_METHOD("update_configuration_warnings"), &Node::update_configuration_warnings);
3932
3933 {
3934 MethodInfo mi;
3935 mi.name = "call_deferred_thread_group";
3936 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3937
3938 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred_thread_group", &Node::_call_deferred_thread_group_bind, mi, varray(), false);
3939 }
3940 ClassDB::bind_method(D_METHOD("set_deferred_thread_group", "property", "value"), &Node::set_deferred_thread_group);
3941 ClassDB::bind_method(D_METHOD("notify_deferred_thread_group", "what"), &Node::notify_deferred_thread_group);
3942
3943 {
3944 MethodInfo mi;
3945 mi.name = "call_thread_safe";
3946 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3947
3948 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_thread_safe", &Node::_call_thread_safe_bind, mi, varray(), false);
3949 }
3950 ClassDB::bind_method(D_METHOD("set_thread_safe", "property", "value"), &Node::set_thread_safe);
3951 ClassDB::bind_method(D_METHOD("notify_thread_safe", "what"), &Node::notify_thread_safe);
3952
3953 BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
3954 BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
3955 BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
3956 BIND_CONSTANT(NOTIFICATION_READY);
3957 BIND_CONSTANT(NOTIFICATION_PAUSED);
3958 BIND_CONSTANT(NOTIFICATION_UNPAUSED);
3959 BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
3960 BIND_CONSTANT(NOTIFICATION_PROCESS);
3961 BIND_CONSTANT(NOTIFICATION_PARENTED);
3962 BIND_CONSTANT(NOTIFICATION_UNPARENTED);
3963 BIND_CONSTANT(NOTIFICATION_SCENE_INSTANTIATED);
3964 BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
3965 BIND_CONSTANT(NOTIFICATION_DRAG_END);
3966 BIND_CONSTANT(NOTIFICATION_PATH_RENAMED);
3967 BIND_CONSTANT(NOTIFICATION_CHILD_ORDER_CHANGED);
3968 BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
3969 BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
3970 BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
3971 BIND_CONSTANT(NOTIFICATION_DISABLED);
3972 BIND_CONSTANT(NOTIFICATION_ENABLED);
3973 BIND_CONSTANT(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
3974
3975 BIND_CONSTANT(NOTIFICATION_EDITOR_PRE_SAVE);
3976 BIND_CONSTANT(NOTIFICATION_EDITOR_POST_SAVE);
3977
3978 BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
3979 BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
3980 BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_IN);
3981 BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_OUT);
3982 BIND_CONSTANT(NOTIFICATION_WM_CLOSE_REQUEST);
3983 BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
3984 BIND_CONSTANT(NOTIFICATION_WM_SIZE_CHANGED);
3985 BIND_CONSTANT(NOTIFICATION_WM_DPI_CHANGE);
3986 BIND_CONSTANT(NOTIFICATION_VP_MOUSE_ENTER);
3987 BIND_CONSTANT(NOTIFICATION_VP_MOUSE_EXIT);
3988 BIND_CONSTANT(NOTIFICATION_WM_POSITION_CHANGED);
3989 BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
3990 BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
3991 BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
3992 BIND_CONSTANT(NOTIFICATION_CRASH);
3993 BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
3994 BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
3995 BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
3996 BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
3997 BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
3998 BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
3999
4000 BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_UPDATE);
4001 BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_INVALIDATE);
4002
4003 BIND_ENUM_CONSTANT(PROCESS_MODE_INHERIT);
4004 BIND_ENUM_CONSTANT(PROCESS_MODE_PAUSABLE);
4005 BIND_ENUM_CONSTANT(PROCESS_MODE_WHEN_PAUSED);
4006 BIND_ENUM_CONSTANT(PROCESS_MODE_ALWAYS);
4007 BIND_ENUM_CONSTANT(PROCESS_MODE_DISABLED);
4008
4009 BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_INHERIT);
4010 BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_MAIN_THREAD);
4011 BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_SUB_THREAD);
4012
4013 BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES);
4014 BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_PHYSICS);
4015 BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_ALL);
4016
4017 BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_INHERIT);
4018 BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_ON);
4019 BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_OFF);
4020
4021 BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
4022 BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
4023 BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
4024 BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION);
4025 BIND_ENUM_CONSTANT(DUPLICATE_INTERNAL_STATE);
4026 BIND_ENUM_CONSTANT(DUPLICATE_DEFAULT);
4027
4028 BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
4029 BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
4030 BIND_ENUM_CONSTANT(INTERNAL_MODE_BACK);
4031
4032 BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_INHERIT);
4033 BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_ALWAYS);
4034 BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_DISABLED);
4035
4036 ADD_SIGNAL(MethodInfo("ready"));
4037 ADD_SIGNAL(MethodInfo("renamed"));
4038 ADD_SIGNAL(MethodInfo("tree_entered"));
4039 ADD_SIGNAL(MethodInfo("tree_exiting"));
4040 ADD_SIGNAL(MethodInfo("tree_exited"));
4041 ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4042 ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4043
4044 ADD_SIGNAL(MethodInfo("child_order_changed"));
4045 ADD_SIGNAL(MethodInfo("replacing_by", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4046 ADD_SIGNAL(MethodInfo("editor_description_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4047 ADD_SIGNAL(MethodInfo("editor_state_changed"));
4048
4049 ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
4050 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_unique_name_in_owner", "is_unique_name_in_owner");
4051 ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
4052 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_owner", "get_owner");
4053 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "", "get_multiplayer");
4054
4055 ADD_GROUP("Process", "process_");
4056 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
4057 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
4058 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_physics_priority"), "set_physics_process_priority", "get_physics_process_priority");
4059
4060 ADD_SUBGROUP("Thread Group", "process_thread");
4061 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group", PROPERTY_HINT_ENUM, "Inherit,Main Thread,Sub Thread"), "set_process_thread_group", "get_process_thread_group");
4062 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group_order"), "set_process_thread_group_order", "get_process_thread_group_order");
4063 ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_messages", PROPERTY_HINT_FLAGS, "Process,Physics Process"), "set_process_thread_messages", "get_process_thread_messages");
4064
4065 ADD_GROUP("Physics Interpolation", "physics_interpolation_");
4066 ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_mode", PROPERTY_HINT_ENUM, "Inherit,On,Off"), "set_physics_interpolation_mode", "get_physics_interpolation_mode");
4067
4068 ADD_GROUP("Auto Translate", "auto_translate_");
4069 ADD_PROPERTY(PropertyInfo(Variant::INT, "auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_auto_translate_mode", "get_auto_translate_mode");
4070
4071 ADD_GROUP("Editor Description", "editor_");
4072 ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
4073
4074 GDVIRTUAL_BIND(_process, "delta");
4075 GDVIRTUAL_BIND(_physics_process, "delta");
4076 GDVIRTUAL_BIND(_enter_tree);
4077 GDVIRTUAL_BIND(_exit_tree);
4078 GDVIRTUAL_BIND(_ready);
4079 GDVIRTUAL_BIND(_get_configuration_warnings);
4080 GDVIRTUAL_BIND(_get_accessibility_configuration_warnings);
4081 GDVIRTUAL_BIND(_input, "event");
4082 GDVIRTUAL_BIND(_shortcut_input, "event");
4083 GDVIRTUAL_BIND(_unhandled_input, "event");
4084 GDVIRTUAL_BIND(_unhandled_key_input, "event");
4085 GDVIRTUAL_BIND(_get_focused_accessibility_element);
4086 }
4087
4088 String Node::_get_name_num_separator() {
4089 switch (GLOBAL_GET("editor/naming/node_name_num_separator").operator int()) {
4090 case 0:
4091 return "";
4092 case 1:
4093 return " ";
4094 case 2:
4095 return "_";
4096 case 3:
4097 return "-";
4098 }
4099 return " ";
4100 }
4101
4102 Node::Node() {
4103 _define_ancestry(AncestralClass::NODE);
4104 #ifdef DEBUG_ENABLED
4105 total_node_count.increment();
4106 #endif
4107 // Default member initializer for bitfield is a C++20 extension, so:
4108
4109 data.process_mode = PROCESS_MODE_INHERIT;
4110 data.physics_interpolation_mode = PHYSICS_INTERPOLATION_MODE_INHERIT;
4111
4112 data.physics_process = false;
4113 data.process = false;
4114
4115 data.physics_process_internal = false;
4116 data.process_internal = false;
4117
4118 data.input = false;
4119 data.shortcut_input = false;
4120 data.unhandled_input = false;
4121 data.unhandled_key_input = false;
4122
4123 data.physics_interpolated = true;
4124 data.physics_interpolation_reset_requested = false;
4125 data.physics_interpolated_client_side = false;
4126 data.use_identity_transform = false;
4127
4128 data.use_placeholder = false;
4129
4130 data.display_folded = false;
4131 data.editable_instance = false;
4132
4133 data.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.
4134 data.ready_first = true;
4135
4136 data.auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
4137 data.is_auto_translating = true;
4138 data.is_auto_translate_dirty = true;
4139
4140 data.is_translation_domain_inherited = true;
4141 data.is_translation_domain_dirty = true;
4142 }
4143
4144 Node::~Node() {
4145 data.grouped.clear();
4146 data.owned.clear();
4147 data.children.clear();
4148 data.children_cache.clear();
4149
4150 ERR_FAIL_COND(data.parent);
4151 ERR_FAIL_COND(data.children_cache.size());
4152
4153 #ifdef DEBUG_ENABLED
4154 total_node_count.decrement();
4155 #endif
4156 }
4157
4158 ////////////////////////////////
4159 // Multithreaded locked version of Object functions.
4160
4161 #ifdef DEBUG_ENABLED
4162
4163 void Node::set_script(const Variant &p_script) {
4164 ERR_THREAD_GUARD;
4165 Object::set_script(p_script);
4166 }
4167
4168 Variant Node::get_script() const {
4169 ERR_THREAD_GUARD_V(Variant());
4170 return Object::get_script();
4171 }
4172
4173 bool Node::has_meta(const StringName &p_name) const {
4174 ERR_THREAD_GUARD_V(false);
4175 return Object::has_meta(p_name);
4176 }
4177
4178 void Node::set_meta(const StringName &p_name, const Variant &p_value) {
4179 ERR_THREAD_GUARD;
4180 Object::set_meta(p_name, p_value);
4181 _emit_editor_state_changed();
4182 }
4183
4184 void Node::remove_meta(const StringName &p_name) {
4185 ERR_THREAD_GUARD;
4186 Object::remove_meta(p_name);
4187 _emit_editor_state_changed();
4188 }
4189
4190 Variant Node::get_meta(const StringName &p_name, const Variant &p_default) const {
4191 ERR_THREAD_GUARD_V(Variant());
4192 return Object::get_meta(p_name, p_default);
4193 }
4194
4195 void Node::get_meta_list(List<StringName> *p_list) const {
4196 ERR_THREAD_GUARD;
4197 Object::get_meta_list(p_list);
4198 }
4199
4200 Error Node::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
4201 ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4202 return Object::emit_signalp(p_name, p_args, p_argcount);
4203 }
4204
4205 bool Node::has_signal(const StringName &p_name) const {
4206 ERR_THREAD_GUARD_V(false);
4207 return Object::has_signal(p_name);
4208 }
4209
4210 void Node::get_signal_list(List<MethodInfo> *p_signals) const {
4211 ERR_THREAD_GUARD;
4212 Object::get_signal_list(p_signals);
4213 }
4214
4215 void Node::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
4216 ERR_THREAD_GUARD;
4217 Object::get_signal_connection_list(p_signal, p_connections);
4218 }
4219
4220 void Node::get_all_signal_connections(List<Connection> *p_connections) const {
4221 ERR_THREAD_GUARD;
4222 Object::get_all_signal_connections(p_connections);
4223 }
4224
4225 int Node::get_persistent_signal_connection_count() const {
4226 ERR_THREAD_GUARD_V(0);
4227 return Object::get_persistent_signal_connection_count();
4228 }
4229
4230 uint32_t Node::get_signal_connection_flags(const StringName &p_signal, const Callable &p_callable) const {
4231 ERR_THREAD_GUARD_V(0);
4232 return Object::get_signal_connection_flags(p_signal, p_callable);
4233 }
4234
4235 void Node::get_signals_connected_to_this(List<Connection> *p_connections) const {
4236 ERR_THREAD_GUARD;
4237 Object::get_signals_connected_to_this(p_connections);
4238 }
4239
4240 Error Node::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
4241 ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4242
4243 Error retval = Object::connect(p_signal, p_callable, p_flags);
4244 #ifdef TOOLS_ENABLED
4245 if (p_flags & CONNECT_PERSIST) {
4246 _emit_editor_state_changed();
4247 }
4248 #endif
4249
4250 return retval;
4251 }
4252
4253 void Node::disconnect(const StringName &p_signal, const Callable &p_callable) {
4254 ERR_THREAD_GUARD;
4255
4256 #ifdef TOOLS_ENABLED
4257 // Already under thread guard, don't check again.
4258 uint32_t connection_flags = Object::get_signal_connection_flags(p_signal, p_callable);
4259 #endif
4260
4261 [[maybe_unused]] bool changed = Object::_disconnect(p_signal, p_callable);
4262
4263 #ifdef TOOLS_ENABLED
4264 if (changed && connection_flags & CONNECT_PERSIST) {
4265 _emit_editor_state_changed();
4266 }
4267 #endif
4268 }
4269
4270 bool Node::is_connected(const StringName &p_signal, const Callable &p_callable) const {
4271 ERR_THREAD_GUARD_V(false);
4272 return Object::is_connected(p_signal, p_callable);
4273 }
4274
4275 bool Node::has_connections(const StringName &p_signal) const {
4276 ERR_THREAD_GUARD_V(false);
4277 return Object::has_connections(p_signal);
4278 }
4279
4280 #endif