Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/node.h
9896 views
1
/**************************************************************************/
2
/* node.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/string/node_path.h"
34
#include "core/variant/typed_array.h"
35
#include "scene/main/scene_tree.h"
36
#include "scene/scene_string_names.h"
37
38
class Viewport;
39
class Window;
40
class SceneState;
41
class Tween;
42
class PropertyTweener;
43
44
SAFE_FLAG_TYPE_PUN_GUARANTEES
45
SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t)
46
47
class Node : public Object {
48
GDCLASS(Node, Object);
49
50
friend class SceneTreeFTI;
51
52
protected:
53
// During group processing, these are thread-safe.
54
// Outside group processing, these avoid the cost of sync by working as plain primitive types.
55
union MTFlag {
56
SafeFlag mt;
57
bool st;
58
MTFlag() :
59
mt{} {}
60
};
61
template <typename T>
62
union MTNumeric {
63
SafeNumeric<T> mt;
64
T st;
65
MTNumeric() :
66
mt{} {}
67
};
68
69
public:
70
// N.B. Any enum stored as a bitfield should be specified as UNSIGNED to work around
71
// some compilers trying to store it as signed, and requiring 1 more bit than necessary.
72
enum ProcessMode : unsigned int {
73
PROCESS_MODE_INHERIT, // same as parent node
74
PROCESS_MODE_PAUSABLE, // process only if not paused
75
PROCESS_MODE_WHEN_PAUSED, // process only if paused
76
PROCESS_MODE_ALWAYS, // process always
77
PROCESS_MODE_DISABLED, // never process
78
};
79
80
enum ProcessThreadGroup {
81
PROCESS_THREAD_GROUP_INHERIT,
82
PROCESS_THREAD_GROUP_MAIN_THREAD,
83
PROCESS_THREAD_GROUP_SUB_THREAD,
84
};
85
86
enum ProcessThreadMessages {
87
FLAG_PROCESS_THREAD_MESSAGES = 1,
88
FLAG_PROCESS_THREAD_MESSAGES_PHYSICS = 2,
89
FLAG_PROCESS_THREAD_MESSAGES_ALL = 3,
90
};
91
92
enum PhysicsInterpolationMode : unsigned int {
93
PHYSICS_INTERPOLATION_MODE_INHERIT,
94
PHYSICS_INTERPOLATION_MODE_ON,
95
PHYSICS_INTERPOLATION_MODE_OFF,
96
};
97
98
enum DuplicateFlags {
99
DUPLICATE_SIGNALS = 1,
100
DUPLICATE_GROUPS = 2,
101
DUPLICATE_SCRIPTS = 4,
102
DUPLICATE_USE_INSTANTIATION = 8,
103
#ifdef TOOLS_ENABLED
104
DUPLICATE_FROM_EDITOR = 16,
105
#endif
106
};
107
108
enum NameCasing {
109
NAME_CASING_PASCAL_CASE,
110
NAME_CASING_CAMEL_CASE,
111
NAME_CASING_SNAKE_CASE,
112
NAME_CASING_KEBAB_CASE,
113
};
114
115
enum InternalMode {
116
INTERNAL_MODE_DISABLED,
117
INTERNAL_MODE_FRONT,
118
INTERNAL_MODE_BACK,
119
};
120
121
enum AutoTranslateMode : unsigned int {
122
AUTO_TRANSLATE_MODE_INHERIT,
123
AUTO_TRANSLATE_MODE_ALWAYS,
124
AUTO_TRANSLATE_MODE_DISABLED,
125
};
126
127
struct Comparator {
128
bool operator()(const Node *p_a, const Node *p_b) const { return p_b->is_greater_than(p_a); }
129
};
130
131
static int orphan_node_count;
132
133
void _update_process(bool p_enable, bool p_for_children);
134
135
private:
136
struct GroupData {
137
bool persistent = false;
138
SceneTree::Group *group = nullptr;
139
};
140
141
struct ComparatorByIndex {
142
bool operator()(const Node *p_left, const Node *p_right) const {
143
static const uint32_t order[3] = { 1, 0, 2 };
144
uint32_t order_left = order[p_left->data.internal_mode];
145
uint32_t order_right = order[p_right->data.internal_mode];
146
if (order_left == order_right) {
147
return p_left->data.index < p_right->data.index;
148
}
149
return order_left < order_right;
150
}
151
};
152
153
struct ComparatorWithPriority {
154
bool operator()(const Node *p_a, const Node *p_b) const { return p_b->data.process_priority == p_a->data.process_priority ? p_b->is_greater_than(p_a) : p_b->data.process_priority > p_a->data.process_priority; }
155
};
156
157
struct ComparatorWithPhysicsPriority {
158
bool operator()(const Node *p_a, const Node *p_b) const { return p_b->data.physics_process_priority == p_a->data.physics_process_priority ? p_b->is_greater_than(p_a) : p_b->data.physics_process_priority > p_a->data.physics_process_priority; }
159
};
160
161
// This Data struct is to avoid namespace pollution in derived classes.
162
struct Data {
163
String scene_file_path;
164
Ref<SceneState> instance_state;
165
Ref<SceneState> inherited_state;
166
167
Node *parent = nullptr;
168
Node *owner = nullptr;
169
HashMap<StringName, Node *> children;
170
mutable bool children_cache_dirty = true;
171
mutable LocalVector<Node *> children_cache;
172
HashMap<StringName, Node *> owned_unique_nodes;
173
bool unique_name_in_owner = false;
174
InternalMode internal_mode = INTERNAL_MODE_DISABLED;
175
mutable int internal_children_front_count_cache = 0;
176
mutable int internal_children_back_count_cache = 0;
177
mutable int external_children_count_cache = 0;
178
mutable int index = -1; // relative to front, normal or back.
179
int32_t depth = -1;
180
int blocked = 0; // Safeguard that throws an error when attempting to modify the tree in a harmful way while being traversed.
181
StringName name;
182
SceneTree *tree = nullptr;
183
184
#ifdef TOOLS_ENABLED
185
NodePath import_path; // Path used when imported, used by scene editors to keep tracking.
186
#endif
187
String editor_description;
188
189
Viewport *viewport = nullptr;
190
191
mutable RID accessibility_element;
192
193
HashMap<StringName, GroupData> grouped;
194
List<Node *>::Element *OW = nullptr; // Owned element.
195
List<Node *> owned;
196
197
Node *process_owner = nullptr;
198
ProcessThreadGroup process_thread_group = PROCESS_THREAD_GROUP_INHERIT;
199
Node *process_thread_group_owner = nullptr;
200
int process_thread_group_order = 0;
201
BitField<ProcessThreadMessages> process_thread_messages = {};
202
void *process_group = nullptr; // to avoid cyclic dependency
203
204
int multiplayer_authority = 1; // Server by default.
205
Variant rpc_config;
206
207
// Variables used to properly sort the node when processing, ignored otherwise.
208
int process_priority = 0;
209
int physics_process_priority = 0;
210
211
// Keep bitpacked values together to get better packing.
212
ProcessMode process_mode : 3;
213
PhysicsInterpolationMode physics_interpolation_mode : 2;
214
AutoTranslateMode auto_translate_mode : 2;
215
216
bool physics_process : 1;
217
bool process : 1;
218
219
bool physics_process_internal : 1;
220
bool process_internal : 1;
221
222
bool input : 1;
223
bool shortcut_input : 1;
224
bool unhandled_input : 1;
225
bool unhandled_key_input : 1;
226
227
// Physics interpolation can be turned on and off on a per node basis.
228
// This only takes effect when the SceneTree (or project setting) physics interpolation
229
// is switched on.
230
bool physics_interpolated : 1;
231
232
// We can auto-reset physics interpolation when e.g. adding a node for the first time.
233
bool physics_interpolation_reset_requested : 1;
234
235
// Most nodes need not be interpolated in the scene tree, physics interpolation
236
// is normally only needed in the RenderingServer. However if we need to read the
237
// interpolated transform of a node in the SceneTree, it is necessary to duplicate
238
// the interpolation logic client side, in order to prevent stalling the RenderingServer
239
// by reading back.
240
bool physics_interpolated_client_side : 1;
241
242
// For certain nodes (e.g. CPU particles in global mode)
243
// it can be useful to not send the instance transform to the
244
// RenderingServer, and specify the mesh in world space.
245
bool use_identity_transform : 1;
246
247
bool use_placeholder : 1;
248
249
bool display_folded : 1;
250
bool editable_instance : 1;
251
252
bool ready_notified : 1;
253
bool ready_first : 1;
254
255
mutable bool is_auto_translating : 1;
256
mutable bool is_auto_translate_dirty : 1;
257
258
mutable bool is_translation_domain_inherited : 1;
259
mutable bool is_translation_domain_dirty : 1;
260
261
mutable NodePath *path_cache = nullptr;
262
263
} data;
264
265
Ref<MultiplayerAPI> multiplayer;
266
267
String _get_tree_string_pretty(const String &p_prefix, bool p_last);
268
String _get_tree_string(const Node *p_node);
269
270
Node *_get_child_by_name(const StringName &p_name) const;
271
272
void _replace_connections_target(Node *p_new_target);
273
274
void _validate_child_name(Node *p_child, bool p_force_human_readable = false);
275
void _generate_serial_child_name(const Node *p_child, StringName &name) const;
276
277
void _propagate_reverse_notification(int p_notification);
278
void _propagate_deferred_notification(int p_notification, bool p_reverse);
279
void _propagate_enter_tree();
280
void _propagate_ready();
281
void _propagate_exit_tree();
282
void _propagate_after_exit_tree();
283
void _propagate_physics_interpolated(bool p_interpolated);
284
void _propagate_physics_interpolation_reset_requested(bool p_requested);
285
void _propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification);
286
void _propagate_groups_dirty();
287
void _propagate_translation_domain_dirty();
288
Array _get_node_and_resource(const NodePath &p_path);
289
290
void _duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const;
291
void _duplicate_signals(const Node *p_original, Node *p_copy) const;
292
Node *_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap = nullptr) const;
293
294
TypedArray<StringName> _get_groups() const;
295
296
Error _rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
297
Error _rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
298
299
friend class SceneTree;
300
301
void _set_tree(SceneTree *p_tree);
302
void _propagate_pause_notification(bool p_enable);
303
void _propagate_suspend_notification(bool p_enable);
304
305
_FORCE_INLINE_ bool _can_process(bool p_paused) const;
306
_FORCE_INLINE_ bool _is_enabled() const;
307
308
void _release_unique_name_in_owner();
309
void _acquire_unique_name_in_owner();
310
311
void _clean_up_owner();
312
313
_FORCE_INLINE_ void _update_children_cache() const {
314
if (unlikely(data.children_cache_dirty)) {
315
_update_children_cache_impl();
316
}
317
}
318
319
void _update_children_cache_impl() const;
320
321
// Process group management
322
void _add_process_group();
323
void _remove_process_group();
324
void _add_to_process_thread_group();
325
void _remove_from_process_thread_group();
326
void _remove_tree_from_process_thread_group();
327
void _add_tree_to_process_thread_group(Node *p_owner);
328
329
static thread_local Node *current_process_thread_group;
330
331
Variant _call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
332
Variant _call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
333
334
// Editor only signal to keep the SceneTreeEditor in sync.
335
#ifdef TOOLS_ENABLED
336
void _emit_editor_state_changed();
337
#else
338
void _emit_editor_state_changed() {}
339
#endif
340
341
protected:
342
void _block() { data.blocked++; }
343
void _unblock() { data.blocked--; }
344
345
void _notification(int p_notification);
346
347
virtual void _physics_interpolated_changed();
348
349
virtual void add_child_notify(Node *p_child);
350
virtual void remove_child_notify(Node *p_child);
351
virtual void move_child_notify(Node *p_child);
352
virtual void owner_changed_notify();
353
354
void _propagate_replace_owner(Node *p_owner, Node *p_by_owner);
355
356
static void _bind_methods();
357
static String _get_name_num_separator();
358
359
friend class SceneState;
360
361
void _add_child_nocheck(Node *p_child, const StringName &p_name, InternalMode p_internal_mode = INTERNAL_MODE_DISABLED);
362
void _set_owner_nocheck(Node *p_owner);
363
void _set_name_nocheck(const StringName &p_name);
364
365
void _set_physics_interpolated_client_side(bool p_enable) { data.physics_interpolated_client_side = p_enable; }
366
bool _is_physics_interpolated_client_side() const { return data.physics_interpolated_client_side; }
367
368
void _set_physics_interpolation_reset_requested(bool p_enable) { data.physics_interpolation_reset_requested = p_enable; }
369
bool _is_physics_interpolation_reset_requested() const { return data.physics_interpolation_reset_requested; }
370
371
void _set_use_identity_transform(bool p_enable) { data.use_identity_transform = p_enable; }
372
bool _is_using_identity_transform() const { return data.use_identity_transform; }
373
int32_t _get_scene_tree_depth() const { return data.depth; }
374
375
//call from SceneTree
376
void _call_input(const Ref<InputEvent> &p_event);
377
void _call_shortcut_input(const Ref<InputEvent> &p_event);
378
void _call_unhandled_input(const Ref<InputEvent> &p_event);
379
void _call_unhandled_key_input(const Ref<InputEvent> &p_event);
380
381
void _validate_property(PropertyInfo &p_property) const;
382
383
Variant _get_node_rpc_config_bind() const {
384
return get_node_rpc_config().duplicate(true);
385
}
386
387
protected:
388
virtual bool _uses_signal_mutex() const override { return false; } // Node uses thread guards instead.
389
390
virtual void input(const Ref<InputEvent> &p_event);
391
virtual void shortcut_input(const Ref<InputEvent> &p_key_event);
392
virtual void unhandled_input(const Ref<InputEvent> &p_event);
393
virtual void unhandled_key_input(const Ref<InputEvent> &p_key_event);
394
395
GDVIRTUAL1(_process, double)
396
GDVIRTUAL1(_physics_process, double)
397
GDVIRTUAL0(_enter_tree)
398
GDVIRTUAL0(_exit_tree)
399
GDVIRTUAL0(_ready)
400
GDVIRTUAL0RC(Vector<String>, _get_accessibility_configuration_warnings)
401
GDVIRTUAL0RC(Vector<String>, _get_configuration_warnings)
402
403
GDVIRTUAL1(_input, Ref<InputEvent>)
404
GDVIRTUAL1(_shortcut_input, Ref<InputEvent>)
405
GDVIRTUAL1(_unhandled_input, Ref<InputEvent>)
406
GDVIRTUAL1(_unhandled_key_input, Ref<InputEvent>)
407
408
GDVIRTUAL0RC(RID, _get_focused_accessibility_element)
409
410
#ifndef DISABLE_DEPRECATED
411
void _set_name_bind_compat_76560(const String &p_name);
412
Variant _get_rpc_config_bind_compat_106848() const;
413
static void _bind_compatibility_methods();
414
#endif
415
416
public:
417
enum {
418
// You can make your own, but don't use the same numbers as other notifications in other nodes.
419
NOTIFICATION_ENTER_TREE = 10,
420
NOTIFICATION_EXIT_TREE = 11,
421
NOTIFICATION_MOVED_IN_PARENT = 12,
422
NOTIFICATION_READY = 13,
423
NOTIFICATION_PAUSED = 14,
424
NOTIFICATION_UNPAUSED = 15,
425
NOTIFICATION_PHYSICS_PROCESS = 16,
426
NOTIFICATION_PROCESS = 17,
427
NOTIFICATION_PARENTED = 18,
428
NOTIFICATION_UNPARENTED = 19,
429
NOTIFICATION_SCENE_INSTANTIATED = 20,
430
NOTIFICATION_DRAG_BEGIN = 21,
431
NOTIFICATION_DRAG_END = 22,
432
NOTIFICATION_PATH_RENAMED = 23,
433
NOTIFICATION_CHILD_ORDER_CHANGED = 24,
434
NOTIFICATION_INTERNAL_PROCESS = 25,
435
NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26,
436
NOTIFICATION_POST_ENTER_TREE = 27,
437
NOTIFICATION_DISABLED = 28,
438
NOTIFICATION_ENABLED = 29,
439
NOTIFICATION_RESET_PHYSICS_INTERPOLATION = 2001, // A GodotSpace Odyssey.
440
// Keep these linked to Node.
441
442
NOTIFICATION_ACCESSIBILITY_UPDATE = 3000,
443
NOTIFICATION_ACCESSIBILITY_INVALIDATE = 3001,
444
445
NOTIFICATION_WM_MOUSE_ENTER = 1002,
446
NOTIFICATION_WM_MOUSE_EXIT = 1003,
447
NOTIFICATION_WM_WINDOW_FOCUS_IN = 1004,
448
NOTIFICATION_WM_WINDOW_FOCUS_OUT = 1005,
449
NOTIFICATION_WM_CLOSE_REQUEST = 1006,
450
NOTIFICATION_WM_GO_BACK_REQUEST = 1007,
451
NOTIFICATION_WM_SIZE_CHANGED = 1008,
452
NOTIFICATION_WM_DPI_CHANGE = 1009,
453
NOTIFICATION_VP_MOUSE_ENTER = 1010,
454
NOTIFICATION_VP_MOUSE_EXIT = 1011,
455
NOTIFICATION_WM_POSITION_CHANGED = 1012,
456
457
NOTIFICATION_OS_MEMORY_WARNING = MainLoop::NOTIFICATION_OS_MEMORY_WARNING,
458
NOTIFICATION_TRANSLATION_CHANGED = MainLoop::NOTIFICATION_TRANSLATION_CHANGED,
459
NOTIFICATION_WM_ABOUT = MainLoop::NOTIFICATION_WM_ABOUT,
460
NOTIFICATION_CRASH = MainLoop::NOTIFICATION_CRASH,
461
NOTIFICATION_OS_IME_UPDATE = MainLoop::NOTIFICATION_OS_IME_UPDATE,
462
NOTIFICATION_APPLICATION_RESUMED = MainLoop::NOTIFICATION_APPLICATION_RESUMED,
463
NOTIFICATION_APPLICATION_PAUSED = MainLoop::NOTIFICATION_APPLICATION_PAUSED,
464
NOTIFICATION_APPLICATION_FOCUS_IN = MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN,
465
NOTIFICATION_APPLICATION_FOCUS_OUT = MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT,
466
NOTIFICATION_TEXT_SERVER_CHANGED = MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED,
467
468
// Editor specific node notifications
469
NOTIFICATION_EDITOR_PRE_SAVE = 9001,
470
NOTIFICATION_EDITOR_POST_SAVE = 9002,
471
NOTIFICATION_SUSPENDED = 9003,
472
NOTIFICATION_UNSUSPENDED = 9004
473
};
474
475
/* NODE/TREE */
476
477
StringName get_name() const;
478
String get_description() const;
479
void set_name(const StringName &p_name);
480
481
InternalMode get_internal_mode() const;
482
483
void add_child(Node *p_child, bool p_force_readable_name = false, InternalMode p_internal = INTERNAL_MODE_DISABLED);
484
void add_sibling(Node *p_sibling, bool p_force_readable_name = false);
485
void remove_child(Node *p_child);
486
487
int get_child_count(bool p_include_internal = true) const;
488
Node *get_child(int p_index, bool p_include_internal = true) const;
489
TypedArray<Node> get_children(bool p_include_internal = true) const;
490
bool has_node(const NodePath &p_path) const;
491
Node *get_node(const NodePath &p_path) const;
492
Node *get_node_or_null(const NodePath &p_path) const;
493
Node *find_child(const String &p_pattern, bool p_recursive = true, bool p_owned = true) const;
494
TypedArray<Node> find_children(const String &p_pattern, const String &p_type = "", bool p_recursive = true, bool p_owned = true) const;
495
bool has_node_and_resource(const NodePath &p_path) const;
496
Node *get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property = true) const;
497
498
virtual void reparent(Node *p_parent, bool p_keep_global_transform = true);
499
Node *get_parent() const;
500
Node *find_parent(const String &p_pattern) const;
501
502
Window *get_window() const;
503
Window *get_non_popup_window() const;
504
Window *get_last_exclusive_window() const;
505
506
_FORCE_INLINE_ SceneTree *get_tree() const {
507
ERR_FAIL_NULL_V(data.tree, nullptr);
508
return data.tree;
509
}
510
511
_FORCE_INLINE_ bool is_inside_tree() const { return data.tree; }
512
bool is_internal() const { return data.internal_mode != INTERNAL_MODE_DISABLED; }
513
514
bool is_ancestor_of(const Node *p_node) const;
515
bool is_greater_than(const Node *p_node) const;
516
517
NodePath get_path() const;
518
NodePath get_path_to(const Node *p_node, bool p_use_unique_path = false) const;
519
Node *find_common_parent_with(const Node *p_node) const;
520
521
void add_to_group(const StringName &p_identifier, bool p_persistent = false);
522
void remove_from_group(const StringName &p_identifier);
523
bool is_in_group(const StringName &p_identifier) const;
524
525
struct GroupInfo {
526
StringName name;
527
bool persistent = false;
528
};
529
530
void get_groups(List<GroupInfo> *p_groups) const;
531
int get_persistent_group_count() const;
532
533
void move_child(Node *p_child, int p_index);
534
void _move_child(Node *p_child, int p_index, bool p_ignore_end = false);
535
536
void set_owner(Node *p_owner);
537
Node *get_owner() const;
538
void get_owned_by(Node *p_by, List<Node *> *p_owned);
539
540
void set_unique_name_in_owner(bool p_enabled);
541
bool is_unique_name_in_owner() const;
542
543
_FORCE_INLINE_ int get_index(bool p_include_internal = true) const {
544
// p_include_internal = false doesn't make sense if the node is internal.
545
ERR_FAIL_COND_V_MSG(!p_include_internal && data.internal_mode != INTERNAL_MODE_DISABLED, -1, "Node is internal. Can't get index with 'include_internal' being false.");
546
if (!data.parent) {
547
return data.index;
548
}
549
data.parent->_update_children_cache();
550
551
if (!p_include_internal) {
552
return data.index;
553
} else {
554
switch (data.internal_mode) {
555
case INTERNAL_MODE_DISABLED: {
556
return data.parent->data.internal_children_front_count_cache + data.index;
557
} break;
558
case INTERNAL_MODE_FRONT: {
559
return data.index;
560
} break;
561
case INTERNAL_MODE_BACK: {
562
return data.parent->data.internal_children_front_count_cache + data.parent->data.external_children_count_cache + data.index;
563
} break;
564
}
565
return -1;
566
}
567
}
568
569
Ref<Tween> create_tween();
570
571
void print_tree();
572
void print_tree_pretty();
573
String get_tree_string();
574
String get_tree_string_pretty();
575
576
void set_scene_file_path(const String &p_scene_file_path);
577
String get_scene_file_path() const;
578
579
void set_editor_description(const String &p_editor_description);
580
String get_editor_description() const;
581
582
void set_editable_instance(Node *p_node, bool p_editable);
583
bool is_editable_instance(const Node *p_node) const;
584
Node *get_deepest_editable_node(Node *p_start_node) const;
585
586
#ifdef TOOLS_ENABLED
587
void set_property_pinned(const String &p_property, bool p_pinned);
588
bool is_property_pinned(const StringName &p_property) const;
589
virtual StringName get_property_store_alias(const StringName &p_property) const;
590
bool is_part_of_edited_scene() const;
591
#else
592
bool is_part_of_edited_scene() const { return false; }
593
#endif
594
void get_storable_properties(HashSet<StringName> &r_storable_properties) const;
595
596
virtual String to_string() override;
597
598
/* NOTIFICATIONS */
599
600
void propagate_notification(int p_notification);
601
602
void propagate_call(const StringName &p_method, const Array &p_args = Array(), const bool p_parent_first = false);
603
604
/* PROCESSING */
605
606
void set_physics_process(bool p_process);
607
double get_physics_process_delta_time() const;
608
bool is_physics_processing() const;
609
610
void set_process(bool p_process);
611
double get_process_delta_time() const;
612
bool is_processing() const;
613
614
void set_physics_process_internal(bool p_process_internal);
615
bool is_physics_processing_internal() const;
616
617
void set_process_internal(bool p_process_internal);
618
bool is_processing_internal() const;
619
620
void set_process_priority(int p_priority);
621
int get_process_priority() const;
622
623
void set_process_thread_group_order(int p_order);
624
int get_process_thread_group_order() const;
625
626
void set_physics_process_priority(int p_priority);
627
int get_physics_process_priority() const;
628
629
void set_process_input(bool p_enable);
630
bool is_processing_input() const;
631
632
void set_process_shortcut_input(bool p_enable);
633
bool is_processing_shortcut_input() const;
634
635
void set_process_unhandled_input(bool p_enable);
636
bool is_processing_unhandled_input() const;
637
638
void set_process_unhandled_key_input(bool p_enable);
639
bool is_processing_unhandled_key_input() const;
640
641
_FORCE_INLINE_ bool _is_any_processing() const {
642
return data.process || data.process_internal || data.physics_process || data.physics_process_internal;
643
}
644
_FORCE_INLINE_ bool is_accessible_from_caller_thread() const {
645
if (current_process_thread_group == nullptr) {
646
// No thread processing.
647
// Only accessible if node is outside the scene tree
648
// or access will happen from a node-safe thread.
649
return !data.tree || is_current_thread_safe_for_nodes();
650
} else {
651
// Thread processing.
652
return current_process_thread_group == data.process_thread_group_owner;
653
}
654
}
655
656
_FORCE_INLINE_ bool is_readable_from_caller_thread() const {
657
if (current_process_thread_group == nullptr) {
658
// No thread processing.
659
// Only accessible if node is outside the scene tree
660
// or access will happen from a node-safe thread.
661
return is_current_thread_safe_for_nodes() || unlikely(!data.tree);
662
} else {
663
// Thread processing.
664
return true;
665
}
666
}
667
668
_FORCE_INLINE_ static bool is_group_processing() { return current_process_thread_group; }
669
670
void set_process_thread_messages(BitField<ProcessThreadMessages> p_flags);
671
BitField<ProcessThreadMessages> get_process_thread_messages() const;
672
673
void queue_accessibility_update();
674
675
virtual RID get_accessibility_element() const;
676
virtual RID get_focused_accessibility_element() const;
677
virtual bool accessibility_override_tree_hierarchy() const { return false; }
678
679
virtual PackedStringArray get_accessibility_configuration_warnings() const;
680
681
Node *duplicate(int p_flags = DUPLICATE_GROUPS | DUPLICATE_SIGNALS | DUPLICATE_SCRIPTS) const;
682
#ifdef TOOLS_ENABLED
683
Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const;
684
Node *duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
685
void remap_node_resources(Node *p_node, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
686
void remap_nested_resources(Ref<Resource> p_resource, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const;
687
#endif
688
689
// used by editors, to save what has changed only
690
void set_scene_instance_state(const Ref<SceneState> &p_state);
691
Ref<SceneState> get_scene_instance_state() const;
692
693
void set_scene_inherited_state(const Ref<SceneState> &p_state);
694
Ref<SceneState> get_scene_inherited_state() const;
695
696
void set_scene_instance_load_placeholder(bool p_enable);
697
bool get_scene_instance_load_placeholder() const;
698
699
template <typename... VarArgs>
700
Vector<Variant> make_binds(VarArgs... p_args) {
701
Vector<Variant> binds = { p_args... };
702
return binds;
703
}
704
705
void replace_by(Node *p_node, bool p_keep_groups = false);
706
707
void set_process_mode(ProcessMode p_mode);
708
ProcessMode get_process_mode() const;
709
bool can_process() const;
710
bool can_process_notification(int p_what) const;
711
712
void set_physics_interpolation_mode(PhysicsInterpolationMode p_mode);
713
PhysicsInterpolationMode get_physics_interpolation_mode() const { return data.physics_interpolation_mode; }
714
_FORCE_INLINE_ bool is_physics_interpolated() const { return data.physics_interpolated; }
715
_FORCE_INLINE_ bool is_physics_interpolated_and_enabled() const { return SceneTree::is_fti_enabled() && is_physics_interpolated(); }
716
void reset_physics_interpolation();
717
718
bool is_enabled() const;
719
bool is_ready() const;
720
721
void request_ready();
722
723
void set_process_thread_group(ProcessThreadGroup p_mode);
724
ProcessThreadGroup get_process_thread_group() const;
725
726
static void print_orphan_nodes();
727
static TypedArray<int> get_orphan_node_ids();
728
729
#ifdef TOOLS_ENABLED
730
String validate_child_name(Node *p_child);
731
String prevalidate_child_name(Node *p_child, StringName p_name);
732
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
733
#endif
734
static String adjust_name_casing(const String &p_name);
735
736
void queue_free();
737
738
//hacks for speed
739
static void init_node_hrcr();
740
741
void set_import_path(const NodePath &p_import_path); //path used when imported, used by scene editors to keep tracking
742
NodePath get_import_path() const;
743
744
bool is_owned_by_parent() const;
745
746
void clear_internal_tree_resource_paths();
747
748
_FORCE_INLINE_ Viewport *get_viewport() const { return data.viewport; }
749
750
virtual PackedStringArray get_configuration_warnings() const;
751
752
void update_configuration_warnings();
753
754
void set_display_folded(bool p_folded);
755
bool is_displayed_folded() const;
756
757
/* NETWORK */
758
759
virtual void set_multiplayer_authority(int p_peer_id, bool p_recursive = true);
760
int get_multiplayer_authority() const;
761
bool is_multiplayer_authority() const;
762
763
void rpc_config(const StringName &p_method, const Variant &p_config); // config a local method for RPC
764
const Variant get_node_rpc_config() const;
765
766
template <typename... VarArgs>
767
Error rpc(const StringName &p_method, VarArgs... p_args);
768
769
template <typename... VarArgs>
770
Error rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args);
771
772
Error rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);
773
774
Ref<MultiplayerAPI> get_multiplayer() const;
775
776
/* INTERNATIONALIZATION */
777
778
void set_auto_translate_mode(AutoTranslateMode p_mode);
779
AutoTranslateMode get_auto_translate_mode() const;
780
bool can_auto_translate() const;
781
782
virtual StringName get_translation_domain() const override;
783
virtual void set_translation_domain(const StringName &p_domain) override;
784
void set_translation_domain_inherited();
785
786
_FORCE_INLINE_ String atr(const String &p_message, const StringName &p_context = "") const { return can_auto_translate() ? tr(p_message, p_context) : p_message; }
787
_FORCE_INLINE_ String atr_n(const String &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const {
788
if (can_auto_translate()) {
789
return tr_n(p_message, p_message_plural, p_n, p_context);
790
}
791
return p_n == 1 ? p_message : String(p_message_plural);
792
}
793
794
/* THREADING */
795
796
void call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
797
template <typename... VarArgs>
798
void call_deferred_thread_group(const StringName &p_method, VarArgs... p_args) {
799
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
800
const Variant *argptrs[sizeof...(p_args) + 1];
801
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
802
argptrs[i] = &args[i];
803
}
804
call_deferred_thread_groupp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
805
}
806
void set_deferred_thread_group(const StringName &p_property, const Variant &p_value);
807
void notify_deferred_thread_group(int p_notification);
808
809
void call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
810
template <typename... VarArgs>
811
void call_thread_safe(const StringName &p_method, VarArgs... p_args) {
812
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
813
const Variant *argptrs[sizeof...(p_args) + 1];
814
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
815
argptrs[i] = &args[i];
816
}
817
call_deferred_thread_groupp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
818
}
819
void set_thread_safe(const StringName &p_property, const Variant &p_value);
820
void notify_thread_safe(int p_notification);
821
822
// These inherited functions need proper multithread locking when overridden in Node.
823
#ifdef DEBUG_ENABLED
824
825
virtual void set_script(const Variant &p_script) override;
826
virtual Variant get_script() const override;
827
828
virtual bool has_meta(const StringName &p_name) const override;
829
virtual void set_meta(const StringName &p_name, const Variant &p_value) override;
830
virtual void remove_meta(const StringName &p_name) override;
831
virtual Variant get_meta(const StringName &p_name, const Variant &p_default = Variant()) const override;
832
virtual void get_meta_list(List<StringName> *p_list) const override;
833
834
virtual Error emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) override;
835
virtual bool has_signal(const StringName &p_name) const override;
836
virtual void get_signal_list(List<MethodInfo> *p_signals) const override;
837
virtual void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const override;
838
virtual void get_all_signal_connections(List<Connection> *p_connections) const override;
839
virtual int get_persistent_signal_connection_count() const override;
840
virtual void get_signals_connected_to_this(List<Connection> *p_connections) const override;
841
842
virtual Error connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags = 0) override;
843
virtual void disconnect(const StringName &p_signal, const Callable &p_callable) override;
844
virtual bool is_connected(const StringName &p_signal, const Callable &p_callable) const override;
845
virtual bool has_connections(const StringName &p_signal) const override;
846
#endif
847
Node();
848
~Node();
849
};
850
851
VARIANT_ENUM_CAST(Node::DuplicateFlags);
852
VARIANT_ENUM_CAST(Node::ProcessMode);
853
VARIANT_ENUM_CAST(Node::ProcessThreadGroup);
854
VARIANT_BITFIELD_CAST(Node::ProcessThreadMessages);
855
VARIANT_ENUM_CAST(Node::InternalMode);
856
VARIANT_ENUM_CAST(Node::PhysicsInterpolationMode);
857
VARIANT_ENUM_CAST(Node::AutoTranslateMode);
858
859
typedef HashSet<Node *, Node::Comparator> NodeSet;
860
861
// Template definitions must be in the header so they are always fully initialized before their usage.
862
// See this StackOverflow question for more information: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
863
864
template <typename... VarArgs>
865
Error Node::rpc(const StringName &p_method, VarArgs... p_args) {
866
return rpc_id(0, p_method, p_args...);
867
}
868
869
template <typename... VarArgs>
870
Error Node::rpc_id(int p_peer_id, const StringName &p_method, VarArgs... p_args) {
871
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
872
const Variant *argptrs[sizeof...(p_args) + 1];
873
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
874
argptrs[i] = &args[i];
875
}
876
return rpcp(p_peer_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
877
}
878
879
#ifdef DEBUG_ENABLED
880
#define ERR_THREAD_GUARD ERR_FAIL_COND_MSG(!is_accessible_from_caller_thread(), vformat("Caller thread can't call this function in this node (%s). Use call_deferred() or call_thread_group() instead.", get_description()));
881
#define ERR_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_accessible_from_caller_thread(), (m_ret), vformat("Caller thread can't call this function in this node (%s). Use call_deferred() or call_thread_group() instead.", get_description()));
882
#define ERR_MAIN_THREAD_GUARD ERR_FAIL_COND_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), vformat("This function in this node (%s) can only be accessed from the main thread. Use call_deferred() instead.", get_description()));
883
#define ERR_MAIN_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(is_inside_tree() && !is_current_thread_safe_for_nodes(), (m_ret), vformat("This function in this node (%s) can only be accessed from the main thread. Use call_deferred() instead.", get_description()));
884
#define ERR_READ_THREAD_GUARD ERR_FAIL_COND_MSG(!is_readable_from_caller_thread(), vformat("This function in this node (%s) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.", get_description()));
885
#define ERR_READ_THREAD_GUARD_V(m_ret) ERR_FAIL_COND_V_MSG(!is_readable_from_caller_thread(), (m_ret), vformat("This function in this node (%s) can only be accessed from either the main thread or a thread group. Use call_deferred() instead.", get_description()));
886
#else
887
#define ERR_THREAD_GUARD
888
#define ERR_THREAD_GUARD_V(m_ret)
889
#define ERR_MAIN_THREAD_GUARD
890
#define ERR_MAIN_THREAD_GUARD_V(m_ret)
891
#define ERR_READ_THREAD_GUARD
892
#define ERR_READ_THREAD_GUARD_V(m_ret)
893
#endif
894
895
// Add these macro to your class's 'get_configuration_warnings' function to have warnings show up in the scene tree inspector.
896
#define DEPRECATED_NODE_WARNING warnings.push_back(RTR("This node is marked as deprecated and will be removed in future versions.\nPlease check the Godot documentation for information about migration."));
897
#define EXPERIMENTAL_NODE_WARNING warnings.push_back(RTR("This node is marked as experimental and may be subject to removal or major changes in future versions."));
898
899