Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/node.cpp
20832 views
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
2871
if (get_name() != String()) {
2872
node->set_name(get_name());
2873
}
2874
2875
#ifdef TOOLS_ENABLED
2876
if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
2877
r_duplimap->insert(this, node);
2878
}
2879
#endif
2880
2881
if (p_flags & DUPLICATE_GROUPS) {
2882
List<GroupInfo> gi;
2883
get_groups(&gi);
2884
for (const GroupInfo &E : gi) {
2885
#ifdef TOOLS_ENABLED
2886
if ((p_flags & DUPLICATE_FROM_EDITOR) && !E.persistent) {
2887
continue;
2888
}
2889
#endif
2890
2891
node->add_to_group(E.name, E.persistent);
2892
}
2893
}
2894
2895
for (int i = 0; i < get_child_count(false); i++) {
2896
if (instantiated && get_child(i, false)->data.owner == this) {
2897
continue; //part of instance
2898
}
2899
2900
Node *dup = get_child(i, false)->_duplicate(p_flags, r_duplimap);
2901
if (!dup) {
2902
memdelete(node);
2903
return nullptr;
2904
}
2905
2906
node->add_child(dup);
2907
if (i < node->get_child_count(false) - 1) {
2908
node->move_child(dup, i);
2909
}
2910
}
2911
2912
for (const Node *&E : hidden_roots) {
2913
Node *parent = node->get_node(get_path_to(E->data.parent));
2914
if (!parent) {
2915
memdelete(node);
2916
return nullptr;
2917
}
2918
2919
Node *dup = E->_duplicate(p_flags, r_duplimap);
2920
if (!dup) {
2921
memdelete(node);
2922
return nullptr;
2923
}
2924
2925
parent->add_child(dup);
2926
int pos = E->get_index(false);
2927
2928
if (pos < parent->get_child_count(false) - 1) {
2929
parent->move_child(dup, pos);
2930
}
2931
}
2932
return node;
2933
}
2934
2935
Node *Node::duplicate(int p_flags) const {
2936
ERR_THREAD_GUARD_V(nullptr);
2937
Node *dupe = _duplicate(p_flags);
2938
2939
ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2940
2941
if (p_flags & DUPLICATE_SCRIPTS) {
2942
_duplicate_scripts(this, dupe);
2943
}
2944
2945
_duplicate_properties(this, this, dupe, p_flags);
2946
2947
if (p_flags & DUPLICATE_SIGNALS) {
2948
_duplicate_signals(this, dupe);
2949
}
2950
2951
return dupe;
2952
}
2953
2954
#ifdef TOOLS_ENABLED
2955
Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const {
2956
HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> tmp;
2957
return duplicate_from_editor(r_duplimap, nullptr, tmp);
2958
}
2959
2960
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 {
2961
int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR;
2962
Node *dupe = _duplicate(flags, &r_duplimap);
2963
2964
ERR_FAIL_NULL_V_MSG(dupe, nullptr, "Failed to duplicate node.");
2965
2966
if (flags & DUPLICATE_SCRIPTS) {
2967
_duplicate_scripts(this, dupe);
2968
}
2969
2970
_duplicate_properties(this, this, dupe, flags);
2971
2972
// This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
2973
if (p_scene_root) {
2974
remap_node_resources(dupe, p_scene_root, p_resource_remap);
2975
}
2976
2977
// Duplication of signals must happen after all the node descendants have been copied,
2978
// because re-targeting of connections from some descendant to another is not possible
2979
// if the emitter node comes later in tree order than the receiver
2980
_duplicate_signals(this, dupe);
2981
2982
return dupe;
2983
}
2984
2985
void Node::remap_node_resources(Node *p_node, Node *p_scene_root, HashMap<Node *, HashMap<Ref<Resource>, Ref<Resource>>> &p_resource_remap) const {
2986
Node *local_scene = p_node->is_instance() ? p_node : (p_node->get_owner() ? p_node->get_owner() : p_scene_root);
2987
2988
List<PropertyInfo> props;
2989
p_node->get_property_list(&props);
2990
2991
for (const PropertyInfo &E : props) {
2992
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
2993
continue;
2994
}
2995
2996
Variant v = p_node->get(E.name);
2997
if (!v.is_ref_counted()) {
2998
continue;
2999
}
3000
Ref<Resource> res = v;
3001
if (res.is_null()) {
3002
continue;
3003
}
3004
3005
if (res->is_local_to_scene()) {
3006
if (local_scene == res->get_local_scene()) {
3007
continue;
3008
}
3009
Ref<Resource> dup = SceneState::get_remap_resource(res, p_resource_remap, nullptr, local_scene);
3010
p_node->set(E.name, dup);
3011
continue;
3012
}
3013
3014
if (res->is_built_in()) {
3015
// Use nullptr instead of a specific node (current scene root node) to represent the scene,
3016
// as the Make Scene Root operation may be executed.
3017
if (p_resource_remap[nullptr].has(res)) {
3018
p_node->set(E.name, p_resource_remap[nullptr][res]);
3019
remap_nested_resources(res, p_resource_remap[nullptr]);
3020
}
3021
}
3022
}
3023
3024
for (int i = 0; i < p_node->get_child_count(); i++) {
3025
remap_node_resources(p_node->get_child(i), p_scene_root, p_resource_remap);
3026
}
3027
}
3028
3029
void Node::remap_nested_resources(Ref<Resource> p_resource, HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
3030
List<PropertyInfo> props;
3031
p_resource->get_property_list(&props);
3032
3033
for (const PropertyInfo &E : props) {
3034
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
3035
continue;
3036
}
3037
3038
Variant v = p_resource->get(E.name);
3039
if (v.is_ref_counted()) {
3040
Ref<Resource> res = v;
3041
if (res.is_valid()) {
3042
if (p_resource_remap.has(res)) {
3043
p_resource->set(E.name, p_resource_remap[res]);
3044
remap_nested_resources(res, p_resource_remap);
3045
}
3046
}
3047
}
3048
}
3049
}
3050
3051
void Node::_emit_editor_state_changed() {
3052
// This is required for the SceneTreeEditor to properly keep track of when an update is needed.
3053
// This signal might be expensive and not needed for anything outside of the editor.
3054
if (Engine::get_singleton()->is_editor_hint()) {
3055
emit_signal(SNAME("editor_state_changed"));
3056
}
3057
}
3058
#endif
3059
3060
void Node::_duplicate_scripts(const Node *p_original, Node *p_copy) const {
3061
bool is_valid = false;
3062
Variant scr = p_original->get(CoreStringName(script), &is_valid);
3063
if (is_valid) {
3064
p_copy->set(CoreStringName(script), scr);
3065
}
3066
3067
for (int i = 0; i < p_original->get_child_count(false); i++) {
3068
Node *copy_child = p_copy->get_child(i, false);
3069
ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
3070
_duplicate_scripts(p_original->get_child(i, false), copy_child);
3071
}
3072
}
3073
3074
// Duplicate node's properties.
3075
// This has to be called after nodes have been duplicated since there might be properties
3076
// of type Node that can be updated properly only if duplicated node tree is complete.
3077
void Node::_duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const {
3078
List<PropertyInfo> props;
3079
p_original->get_property_list(&props);
3080
const StringName &script_property_name = CoreStringName(script);
3081
for (const PropertyInfo &E : props) {
3082
if (!(p_flags & DUPLICATE_INTERNAL_STATE) && !(E.usage & PROPERTY_USAGE_STORAGE)) {
3083
continue;
3084
}
3085
const StringName name = E.name;
3086
3087
if (name == script_property_name) {
3088
continue;
3089
}
3090
3091
Variant value = p_original->get(name);
3092
// To keep classic behavior, because, in contrast, nowadays a resource would be duplicated.
3093
if (value.get_type() != Variant::OBJECT) {
3094
value = value.duplicate(true);
3095
}
3096
3097
if (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE) {
3098
Resource *res = Object::cast_to<Resource>(value);
3099
if (res) { // Duplicate only if it's a resource
3100
p_copy->set(name, res->duplicate());
3101
}
3102
} else {
3103
if (value.get_type() == Variant::OBJECT) {
3104
Node *property_node = Object::cast_to<Node>(value);
3105
Variant out_value = value;
3106
if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3107
out_value = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3108
}
3109
p_copy->set(name, out_value);
3110
} else if (value.get_type() == Variant::ARRAY) {
3111
Array arr = value;
3112
if (arr.get_typed_builtin() == Variant::OBJECT) {
3113
for (int i = 0; i < arr.size(); i++) {
3114
Node *property_node = Object::cast_to<Node>(arr[i]);
3115
if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
3116
arr[i] = p_copy->get_node_or_null(p_original->get_path_to(property_node));
3117
}
3118
}
3119
}
3120
p_copy->set(name, arr);
3121
} else {
3122
p_copy->set(name, value);
3123
}
3124
}
3125
}
3126
3127
for (int i = 0; i < p_original->get_child_count(false); i++) {
3128
Node *copy_child = p_copy->get_child(i, false);
3129
ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
3130
_duplicate_properties(p_root, p_original->get_child(i, false), copy_child, p_flags);
3131
}
3132
}
3133
3134
// Duplication of signals must happen after all the node descendants have been copied,
3135
// because re-targeting of connections from some descendant to another is not possible
3136
// if the emitter node comes later in tree order than the receiver
3137
void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
3138
if ((this != p_original) && !(p_original->is_ancestor_of(this))) {
3139
return;
3140
}
3141
3142
List<const Node *> process_list;
3143
process_list.push_back(this);
3144
while (!process_list.is_empty()) {
3145
const Node *n = process_list.front()->get();
3146
process_list.pop_front();
3147
3148
List<Connection> conns;
3149
n->get_all_signal_connections(&conns);
3150
3151
for (const Connection &E : conns) {
3152
if (E.flags & CONNECT_PERSIST) {
3153
//user connected
3154
NodePath p = p_original->get_path_to(n);
3155
Node *copy = p_copy->get_node(p);
3156
3157
Node *target = Object::cast_to<Node>(E.callable.get_object());
3158
if (!target) {
3159
continue;
3160
}
3161
3162
NodePath ptarget = p_original->get_path_to(target);
3163
if (ptarget.is_empty()) {
3164
continue;
3165
}
3166
3167
Node *copytarget = target;
3168
3169
// Attempt to find a path to the duplicate target, if it seems it's not part
3170
// of the duplicated and not yet parented hierarchy then at least try to connect
3171
// to the same target as the original
3172
3173
if (p_copy->has_node(ptarget)) {
3174
copytarget = p_copy->get_node(ptarget);
3175
}
3176
3177
if (copy && copytarget && E.callable.get_method() != StringName()) {
3178
Callable copy_callable = Callable(copytarget, E.callable.get_method());
3179
if (!copy->is_connected(E.signal.get_name(), copy_callable)) {
3180
int unbound_arg_count = E.callable.get_unbound_arguments_count();
3181
if (unbound_arg_count > 0) {
3182
copy_callable = copy_callable.unbind(unbound_arg_count);
3183
}
3184
if (E.callable.get_bound_arguments_count() > 0) {
3185
copy_callable = copy_callable.bindv(E.callable.get_bound_arguments());
3186
}
3187
copy->connect(E.signal.get_name(), copy_callable, E.flags);
3188
}
3189
}
3190
}
3191
}
3192
3193
for (int i = 0; i < n->get_child_count(); i++) {
3194
process_list.push_back(n->get_child(i));
3195
}
3196
}
3197
}
3198
3199
static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
3200
if (p_node->get_owner() == p_by) {
3201
p_owned->push_back(p_node);
3202
}
3203
3204
for (int i = 0; i < p_node->get_child_count(); i++) {
3205
find_owned_by(p_by, p_node->get_child(i), p_owned);
3206
}
3207
}
3208
3209
void Node::replace_by(RequiredParam<Node> rp_node, bool p_keep_groups) {
3210
ERR_THREAD_GUARD
3211
EXTRACT_PARAM_OR_FAIL(p_node, rp_node);
3212
ERR_FAIL_COND(p_node->data.parent);
3213
3214
List<Node *> owned = data.owned;
3215
List<Node *> owned_by_owner;
3216
Node *owner = (data.owner == this) ? p_node : data.owner;
3217
3218
if (p_keep_groups) {
3219
List<GroupInfo> groups;
3220
get_groups(&groups);
3221
3222
for (const GroupInfo &E : groups) {
3223
p_node->add_to_group(E.name, E.persistent);
3224
}
3225
}
3226
3227
_replace_connections_target(p_node);
3228
3229
if (data.owner) {
3230
for (int i = 0; i < get_child_count(); i++) {
3231
find_owned_by(data.owner, get_child(i), &owned_by_owner);
3232
}
3233
3234
_clean_up_owner();
3235
}
3236
3237
Node *parent = data.parent;
3238
int index_in_parent = get_index(false);
3239
3240
if (data.parent) {
3241
parent->remove_child(this);
3242
parent->add_child(p_node);
3243
parent->move_child(p_node, index_in_parent);
3244
}
3245
3246
emit_signal(SNAME("replacing_by"), p_node);
3247
3248
while (get_child_count()) {
3249
Node *child = get_child(0);
3250
remove_child(child);
3251
if (!child->is_internal()) {
3252
// Add the custom children to the p_node.
3253
Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
3254
child->set_owner(nullptr);
3255
p_node->add_child(child);
3256
child->set_owner(child_owner);
3257
}
3258
}
3259
3260
p_node->set_owner(owner);
3261
for (Node *E : owned) {
3262
if (E->data.owner != p_node) {
3263
E->set_owner(p_node);
3264
}
3265
}
3266
3267
for (Node *E : owned_by_owner) {
3268
if (E->data.owner != owner) {
3269
E->set_owner(owner);
3270
}
3271
}
3272
3273
p_node->set_scene_file_path(get_scene_file_path());
3274
}
3275
3276
void Node::_replace_connections_target(Node *p_new_target) {
3277
List<Connection> cl;
3278
get_signals_connected_to_this(&cl);
3279
3280
for (const Connection &c : cl) {
3281
if (c.flags & CONNECT_PERSIST) {
3282
c.signal.get_object()->disconnect(c.signal.get_name(), Callable(this, c.callable.get_method()));
3283
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());
3284
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()));
3285
c.signal.get_object()->connect(c.signal.get_name(), Callable(p_new_target, c.callable.get_method()), c.flags);
3286
}
3287
}
3288
}
3289
3290
bool Node::has_node_and_resource(const NodePath &p_path) const {
3291
ERR_THREAD_GUARD_V(false);
3292
if (!has_node(p_path)) {
3293
return false;
3294
}
3295
Ref<Resource> res;
3296
Vector<StringName> leftover_path;
3297
Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3298
3299
return node;
3300
}
3301
3302
Array Node::_get_node_and_resource(const NodePath &p_path) {
3303
Ref<Resource> res;
3304
Vector<StringName> leftover_path;
3305
Node *node = get_node_and_resource(p_path, res, leftover_path, false);
3306
Array result;
3307
3308
if (node) {
3309
result.push_back(node);
3310
} else {
3311
result.push_back(Variant());
3312
}
3313
3314
if (res.is_valid()) {
3315
result.push_back(res);
3316
} else {
3317
result.push_back(Variant());
3318
}
3319
3320
result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
3321
3322
return result;
3323
}
3324
3325
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 {
3326
ERR_THREAD_GUARD_V(nullptr);
3327
r_res = Ref<Resource>();
3328
r_leftover_subpath = Vector<StringName>();
3329
Node *node = get_node_or_null(p_path);
3330
if (!node) {
3331
return nullptr;
3332
}
3333
3334
if (p_path.get_subname_count()) {
3335
int j = 0;
3336
// If not p_last_is_property, we shouldn't consider the last one as part of the resource
3337
for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
3338
bool is_valid = false;
3339
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);
3340
3341
if (!is_valid) { // Found nothing on that path
3342
return nullptr;
3343
}
3344
3345
Ref<Resource> new_res = new_res_v;
3346
3347
if (new_res.is_null()) { // No longer a resource, assume property
3348
break;
3349
}
3350
3351
r_res = new_res;
3352
}
3353
for (; j < p_path.get_subname_count(); j++) {
3354
// Put the rest of the subpath in the leftover path
3355
r_leftover_subpath.push_back(p_path.get_subname(j));
3356
}
3357
}
3358
3359
return node;
3360
}
3361
3362
void Node::_set_tree(SceneTree *p_tree) {
3363
SceneTree *tree_changed_a = nullptr;
3364
SceneTree *tree_changed_b = nullptr;
3365
3366
//ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
3367
3368
if (data.tree) {
3369
_propagate_exit_tree();
3370
3371
tree_changed_a = data.tree;
3372
}
3373
3374
data.tree = p_tree;
3375
3376
if (data.tree) {
3377
_propagate_enter_tree();
3378
if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
3379
_propagate_ready(); //reverse_notification(NOTIFICATION_READY);
3380
}
3381
3382
tree_changed_b = data.tree;
3383
}
3384
3385
if (tree_changed_a) {
3386
tree_changed_a->tree_changed();
3387
}
3388
if (tree_changed_b) {
3389
tree_changed_b->tree_changed();
3390
}
3391
}
3392
3393
#ifdef DEBUG_ENABLED
3394
static HashMap<ObjectID, List<String>> _print_orphan_nodes_map;
3395
3396
static void _print_orphan_nodes_routine(Object *p_obj, void *p_user_data) {
3397
Node *n = Object::cast_to<Node>(p_obj);
3398
if (!n) {
3399
return;
3400
}
3401
3402
if (n->is_inside_tree()) {
3403
return;
3404
}
3405
3406
Node *p = n;
3407
while (p->get_parent()) {
3408
p = p->get_parent();
3409
}
3410
3411
String path;
3412
if (p == n) {
3413
path = n->get_name();
3414
} else {
3415
path = String(p->get_name()) + "/" + String(p->get_path_to(n));
3416
}
3417
3418
String source;
3419
Variant script = n->get_script();
3420
if (!script.is_null()) {
3421
Resource *obj = Object::cast_to<Resource>(script);
3422
source = obj->get_path();
3423
}
3424
3425
List<String> info_strings;
3426
info_strings.push_back(path);
3427
info_strings.push_back(n->get_class());
3428
info_strings.push_back(source);
3429
3430
_print_orphan_nodes_map[p_obj->get_instance_id()] = info_strings;
3431
}
3432
#endif // DEBUG_ENABLED
3433
3434
void Node::print_orphan_nodes() {
3435
#ifdef DEBUG_ENABLED
3436
// Make sure it's empty.
3437
_print_orphan_nodes_map.clear();
3438
3439
// Collect and print information about orphan nodes.
3440
ObjectDB::debug_objects(_print_orphan_nodes_routine, nullptr);
3441
3442
for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3443
print_line(itos(E.key) + " - Stray Node: " + E.value.get(0) + " (Type: " + E.value.get(1) + ") (Source:" + E.value.get(2) + ")");
3444
}
3445
3446
// Flush it after use.
3447
_print_orphan_nodes_map.clear();
3448
#endif
3449
}
3450
TypedArray<int> Node::get_orphan_node_ids() {
3451
TypedArray<int> ret;
3452
#ifdef DEBUG_ENABLED
3453
// Make sure it's empty.
3454
_print_orphan_nodes_map.clear();
3455
3456
// Collect and return information about orphan nodes.
3457
ObjectDB::debug_objects(_print_orphan_nodes_routine, nullptr);
3458
3459
for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
3460
ret.push_back(E.key);
3461
}
3462
3463
// Flush it after use.
3464
_print_orphan_nodes_map.clear();
3465
#endif
3466
return ret;
3467
}
3468
3469
void Node::queue_free() {
3470
// There are users which instantiate multiple scene trees for their games.
3471
// Use the node's own tree to handle its deletion when relevant.
3472
if (data.tree) {
3473
data.tree->queue_delete(this);
3474
} else {
3475
SceneTree *tree = SceneTree::get_singleton();
3476
ERR_FAIL_NULL_MSG(tree, "Can't queue free a node when no SceneTree is available.");
3477
tree->queue_delete(this);
3478
}
3479
}
3480
3481
#ifdef TOOLS_ENABLED
3482
static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
3483
if (p_node != p_base && !p_node->get_owner()) {
3484
return;
3485
}
3486
if (p_node->is_unique_name_in_owner() && p_node->get_owner() == p_base) {
3487
String n = "%" + p_node->get_name();
3488
r_options->push_back(n.quote());
3489
}
3490
String n = String(p_base->get_path_to(p_node));
3491
r_options->push_back(n.quote());
3492
for (int i = 0; i < p_node->get_child_count(); i++) {
3493
_add_nodes_to_options(p_base, p_node->get_child(i), r_options);
3494
}
3495
}
3496
3497
void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
3498
const String pf = p_function;
3499
if (p_idx == 0 && (pf == "has_node" || pf == "get_node" || pf == "get_node_or_null")) {
3500
_add_nodes_to_options(this, this, r_options);
3501
} else if (p_idx == 0 && (pf == "add_to_group" || pf == "remove_from_group" || pf == "is_in_group")) {
3502
HashMap<StringName, String> global_groups(ProjectSettings::get_singleton()->get_global_groups_list());
3503
for (const KeyValue<StringName, String> &E : global_groups) {
3504
r_options->push_back(E.key.operator String().quote());
3505
}
3506
}
3507
Object::get_argument_options(p_function, p_idx, r_options);
3508
}
3509
#endif
3510
3511
void Node::clear_internal_tree_resource_paths() {
3512
clear_internal_resource_paths();
3513
for (KeyValue<StringName, Node *> &K : data.children) {
3514
K.value->clear_internal_tree_resource_paths();
3515
}
3516
}
3517
3518
PackedStringArray Node::get_accessibility_configuration_warnings() const {
3519
ERR_THREAD_GUARD_V(PackedStringArray());
3520
PackedStringArray ret;
3521
3522
Vector<String> warnings;
3523
if (GDVIRTUAL_CALL(_get_accessibility_configuration_warnings, warnings)) {
3524
ret.append_array(warnings);
3525
}
3526
3527
return ret;
3528
}
3529
3530
PackedStringArray Node::get_configuration_warnings() const {
3531
ERR_THREAD_GUARD_V(PackedStringArray());
3532
PackedStringArray ret;
3533
3534
Vector<String> warnings;
3535
if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
3536
ret.append_array(warnings);
3537
}
3538
3539
return ret;
3540
}
3541
3542
void Node::update_configuration_warnings() {
3543
ERR_THREAD_GUARD
3544
#ifdef TOOLS_ENABLED
3545
if (!data.tree) {
3546
return;
3547
}
3548
if (data.tree->get_edited_scene_root() && (data.tree->get_edited_scene_root() == this || data.tree->get_edited_scene_root()->is_ancestor_of(this))) {
3549
data.tree->emit_signal(SceneStringName(node_configuration_warning_changed), this);
3550
}
3551
#endif
3552
}
3553
3554
void Node::set_display_folded(bool p_folded) {
3555
ERR_THREAD_GUARD
3556
data.display_folded = p_folded;
3557
}
3558
3559
bool Node::is_displayed_folded() const {
3560
return data.display_folded;
3561
}
3562
3563
bool Node::is_ready() const {
3564
return !data.ready_first;
3565
}
3566
3567
void Node::request_ready() {
3568
ERR_THREAD_GUARD
3569
data.ready_first = true;
3570
}
3571
3572
void Node::_call_input(const Ref<InputEvent> &p_event) {
3573
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3574
GDVIRTUAL_CALL(_input, p_event);
3575
}
3576
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3577
return;
3578
}
3579
input(p_event);
3580
}
3581
3582
void Node::_call_shortcut_input(const Ref<InputEvent> &p_event) {
3583
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3584
GDVIRTUAL_CALL(_shortcut_input, p_event);
3585
}
3586
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3587
return;
3588
}
3589
shortcut_input(p_event);
3590
}
3591
3592
void Node::_call_unhandled_input(const Ref<InputEvent> &p_event) {
3593
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3594
GDVIRTUAL_CALL(_unhandled_input, p_event);
3595
}
3596
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3597
return;
3598
}
3599
unhandled_input(p_event);
3600
}
3601
3602
void Node::_call_unhandled_key_input(const Ref<InputEvent> &p_event) {
3603
if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
3604
GDVIRTUAL_CALL(_unhandled_key_input, p_event);
3605
}
3606
if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
3607
return;
3608
}
3609
unhandled_key_input(p_event);
3610
}
3611
3612
void Node::_validate_property(PropertyInfo &p_property) const {
3613
if ((p_property.name == "process_thread_group_order" || p_property.name == "process_thread_messages") && data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
3614
p_property.usage = 0;
3615
}
3616
}
3617
3618
String Node::_to_string() {
3619
ERR_THREAD_GUARD_V(String());
3620
return (get_name() ? String(get_name()) + ":" : "") + Object::_to_string();
3621
}
3622
3623
void Node::input(const Ref<InputEvent> &p_event) {
3624
}
3625
3626
void Node::shortcut_input(const Ref<InputEvent> &p_key_event) {
3627
}
3628
3629
void Node::unhandled_input(const Ref<InputEvent> &p_event) {
3630
}
3631
3632
void Node::unhandled_key_input(const Ref<InputEvent> &p_key_event) {
3633
}
3634
3635
Variant Node::_call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3636
if (p_argcount < 1) {
3637
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3638
r_error.expected = 1;
3639
return Variant();
3640
}
3641
3642
if (!p_args[0]->is_string()) {
3643
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3644
r_error.argument = 0;
3645
r_error.expected = Variant::STRING_NAME;
3646
return Variant();
3647
}
3648
3649
r_error.error = Callable::CallError::CALL_OK;
3650
3651
StringName method = *p_args[0];
3652
3653
call_deferred_thread_groupp(method, &p_args[1], p_argcount - 1, true);
3654
3655
return Variant();
3656
}
3657
3658
Variant Node::_call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
3659
if (p_argcount < 1) {
3660
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
3661
r_error.expected = 1;
3662
return Variant();
3663
}
3664
3665
if (!p_args[0]->is_string()) {
3666
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
3667
r_error.argument = 0;
3668
r_error.expected = Variant::STRING_NAME;
3669
return Variant();
3670
}
3671
3672
r_error.error = Callable::CallError::CALL_OK;
3673
3674
StringName method = *p_args[0];
3675
3676
call_thread_safep(method, &p_args[1], p_argcount - 1, true);
3677
3678
return Variant();
3679
}
3680
3681
void Node::call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3682
ERR_FAIL_COND(!is_inside_tree());
3683
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3684
pg->call_queue.push_callp(this, p_method, p_args, p_argcount, p_show_error);
3685
}
3686
3687
void Node::set_deferred_thread_group(const StringName &p_property, const Variant &p_value) {
3688
ERR_FAIL_COND(!is_inside_tree());
3689
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3690
pg->call_queue.push_set(this, p_property, p_value);
3691
}
3692
3693
void Node::notify_deferred_thread_group(int p_notification) {
3694
ERR_FAIL_COND(!is_inside_tree());
3695
SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
3696
pg->call_queue.push_notification(this, p_notification);
3697
}
3698
3699
void Node::call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
3700
if (is_accessible_from_caller_thread()) {
3701
Callable::CallError ce;
3702
callp(p_method, p_args, p_argcount, ce);
3703
if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
3704
ERR_FAIL_MSG("Error calling method from 'call_threadp': " + Variant::get_call_error_text(this, p_method, p_args, p_argcount, ce) + ".");
3705
}
3706
} else {
3707
call_deferred_thread_groupp(p_method, p_args, p_argcount, p_show_error);
3708
}
3709
}
3710
3711
void Node::set_thread_safe(const StringName &p_property, const Variant &p_value) {
3712
if (is_accessible_from_caller_thread()) {
3713
set(p_property, p_value);
3714
} else {
3715
set_deferred_thread_group(p_property, p_value);
3716
}
3717
}
3718
3719
void Node::notify_thread_safe(int p_notification) {
3720
if (is_accessible_from_caller_thread()) {
3721
notification(p_notification);
3722
} else {
3723
notify_deferred_thread_group(p_notification);
3724
}
3725
}
3726
3727
RID Node::get_focused_accessibility_element() const {
3728
RID id;
3729
if (GDVIRTUAL_CALL(_get_focused_accessibility_element, id)) {
3730
return id;
3731
} else {
3732
return get_accessibility_element();
3733
}
3734
}
3735
3736
void Node::queue_accessibility_update() {
3737
if (is_inside_tree() && !is_part_of_edited_scene()) {
3738
data.tree->_accessibility_notify_change(this);
3739
}
3740
}
3741
3742
RID Node::get_accessibility_element() const {
3743
if (is_part_of_edited_scene()) {
3744
return RID();
3745
}
3746
if (unlikely(data.accessibility_element.is_null())) {
3747
Window *w = get_non_popup_window();
3748
if (w && w->get_window_id() != DisplayServer::INVALID_WINDOW_ID && get_window()->is_visible()) {
3749
data.accessibility_element = DisplayServer::get_singleton()->accessibility_create_element(w->get_window_id(), DisplayServer::ROLE_CONTAINER);
3750
}
3751
}
3752
return data.accessibility_element;
3753
}
3754
3755
void Node::_bind_methods() {
3756
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"), 0);
3757
GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case,kebab-case"), NAME_CASING_PASCAL_CASE);
3758
3759
ClassDB::bind_static_method("Node", D_METHOD("print_orphan_nodes"), &Node::print_orphan_nodes);
3760
ClassDB::bind_static_method("Node", D_METHOD("get_orphan_node_ids"), &Node::get_orphan_node_ids);
3761
ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "force_readable_name"), &Node::add_sibling, DEFVAL(false));
3762
3763
ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
3764
ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
3765
ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
3766
ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
3767
ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true));
3768
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.
3769
ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::get_children, DEFVAL(false));
3770
ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));
3771
ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
3772
ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
3773
ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
3774
ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
3775
ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
3776
ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
3777
ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
3778
ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
3779
ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
3780
3781
ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
3782
ClassDB::bind_method(D_METHOD("is_part_of_edited_scene"), &Node::is_part_of_edited_scene);
3783
ClassDB::bind_method(D_METHOD("is_ancestor_of", "node"), &Node::is_ancestor_of);
3784
ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
3785
ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
3786
ClassDB::bind_method(D_METHOD("get_path_to", "node", "use_unique_path"), &Node::get_path_to, DEFVAL(false));
3787
ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
3788
ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
3789
ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
3790
ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_index"), &Node::move_child);
3791
ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
3792
ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
3793
ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
3794
ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false));
3795
ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
3796
ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
3797
ClassDB::bind_method(D_METHOD("get_tree_string"), &Node::get_tree_string);
3798
ClassDB::bind_method(D_METHOD("get_tree_string_pretty"), &Node::get_tree_string_pretty);
3799
ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path);
3800
ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path);
3801
ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
3802
ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
3803
ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
3804
ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
3805
ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
3806
ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
3807
ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
3808
ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
3809
ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
3810
ClassDB::bind_method(D_METHOD("set_physics_process_priority", "priority"), &Node::set_physics_process_priority);
3811
ClassDB::bind_method(D_METHOD("get_physics_process_priority"), &Node::get_physics_process_priority);
3812
ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
3813
ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
3814
ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
3815
ClassDB::bind_method(D_METHOD("set_process_shortcut_input", "enable"), &Node::set_process_shortcut_input);
3816
ClassDB::bind_method(D_METHOD("is_processing_shortcut_input"), &Node::is_processing_shortcut_input);
3817
ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
3818
ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
3819
ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
3820
ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
3821
ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
3822
ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
3823
ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
3824
3825
ClassDB::bind_method(D_METHOD("set_process_thread_group", "mode"), &Node::set_process_thread_group);
3826
ClassDB::bind_method(D_METHOD("get_process_thread_group"), &Node::get_process_thread_group);
3827
3828
ClassDB::bind_method(D_METHOD("set_process_thread_messages", "flags"), &Node::set_process_thread_messages);
3829
ClassDB::bind_method(D_METHOD("get_process_thread_messages"), &Node::get_process_thread_messages);
3830
3831
ClassDB::bind_method(D_METHOD("set_process_thread_group_order", "order"), &Node::set_process_thread_group_order);
3832
ClassDB::bind_method(D_METHOD("get_process_thread_group_order"), &Node::get_process_thread_group_order);
3833
3834
ClassDB::bind_method(D_METHOD("queue_accessibility_update"), &Node::queue_accessibility_update);
3835
ClassDB::bind_method(D_METHOD("get_accessibility_element"), &Node::get_accessibility_element);
3836
3837
ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
3838
ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
3839
3840
ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
3841
ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
3842
3843
ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
3844
ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
3845
3846
ClassDB::bind_method(D_METHOD("set_physics_interpolation_mode", "mode"), &Node::set_physics_interpolation_mode);
3847
ClassDB::bind_method(D_METHOD("get_physics_interpolation_mode"), &Node::get_physics_interpolation_mode);
3848
ClassDB::bind_method(D_METHOD("is_physics_interpolated"), &Node::is_physics_interpolated);
3849
ClassDB::bind_method(D_METHOD("is_physics_interpolated_and_enabled"), &Node::is_physics_interpolated_and_enabled);
3850
ClassDB::bind_method(D_METHOD("reset_physics_interpolation"), &Node::reset_physics_interpolation);
3851
3852
ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "mode"), &Node::set_auto_translate_mode);
3853
ClassDB::bind_method(D_METHOD("get_auto_translate_mode"), &Node::get_auto_translate_mode);
3854
ClassDB::bind_method(D_METHOD("can_auto_translate"), &Node::can_auto_translate);
3855
ClassDB::bind_method(D_METHOD("set_translation_domain_inherited"), &Node::set_translation_domain_inherited);
3856
3857
ClassDB::bind_method(D_METHOD("get_window"), &Node::get_window);
3858
ClassDB::bind_method(D_METHOD("get_last_exclusive_window"), &Node::get_last_exclusive_window);
3859
ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
3860
ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
3861
3862
ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_DEFAULT));
3863
ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
3864
3865
ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
3866
ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
3867
ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
3868
ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
3869
3870
ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
3871
3872
ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_free);
3873
3874
ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
3875
ClassDB::bind_method(D_METHOD("is_node_ready"), &Node::is_ready);
3876
3877
ClassDB::bind_method(D_METHOD("set_multiplayer_authority", "id", "recursive"), &Node::set_multiplayer_authority, DEFVAL(true));
3878
ClassDB::bind_method(D_METHOD("get_multiplayer_authority"), &Node::get_multiplayer_authority);
3879
3880
ClassDB::bind_method(D_METHOD("is_multiplayer_authority"), &Node::is_multiplayer_authority);
3881
3882
ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
3883
ClassDB::bind_method(D_METHOD("rpc_config", "method", "config"), &Node::rpc_config);
3884
ClassDB::bind_method(D_METHOD("get_node_rpc_config"), &Node::_get_node_rpc_config_bind);
3885
3886
ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
3887
ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);
3888
3889
ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
3890
ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
3891
3892
ClassDB::bind_method(D_METHOD("atr", "message", "context"), &Node::atr, DEFVAL(""));
3893
ClassDB::bind_method(D_METHOD("atr_n", "message", "plural_message", "n", "context"), &Node::atr_n, DEFVAL(""));
3894
3895
#ifdef TOOLS_ENABLED
3896
ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
3897
#endif
3898
3899
{
3900
MethodInfo mi;
3901
3902
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3903
3904
mi.name = "rpc";
3905
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
3906
}
3907
3908
{
3909
MethodInfo mi;
3910
3911
mi.arguments.push_back(PropertyInfo(Variant::INT, "peer_id"));
3912
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3913
3914
mi.name = "rpc_id";
3915
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
3916
}
3917
3918
ClassDB::bind_method(D_METHOD("update_configuration_warnings"), &Node::update_configuration_warnings);
3919
3920
{
3921
MethodInfo mi;
3922
mi.name = "call_deferred_thread_group";
3923
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3924
3925
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred_thread_group", &Node::_call_deferred_thread_group_bind, mi, varray(), false);
3926
}
3927
ClassDB::bind_method(D_METHOD("set_deferred_thread_group", "property", "value"), &Node::set_deferred_thread_group);
3928
ClassDB::bind_method(D_METHOD("notify_deferred_thread_group", "what"), &Node::notify_deferred_thread_group);
3929
3930
{
3931
MethodInfo mi;
3932
mi.name = "call_thread_safe";
3933
mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
3934
3935
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_thread_safe", &Node::_call_thread_safe_bind, mi, varray(), false);
3936
}
3937
ClassDB::bind_method(D_METHOD("set_thread_safe", "property", "value"), &Node::set_thread_safe);
3938
ClassDB::bind_method(D_METHOD("notify_thread_safe", "what"), &Node::notify_thread_safe);
3939
3940
BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
3941
BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
3942
BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
3943
BIND_CONSTANT(NOTIFICATION_READY);
3944
BIND_CONSTANT(NOTIFICATION_PAUSED);
3945
BIND_CONSTANT(NOTIFICATION_UNPAUSED);
3946
BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
3947
BIND_CONSTANT(NOTIFICATION_PROCESS);
3948
BIND_CONSTANT(NOTIFICATION_PARENTED);
3949
BIND_CONSTANT(NOTIFICATION_UNPARENTED);
3950
BIND_CONSTANT(NOTIFICATION_SCENE_INSTANTIATED);
3951
BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
3952
BIND_CONSTANT(NOTIFICATION_DRAG_END);
3953
BIND_CONSTANT(NOTIFICATION_PATH_RENAMED);
3954
BIND_CONSTANT(NOTIFICATION_CHILD_ORDER_CHANGED);
3955
BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
3956
BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
3957
BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
3958
BIND_CONSTANT(NOTIFICATION_DISABLED);
3959
BIND_CONSTANT(NOTIFICATION_ENABLED);
3960
BIND_CONSTANT(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
3961
3962
BIND_CONSTANT(NOTIFICATION_EDITOR_PRE_SAVE);
3963
BIND_CONSTANT(NOTIFICATION_EDITOR_POST_SAVE);
3964
3965
BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
3966
BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
3967
BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_IN);
3968
BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_OUT);
3969
BIND_CONSTANT(NOTIFICATION_WM_CLOSE_REQUEST);
3970
BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
3971
BIND_CONSTANT(NOTIFICATION_WM_SIZE_CHANGED);
3972
BIND_CONSTANT(NOTIFICATION_WM_DPI_CHANGE);
3973
BIND_CONSTANT(NOTIFICATION_VP_MOUSE_ENTER);
3974
BIND_CONSTANT(NOTIFICATION_VP_MOUSE_EXIT);
3975
BIND_CONSTANT(NOTIFICATION_WM_POSITION_CHANGED);
3976
BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
3977
BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
3978
BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
3979
BIND_CONSTANT(NOTIFICATION_CRASH);
3980
BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
3981
BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
3982
BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
3983
BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
3984
BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
3985
BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
3986
3987
BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_UPDATE);
3988
BIND_CONSTANT(NOTIFICATION_ACCESSIBILITY_INVALIDATE);
3989
3990
BIND_ENUM_CONSTANT(PROCESS_MODE_INHERIT);
3991
BIND_ENUM_CONSTANT(PROCESS_MODE_PAUSABLE);
3992
BIND_ENUM_CONSTANT(PROCESS_MODE_WHEN_PAUSED);
3993
BIND_ENUM_CONSTANT(PROCESS_MODE_ALWAYS);
3994
BIND_ENUM_CONSTANT(PROCESS_MODE_DISABLED);
3995
3996
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_INHERIT);
3997
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_MAIN_THREAD);
3998
BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_SUB_THREAD);
3999
4000
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES);
4001
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_PHYSICS);
4002
BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_ALL);
4003
4004
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_INHERIT);
4005
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_ON);
4006
BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_OFF);
4007
4008
BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
4009
BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
4010
BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
4011
BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION);
4012
BIND_ENUM_CONSTANT(DUPLICATE_INTERNAL_STATE);
4013
BIND_ENUM_CONSTANT(DUPLICATE_DEFAULT);
4014
4015
BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
4016
BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
4017
BIND_ENUM_CONSTANT(INTERNAL_MODE_BACK);
4018
4019
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_INHERIT);
4020
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_ALWAYS);
4021
BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_DISABLED);
4022
4023
ADD_SIGNAL(MethodInfo("ready"));
4024
ADD_SIGNAL(MethodInfo("renamed"));
4025
ADD_SIGNAL(MethodInfo("tree_entered"));
4026
ADD_SIGNAL(MethodInfo("tree_exiting"));
4027
ADD_SIGNAL(MethodInfo("tree_exited"));
4028
ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4029
ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4030
4031
ADD_SIGNAL(MethodInfo("child_order_changed"));
4032
ADD_SIGNAL(MethodInfo("replacing_by", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4033
ADD_SIGNAL(MethodInfo("editor_description_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
4034
ADD_SIGNAL(MethodInfo("editor_state_changed"));
4035
4036
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
4037
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");
4038
ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
4039
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, Node::get_class_static(), PROPERTY_USAGE_NONE), "set_owner", "get_owner");
4040
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, MultiplayerAPI::get_class_static(), PROPERTY_USAGE_NONE), "", "get_multiplayer");
4041
4042
ADD_GROUP("Process", "process_");
4043
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
4044
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
4045
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_physics_priority"), "set_physics_process_priority", "get_physics_process_priority");
4046
4047
ADD_SUBGROUP("Thread Group", "process_thread");
4048
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group", PROPERTY_HINT_ENUM, "Inherit,Main Thread,Sub Thread"), "set_process_thread_group", "get_process_thread_group");
4049
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group_order"), "set_process_thread_group_order", "get_process_thread_group_order");
4050
ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_messages", PROPERTY_HINT_FLAGS, "Process,Physics Process"), "set_process_thread_messages", "get_process_thread_messages");
4051
4052
ADD_GROUP("Physics Interpolation", "physics_interpolation_");
4053
ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_mode", PROPERTY_HINT_ENUM, "Inherit,On,Off"), "set_physics_interpolation_mode", "get_physics_interpolation_mode");
4054
4055
ADD_GROUP("Auto Translate", "auto_translate_");
4056
ADD_PROPERTY(PropertyInfo(Variant::INT, "auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_auto_translate_mode", "get_auto_translate_mode");
4057
4058
ADD_GROUP("Editor Description", "editor_");
4059
ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
4060
4061
GDVIRTUAL_BIND(_process, "delta");
4062
GDVIRTUAL_BIND(_physics_process, "delta");
4063
GDVIRTUAL_BIND(_enter_tree);
4064
GDVIRTUAL_BIND(_exit_tree);
4065
GDVIRTUAL_BIND(_ready);
4066
GDVIRTUAL_BIND(_get_configuration_warnings);
4067
GDVIRTUAL_BIND(_get_accessibility_configuration_warnings);
4068
GDVIRTUAL_BIND(_input, "event");
4069
GDVIRTUAL_BIND(_shortcut_input, "event");
4070
GDVIRTUAL_BIND(_unhandled_input, "event");
4071
GDVIRTUAL_BIND(_unhandled_key_input, "event");
4072
GDVIRTUAL_BIND(_get_focused_accessibility_element);
4073
}
4074
4075
String Node::_get_name_num_separator() {
4076
switch (GLOBAL_GET("editor/naming/node_name_num_separator").operator int()) {
4077
case 0:
4078
return "";
4079
case 1:
4080
return " ";
4081
case 2:
4082
return "_";
4083
case 3:
4084
return "-";
4085
}
4086
return " ";
4087
}
4088
4089
Node::Node() {
4090
_define_ancestry(AncestralClass::NODE);
4091
#ifdef DEBUG_ENABLED
4092
total_node_count.increment();
4093
#endif
4094
// Default member initializer for bitfield is a C++20 extension, so:
4095
4096
data.process_mode = PROCESS_MODE_INHERIT;
4097
data.physics_interpolation_mode = PHYSICS_INTERPOLATION_MODE_INHERIT;
4098
4099
data.physics_process = false;
4100
data.process = false;
4101
4102
data.physics_process_internal = false;
4103
data.process_internal = false;
4104
4105
data.input = false;
4106
data.shortcut_input = false;
4107
data.unhandled_input = false;
4108
data.unhandled_key_input = false;
4109
4110
data.physics_interpolated = true;
4111
data.physics_interpolation_reset_requested = false;
4112
data.physics_interpolated_client_side = false;
4113
data.use_identity_transform = false;
4114
4115
data.use_placeholder = false;
4116
4117
data.display_folded = false;
4118
data.editable_instance = false;
4119
4120
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.
4121
data.ready_first = true;
4122
4123
data.auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
4124
data.is_auto_translating = true;
4125
data.is_auto_translate_dirty = true;
4126
4127
data.is_translation_domain_inherited = true;
4128
data.is_translation_domain_dirty = true;
4129
}
4130
4131
Node::~Node() {
4132
data.grouped.clear();
4133
data.owned.clear();
4134
data.children.clear();
4135
data.children_cache.clear();
4136
4137
ERR_FAIL_COND(data.parent);
4138
ERR_FAIL_COND(data.children_cache.size());
4139
4140
#ifdef DEBUG_ENABLED
4141
total_node_count.decrement();
4142
#endif
4143
}
4144
4145
////////////////////////////////
4146
// Multithreaded locked version of Object functions.
4147
4148
#ifdef DEBUG_ENABLED
4149
4150
void Node::set_script(const Variant &p_script) {
4151
ERR_THREAD_GUARD;
4152
Object::set_script(p_script);
4153
}
4154
4155
Variant Node::get_script() const {
4156
ERR_THREAD_GUARD_V(Variant());
4157
return Object::get_script();
4158
}
4159
4160
bool Node::has_meta(const StringName &p_name) const {
4161
ERR_THREAD_GUARD_V(false);
4162
return Object::has_meta(p_name);
4163
}
4164
4165
void Node::set_meta(const StringName &p_name, const Variant &p_value) {
4166
ERR_THREAD_GUARD;
4167
Object::set_meta(p_name, p_value);
4168
_emit_editor_state_changed();
4169
}
4170
4171
void Node::remove_meta(const StringName &p_name) {
4172
ERR_THREAD_GUARD;
4173
Object::remove_meta(p_name);
4174
_emit_editor_state_changed();
4175
}
4176
4177
Variant Node::get_meta(const StringName &p_name, const Variant &p_default) const {
4178
ERR_THREAD_GUARD_V(Variant());
4179
return Object::get_meta(p_name, p_default);
4180
}
4181
4182
void Node::get_meta_list(List<StringName> *p_list) const {
4183
ERR_THREAD_GUARD;
4184
Object::get_meta_list(p_list);
4185
}
4186
4187
Error Node::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
4188
ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4189
return Object::emit_signalp(p_name, p_args, p_argcount);
4190
}
4191
4192
bool Node::has_signal(const StringName &p_name) const {
4193
ERR_THREAD_GUARD_V(false);
4194
return Object::has_signal(p_name);
4195
}
4196
4197
void Node::get_signal_list(List<MethodInfo> *p_signals) const {
4198
ERR_THREAD_GUARD;
4199
Object::get_signal_list(p_signals);
4200
}
4201
4202
void Node::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
4203
ERR_THREAD_GUARD;
4204
Object::get_signal_connection_list(p_signal, p_connections);
4205
}
4206
4207
void Node::get_all_signal_connections(List<Connection> *p_connections) const {
4208
ERR_THREAD_GUARD;
4209
Object::get_all_signal_connections(p_connections);
4210
}
4211
4212
int Node::get_persistent_signal_connection_count() const {
4213
ERR_THREAD_GUARD_V(0);
4214
return Object::get_persistent_signal_connection_count();
4215
}
4216
4217
uint32_t Node::get_signal_connection_flags(const StringName &p_signal, const Callable &p_callable) const {
4218
ERR_THREAD_GUARD_V(0);
4219
return Object::get_signal_connection_flags(p_signal, p_callable);
4220
}
4221
4222
void Node::get_signals_connected_to_this(List<Connection> *p_connections) const {
4223
ERR_THREAD_GUARD;
4224
Object::get_signals_connected_to_this(p_connections);
4225
}
4226
4227
Error Node::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
4228
ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
4229
4230
Error retval = Object::connect(p_signal, p_callable, p_flags);
4231
#ifdef TOOLS_ENABLED
4232
if (p_flags & CONNECT_PERSIST) {
4233
_emit_editor_state_changed();
4234
}
4235
#endif
4236
4237
return retval;
4238
}
4239
4240
void Node::disconnect(const StringName &p_signal, const Callable &p_callable) {
4241
ERR_THREAD_GUARD;
4242
4243
#ifdef TOOLS_ENABLED
4244
// Already under thread guard, don't check again.
4245
uint32_t connection_flags = Object::get_signal_connection_flags(p_signal, p_callable);
4246
#endif
4247
4248
[[maybe_unused]] bool changed = Object::_disconnect(p_signal, p_callable);
4249
4250
#ifdef TOOLS_ENABLED
4251
if (changed && connection_flags & CONNECT_PERSIST) {
4252
_emit_editor_state_changed();
4253
}
4254
#endif
4255
}
4256
4257
bool Node::is_connected(const StringName &p_signal, const Callable &p_callable) const {
4258
ERR_THREAD_GUARD_V(false);
4259
return Object::is_connected(p_signal, p_callable);
4260
}
4261
4262
bool Node::has_connections(const StringName &p_signal) const {
4263
ERR_THREAD_GUARD_V(false);
4264
return Object::has_connections(p_signal);
4265
}
4266
4267
#endif
4268
4269