Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/scene_tree.h
9906 views
1
/**************************************************************************/
2
/* scene_tree.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/os/main_loop.h"
34
#include "core/os/thread_safe.h"
35
#include "core/templates/paged_allocator.h"
36
#include "core/templates/self_list.h"
37
#include "scene/main/scene_tree_fti.h"
38
#include "scene/resources/mesh.h"
39
40
#undef Window
41
42
class PackedScene;
43
class Node;
44
#ifndef _3D_DISABLED
45
class Node3D;
46
#endif
47
class Window;
48
class Material;
49
class Mesh;
50
class MultiplayerAPI;
51
class SceneDebugger;
52
class Tween;
53
class Viewport;
54
55
class SceneTreeTimer : public RefCounted {
56
GDCLASS(SceneTreeTimer, RefCounted);
57
58
double time_left = 0.0;
59
bool process_always = true;
60
bool process_in_physics = false;
61
bool ignore_time_scale = false;
62
63
protected:
64
static void _bind_methods();
65
66
public:
67
void set_time_left(double p_time);
68
double get_time_left() const;
69
70
void set_process_always(bool p_process_always);
71
bool is_process_always();
72
73
void set_process_in_physics(bool p_process_in_physics);
74
bool is_process_in_physics();
75
76
void set_ignore_time_scale(bool p_ignore);
77
bool is_ignoring_time_scale();
78
79
void release_connections();
80
};
81
82
class SceneTree : public MainLoop {
83
_THREAD_SAFE_CLASS_
84
85
GDCLASS(SceneTree, MainLoop);
86
87
public:
88
typedef void (*IdleCallback)();
89
90
private:
91
CallQueue::Allocator *process_group_call_queue_allocator = nullptr;
92
93
struct ProcessGroup {
94
CallQueue call_queue;
95
Vector<Node *> nodes;
96
Vector<Node *> physics_nodes;
97
bool node_order_dirty = true;
98
bool physics_node_order_dirty = true;
99
bool removed = false;
100
Node *owner = nullptr;
101
uint64_t last_pass = 0;
102
};
103
104
struct ProcessGroupSort {
105
_FORCE_INLINE_ bool operator()(const ProcessGroup *p_left, const ProcessGroup *p_right) const;
106
};
107
108
PagedAllocator<ProcessGroup, true> group_allocator; // Allocate groups on pages, to enhance cache usage.
109
110
LocalVector<ProcessGroup *> process_groups;
111
bool process_groups_dirty = true;
112
LocalVector<ProcessGroup *> local_process_group_cache; // Used when processing to group what needs to
113
uint64_t process_last_pass = 1;
114
115
ProcessGroup default_process_group;
116
117
bool node_threading_disabled = false;
118
119
struct Group {
120
Vector<Node *> nodes;
121
bool changed = false;
122
};
123
124
#ifndef _3D_DISABLED
125
struct ClientPhysicsInterpolation {
126
SelfList<Node3D>::List _node_3d_list;
127
void physics_process();
128
} _client_physics_interpolation;
129
#endif
130
131
Window *root = nullptr;
132
133
double physics_process_time = 0.0;
134
double process_time = 0.0;
135
bool accept_quit = true;
136
bool quit_on_go_back = true;
137
138
#ifdef DEBUG_ENABLED
139
bool debug_collisions_hint = false;
140
bool debug_paths_hint = false;
141
bool debug_navigation_hint = false;
142
#endif
143
bool paused = false;
144
bool suspended = false;
145
146
HashMap<StringName, Group> group_map;
147
bool _quit = false;
148
149
// Static so we can get directly instead of via SceneTree pointer.
150
static bool _physics_interpolation_enabled;
151
152
// Note that physics interpolation is hard coded to OFF in the editor,
153
// therefore we have a second bool to enable e.g. configuration warnings
154
// to only take effect when the project is using physics interpolation.
155
static bool _physics_interpolation_enabled_in_project;
156
157
SceneTreeFTI scene_tree_fti;
158
159
StringName tree_changed_name = "tree_changed";
160
StringName node_added_name = "node_added";
161
StringName node_removed_name = "node_removed";
162
StringName node_renamed_name = "node_renamed";
163
164
int64_t current_frame = 0;
165
int nodes_in_tree_count = 0;
166
167
#ifdef TOOLS_ENABLED
168
Node *edited_scene_root = nullptr;
169
#endif
170
struct UGCall {
171
StringName group;
172
StringName call;
173
174
static uint32_t hash(const UGCall &p_val) {
175
return p_val.group.hash() ^ p_val.call.hash();
176
}
177
bool operator==(const UGCall &p_with) const { return group == p_with.group && call == p_with.call; }
178
bool operator<(const UGCall &p_with) const { return group == p_with.group ? call < p_with.call : group < p_with.group; }
179
};
180
181
// Safety for when a node is deleted while a group is being called.
182
183
int nodes_removed_on_group_call_lock = 0;
184
HashSet<Node *> nodes_removed_on_group_call; // Skip erased nodes.
185
186
List<ObjectID> delete_queue;
187
188
uint64_t accessibility_upd_per_sec = 0;
189
bool accessibility_force_update = true;
190
HashSet<ObjectID> accessibility_change_queue;
191
uint64_t accessibility_last_update = 0;
192
193
HashMap<UGCall, Vector<Variant>, UGCall> unique_group_calls;
194
bool ugc_locked = false;
195
void _flush_ugc();
196
197
_FORCE_INLINE_ void _update_group_order(Group &g);
198
199
TypedArray<Node> _get_nodes_in_group(const StringName &p_group);
200
201
Node *current_scene = nullptr;
202
ObjectID prev_scene_id;
203
ObjectID pending_new_scene_id;
204
205
Color debug_collisions_color;
206
Color debug_collision_contact_color;
207
Color debug_paths_color;
208
float debug_paths_width = 1.0f;
209
Ref<ArrayMesh> debug_contact_mesh;
210
Ref<Material> debug_paths_material;
211
Ref<Material> collision_material;
212
int collision_debug_contacts;
213
214
void _flush_scene_change();
215
216
List<Ref<SceneTreeTimer>> timers;
217
List<Ref<Tween>> tweens;
218
219
///network///
220
221
Ref<MultiplayerAPI> multiplayer;
222
HashMap<NodePath, Ref<MultiplayerAPI>> custom_multiplayers;
223
bool multiplayer_poll = true;
224
225
static SceneTree *singleton;
226
friend class Node;
227
228
void tree_changed();
229
void node_added(Node *p_node);
230
void node_removed(Node *p_node);
231
void node_renamed(Node *p_node);
232
void process_timers(double p_delta, bool p_physics_frame);
233
void process_tweens(double p_delta, bool p_physics_frame);
234
235
Group *add_to_group(const StringName &p_group, Node *p_node);
236
void remove_from_group(const StringName &p_group, Node *p_node);
237
238
void _process_group(ProcessGroup *p_group, bool p_physics);
239
void _process_groups_thread(uint32_t p_index, bool p_physics);
240
void _process(bool p_physics);
241
242
void _remove_process_group(Node *p_node);
243
void _add_process_group(Node *p_node);
244
void _remove_node_from_process_group(Node *p_node, Node *p_owner);
245
void _add_node_to_process_group(Node *p_node, Node *p_owner);
246
247
void _call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
248
void _call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
249
250
void _flush_delete_queue();
251
// Optimization.
252
friend class CanvasItem;
253
friend class Node3D;
254
friend class Viewport;
255
256
SelfList<Node>::List xform_change_list;
257
258
#ifdef DEBUG_ENABLED // No live editor in release build.
259
friend class LiveEditor;
260
#endif
261
262
enum {
263
MAX_IDLE_CALLBACKS = 256
264
};
265
266
static IdleCallback idle_callbacks[MAX_IDLE_CALLBACKS];
267
static int idle_callback_count;
268
void _call_idle_callbacks();
269
270
void _main_window_focus_in();
271
void _main_window_close();
272
void _main_window_go_back();
273
274
enum CallInputType {
275
CALL_INPUT_TYPE_INPUT,
276
CALL_INPUT_TYPE_SHORTCUT_INPUT,
277
CALL_INPUT_TYPE_UNHANDLED_INPUT,
278
CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT,
279
};
280
281
//used by viewport
282
void _call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport);
283
284
protected:
285
void _notification(int p_notification);
286
static void _bind_methods();
287
288
public:
289
enum {
290
NOTIFICATION_TRANSFORM_CHANGED = 2000
291
};
292
293
enum GroupCallFlags {
294
GROUP_CALL_DEFAULT = 0,
295
GROUP_CALL_REVERSE = 1,
296
GROUP_CALL_DEFERRED = 2,
297
GROUP_CALL_UNIQUE = 4,
298
};
299
300
_FORCE_INLINE_ Window *get_root() const { return root; }
301
302
void call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount);
303
void notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification);
304
void set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value);
305
306
// `notify_group()` is immediate by default since Godot 4.0.
307
void notify_group(const StringName &p_group, int p_notification);
308
// `set_group()` is immediate by default since Godot 4.0.
309
void set_group(const StringName &p_group, const String &p_name, const Variant &p_value);
310
311
template <typename... VarArgs>
312
// `call_group()` is immediate by default since Godot 4.0.
313
void call_group(const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
314
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
315
const Variant *argptrs[sizeof...(p_args) + 1];
316
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
317
argptrs[i] = &args[i];
318
}
319
call_group_flagsp(GROUP_CALL_DEFAULT, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
320
}
321
322
template <typename... VarArgs>
323
void call_group_flags(uint32_t p_flags, const StringName &p_group, const StringName &p_function, VarArgs... p_args) {
324
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
325
const Variant *argptrs[sizeof...(p_args) + 1];
326
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
327
argptrs[i] = &args[i];
328
}
329
call_group_flagsp(p_flags, p_group, p_function, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
330
}
331
332
void flush_transform_notifications();
333
334
bool is_accessibility_enabled() const;
335
bool is_accessibility_supported() const;
336
void _accessibility_force_update();
337
void _accessibility_notify_change(const Node *p_node, bool p_remove = false);
338
void _flush_accessibility_changes();
339
void _process_accessibility_changes(DisplayServer::WindowID p_window_id);
340
341
virtual void initialize() override;
342
343
virtual void iteration_prepare() override;
344
345
virtual bool physics_process(double p_time) override;
346
virtual void iteration_end() override;
347
virtual bool process(double p_time) override;
348
349
virtual void finalize() override;
350
351
bool is_auto_accept_quit() const;
352
void set_auto_accept_quit(bool p_enable);
353
354
bool is_quit_on_go_back() const;
355
void set_quit_on_go_back(bool p_enable);
356
357
void quit(int p_exit_code = EXIT_SUCCESS);
358
359
_FORCE_INLINE_ double get_physics_process_time() const { return physics_process_time; }
360
_FORCE_INLINE_ double get_process_time() const { return process_time; }
361
362
void set_pause(bool p_enabled);
363
bool is_paused() const;
364
void set_suspend(bool p_enabled);
365
bool is_suspended() const;
366
367
#ifdef DEBUG_ENABLED
368
void set_debug_collisions_hint(bool p_enabled);
369
bool is_debugging_collisions_hint() const;
370
371
void set_debug_paths_hint(bool p_enabled);
372
bool is_debugging_paths_hint() const;
373
374
void set_debug_navigation_hint(bool p_enabled);
375
bool is_debugging_navigation_hint() const;
376
#else
377
void set_debug_collisions_hint(bool p_enabled) {}
378
bool is_debugging_collisions_hint() const { return false; }
379
380
void set_debug_paths_hint(bool p_enabled) {}
381
bool is_debugging_paths_hint() const { return false; }
382
383
void set_debug_navigation_hint(bool p_enabled) {}
384
bool is_debugging_navigation_hint() const { return false; }
385
#endif
386
387
void set_debug_collisions_color(const Color &p_color);
388
Color get_debug_collisions_color() const;
389
390
void set_debug_collision_contact_color(const Color &p_color);
391
Color get_debug_collision_contact_color() const;
392
393
void set_debug_paths_color(const Color &p_color);
394
Color get_debug_paths_color() const;
395
396
void set_debug_paths_width(float p_width);
397
float get_debug_paths_width() const;
398
399
Ref<Material> get_debug_paths_material();
400
Ref<Material> get_debug_collision_material();
401
Ref<ArrayMesh> get_debug_contact_mesh();
402
403
int get_collision_debug_contact_count() { return collision_debug_contacts; }
404
405
int64_t get_frame() const;
406
407
int get_node_count() const;
408
409
void queue_delete(Object *p_object);
410
411
void get_nodes_in_group(const StringName &p_group, List<Node *> *p_list);
412
Node *get_first_node_in_group(const StringName &p_group);
413
bool has_group(const StringName &p_identifier) const;
414
int get_node_count_in_group(const StringName &p_group) const;
415
416
//void change_scene(const String& p_path);
417
//Node *get_loaded_scene();
418
419
void set_edited_scene_root(Node *p_node);
420
Node *get_edited_scene_root() const;
421
422
void set_current_scene(Node *p_scene);
423
Node *get_current_scene() const;
424
Error change_scene_to_file(const String &p_path);
425
Error change_scene_to_packed(const Ref<PackedScene> &p_scene);
426
Error reload_current_scene();
427
void unload_current_scene();
428
429
Ref<SceneTreeTimer> create_timer(double p_delay_sec, bool p_process_always = true, bool p_process_in_physics = false, bool p_ignore_time_scale = false);
430
Ref<Tween> create_tween();
431
void remove_tween(const Ref<Tween> &p_tween);
432
TypedArray<Tween> get_processed_tweens();
433
434
//used by Main::start, don't use otherwise
435
void add_current_scene(Node *p_current);
436
437
static SceneTree *get_singleton() { return singleton; }
438
439
#ifdef TOOLS_ENABLED
440
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
441
#endif
442
443
//network API
444
445
Ref<MultiplayerAPI> get_multiplayer(const NodePath &p_for_path = NodePath()) const;
446
void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path = NodePath());
447
void set_multiplayer_poll_enabled(bool p_enabled);
448
bool is_multiplayer_poll_enabled() const;
449
450
static void add_idle_callback(IdleCallback p_callback);
451
452
void set_disable_node_threading(bool p_disable);
453
//default texture settings
454
455
void set_physics_interpolation_enabled(bool p_enabled);
456
bool is_physics_interpolation_enabled() const { return _physics_interpolation_enabled; }
457
458
// Different name to disambiguate fast static versions from the user bound versions.
459
static bool is_fti_enabled() { return _physics_interpolation_enabled; }
460
static bool is_fti_enabled_in_project() { return _physics_interpolation_enabled_in_project; }
461
462
#ifndef _3D_DISABLED
463
void client_physics_interpolation_add_node_3d(SelfList<Node3D> *p_elem);
464
void client_physics_interpolation_remove_node_3d(SelfList<Node3D> *p_elem);
465
#endif
466
467
SceneTreeFTI &get_scene_tree_fti() { return scene_tree_fti; }
468
469
SceneTree();
470
~SceneTree();
471
};
472
473
VARIANT_ENUM_CAST(SceneTree::GroupCallFlags);
474
475