scene_tree.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /**************************************************************************/
  2. /* scene_tree.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. #include "scene_tree.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/image_loader.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/object/message_queue.h"
  36. #include "core/object/worker_thread_pool.h"
  37. #include "core/os/os.h"
  38. #include "node.h"
  39. #include "scene/animation/tween.h"
  40. #include "scene/debugger/scene_debugger.h"
  41. #include "scene/gui/control.h"
  42. #include "scene/main/multiplayer_api.h"
  43. #include "scene/main/viewport.h"
  44. #include "scene/main/window.h"
  45. #include "scene/resources/environment.h"
  46. #include "scene/resources/image_texture.h"
  47. #include "scene/resources/material.h"
  48. #include "scene/resources/mesh.h"
  49. #include "scene/resources/packed_scene.h"
  50. #include "scene/resources/world_2d.h"
  51. #ifndef _3D_DISABLED
  52. #include "scene/3d/node_3d.h"
  53. #include "scene/resources/3d/world_3d.h"
  54. #endif // _3D_DISABLED
  55. #ifndef PHYSICS_2D_DISABLED
  56. #include "servers/physics_server_2d.h"
  57. #endif // PHYSICS_2D_DISABLED
  58. #ifndef PHYSICS_3D_DISABLED
  59. #include "servers/physics_server_3d.h"
  60. #endif // PHYSICS_3D_DISABLED
  61. void SceneTreeTimer::_bind_methods() {
  62. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  63. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  64. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s"), "set_time_left", "get_time_left");
  65. ADD_SIGNAL(MethodInfo("timeout"));
  66. }
  67. void SceneTreeTimer::set_time_left(double p_time) {
  68. time_left = p_time;
  69. }
  70. double SceneTreeTimer::get_time_left() const {
  71. return MAX(time_left, 0.0);
  72. }
  73. void SceneTreeTimer::set_process_always(bool p_process_always) {
  74. process_always = p_process_always;
  75. }
  76. bool SceneTreeTimer::is_process_always() {
  77. return process_always;
  78. }
  79. void SceneTreeTimer::set_process_in_physics(bool p_process_in_physics) {
  80. process_in_physics = p_process_in_physics;
  81. }
  82. bool SceneTreeTimer::is_process_in_physics() {
  83. return process_in_physics;
  84. }
  85. void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
  86. ignore_time_scale = p_ignore;
  87. }
  88. bool SceneTreeTimer::is_ignoring_time_scale() {
  89. return ignore_time_scale;
  90. }
  91. void SceneTreeTimer::release_connections() {
  92. List<Connection> signal_connections;
  93. get_all_signal_connections(&signal_connections);
  94. for (const Connection &connection : signal_connections) {
  95. disconnect(connection.signal.get_name(), connection.callable);
  96. }
  97. }
  98. SceneTreeTimer::SceneTreeTimer() {}
  99. #ifndef _3D_DISABLED
  100. // This should be called once per physics tick, to make sure the transform previous and current
  101. // is kept up to date on the few Node3Ds that are using client side physics interpolation.
  102. void SceneTree::ClientPhysicsInterpolation::physics_process() {
  103. for (SelfList<Node3D> *E = _node_3d_list.first(); E;) {
  104. Node3D *node_3d = E->self();
  105. SelfList<Node3D> *current = E;
  106. // Get the next element here BEFORE we potentially delete one.
  107. E = E->next();
  108. // This will return false if the Node3D has timed out ..
  109. // i.e. if get_global_transform_interpolated() has not been called
  110. // for a few seconds, we can delete from the list to keep processing
  111. // to a minimum.
  112. if (!node_3d->update_client_physics_interpolation_data()) {
  113. _node_3d_list.remove(current);
  114. }
  115. }
  116. }
  117. #endif // _3D_DISABLED
  118. void SceneTree::tree_changed() {
  119. emit_signal(tree_changed_name);
  120. }
  121. void SceneTree::node_added(Node *p_node) {
  122. emit_signal(node_added_name, p_node);
  123. }
  124. void SceneTree::node_removed(Node *p_node) {
  125. // Nodes can only be removed from the main thread.
  126. if (current_scene == p_node) {
  127. current_scene = nullptr;
  128. }
  129. emit_signal(node_removed_name, p_node);
  130. if (nodes_removed_on_group_call_lock) {
  131. nodes_removed_on_group_call.insert(p_node);
  132. }
  133. }
  134. void SceneTree::node_renamed(Node *p_node) {
  135. emit_signal(node_renamed_name, p_node);
  136. }
  137. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  138. _THREAD_SAFE_METHOD_
  139. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  140. if (!E) {
  141. E = group_map.insert(p_group, Group());
  142. }
  143. ERR_FAIL_COND_V_MSG(E->value.nodes.has(p_node), &E->value, "Already in group: " + p_group + ".");
  144. E->value.nodes.push_back(p_node);
  145. E->value.changed = true;
  146. return &E->value;
  147. }
  148. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  149. _THREAD_SAFE_METHOD_
  150. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  151. ERR_FAIL_COND(!E);
  152. E->value.nodes.erase(p_node);
  153. if (E->value.nodes.is_empty()) {
  154. group_map.remove(E);
  155. }
  156. }
  157. void SceneTree::make_group_changed(const StringName &p_group) {
  158. _THREAD_SAFE_METHOD_
  159. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  160. if (E) {
  161. E->value.changed = true;
  162. }
  163. }
  164. void SceneTree::flush_transform_notifications() {
  165. _THREAD_SAFE_METHOD_
  166. SelfList<Node> *n = xform_change_list.first();
  167. while (n) {
  168. Node *node = n->self();
  169. SelfList<Node> *nx = n->next();
  170. xform_change_list.remove(n);
  171. n = nx;
  172. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  173. }
  174. }
  175. bool SceneTree::is_accessibility_enabled() const {
  176. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_ACCESSIBILITY_SCREEN_READER)) {
  177. return false;
  178. }
  179. DisplayServer::AccessibilityMode accessibility_mode = DisplayServer::accessibility_get_mode();
  180. int screen_reader_acvite = DisplayServer::get_singleton()->accessibility_screen_reader_active();
  181. if ((accessibility_mode == DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) || ((accessibility_mode == DisplayServer::AccessibilityMode::ACCESSIBILITY_AUTO) && (screen_reader_acvite == 0))) {
  182. return false;
  183. }
  184. return true;
  185. }
  186. bool SceneTree::is_accessibility_supported() const {
  187. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_ACCESSIBILITY_SCREEN_READER)) {
  188. return false;
  189. }
  190. DisplayServer::AccessibilityMode accessibility_mode = DisplayServer::accessibility_get_mode();
  191. if (accessibility_mode == DisplayServer::AccessibilityMode::ACCESSIBILITY_DISABLED) {
  192. return false;
  193. }
  194. return true;
  195. }
  196. void SceneTree::_accessibility_force_update() {
  197. accessibility_force_update = true;
  198. }
  199. void SceneTree::_accessibility_notify_change(const Node *p_node, bool p_remove) {
  200. if (p_node) {
  201. if (p_remove) {
  202. accessibility_change_queue.erase(p_node->get_instance_id());
  203. } else {
  204. accessibility_change_queue.insert(p_node->get_instance_id());
  205. }
  206. }
  207. }
  208. void SceneTree::_process_accessibility_changes(DisplayServer::WindowID p_window_id) {
  209. // Process NOTIFICATION_ACCESSIBILITY_UPDATE.
  210. Vector<ObjectID> processed;
  211. for (const ObjectID &id : accessibility_change_queue) {
  212. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(id));
  213. if (!node || !node->get_window()) {
  214. processed.push_back(id);
  215. continue; // Invalid node, remove from list and skip.
  216. } else if (node->get_window()->get_window_id() != p_window_id) {
  217. continue; // Another window, skip.
  218. }
  219. node->notification(Node::NOTIFICATION_ACCESSIBILITY_UPDATE);
  220. processed.push_back(id);
  221. }
  222. // Track focus change.
  223. // Note: Do not use `Window::get_focused_window()`, it returns both native and embedded windows, and we only care about focused element in the currently processed native window.
  224. // Native window focus is handled in the DisplayServer, or AccessKit subclassing adapter.
  225. ObjectID oid = DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id);
  226. Window *w_this = (Window *)ObjectDB::get_instance(oid);
  227. if (w_this) {
  228. Window *w_focus = w_this->get_focused_subwindow();
  229. if (w_focus && !w_focus->is_part_of_edited_scene()) {
  230. w_this = w_focus;
  231. }
  232. RID new_focus_element;
  233. Control *n_focus = w_this->gui_get_focus_owner();
  234. if (n_focus && !n_focus->is_part_of_edited_scene()) {
  235. new_focus_element = n_focus->get_focused_accessibility_element();
  236. } else {
  237. new_focus_element = w_this->get_focused_accessibility_element();
  238. }
  239. DisplayServer::get_singleton()->accessibility_update_set_focus(new_focus_element);
  240. }
  241. // Cleanup.
  242. for (const ObjectID &id : processed) {
  243. accessibility_change_queue.erase(id);
  244. }
  245. }
  246. void SceneTree::_flush_accessibility_changes() {
  247. if (is_accessibility_enabled()) {
  248. uint64_t time = OS::get_singleton()->get_ticks_msec();
  249. if (!accessibility_force_update) {
  250. if (time - accessibility_last_update < 1000 / accessibility_upd_per_sec) {
  251. return;
  252. }
  253. }
  254. accessibility_force_update = false;
  255. accessibility_last_update = time;
  256. // Push update to the accessibility driver.
  257. DisplayServer::get_singleton()->accessibility_update_if_active(callable_mp(this, &SceneTree::_process_accessibility_changes));
  258. }
  259. }
  260. void SceneTree::_flush_ugc() {
  261. ugc_locked = true;
  262. while (unique_group_calls.size()) {
  263. HashMap<UGCall, Vector<Variant>, UGCall>::Iterator E = unique_group_calls.begin();
  264. const Variant **argptrs = (const Variant **)alloca(E->value.size() * sizeof(Variant *));
  265. for (int i = 0; i < E->value.size(); i++) {
  266. argptrs[i] = &E->value[i];
  267. }
  268. call_group_flagsp(GROUP_CALL_DEFAULT, E->key.group, E->key.call, argptrs, E->value.size());
  269. unique_group_calls.remove(E);
  270. }
  271. ugc_locked = false;
  272. }
  273. void SceneTree::_update_group_order(Group &g) {
  274. if (!g.changed) {
  275. return;
  276. }
  277. if (g.nodes.is_empty()) {
  278. return;
  279. }
  280. Node **gr_nodes = g.nodes.ptrw();
  281. int gr_node_count = g.nodes.size();
  282. SortArray<Node *, Node::Comparator> node_sort;
  283. node_sort.sort(gr_nodes, gr_node_count);
  284. g.changed = false;
  285. }
  286. void SceneTree::call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount) {
  287. Vector<Node *> nodes_copy;
  288. {
  289. _THREAD_SAFE_METHOD_
  290. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  291. if (!E) {
  292. return;
  293. }
  294. Group &g = E->value;
  295. if (g.nodes.is_empty()) {
  296. return;
  297. }
  298. if (p_call_flags & GROUP_CALL_UNIQUE && p_call_flags & GROUP_CALL_DEFERRED) {
  299. ERR_FAIL_COND(ugc_locked);
  300. UGCall ug;
  301. ug.call = p_function;
  302. ug.group = p_group;
  303. if (unique_group_calls.has(ug)) {
  304. return;
  305. }
  306. Vector<Variant> args;
  307. for (int i = 0; i < p_argcount; i++) {
  308. args.push_back(*p_args[i]);
  309. }
  310. unique_group_calls[ug] = args;
  311. return;
  312. }
  313. _update_group_order(g);
  314. nodes_copy = g.nodes;
  315. }
  316. Node **gr_nodes = nodes_copy.ptrw();
  317. int gr_node_count = nodes_copy.size();
  318. {
  319. _THREAD_SAFE_METHOD_
  320. nodes_removed_on_group_call_lock++;
  321. }
  322. if (p_call_flags & GROUP_CALL_REVERSE) {
  323. for (int i = gr_node_count - 1; i >= 0; i--) {
  324. if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
  325. continue;
  326. }
  327. Node *node = gr_nodes[i];
  328. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  329. Callable::CallError ce;
  330. node->callp(p_function, p_args, p_argcount, ce);
  331. if (unlikely(ce.error != Callable::CallError::CALL_OK && ce.error != Callable::CallError::CALL_ERROR_INVALID_METHOD)) {
  332. ERR_PRINT(vformat("Error calling group method on node \"%s\": %s.", node->get_name(), Variant::get_callable_error_text(Callable(node, p_function), p_args, p_argcount, ce)));
  333. }
  334. } else {
  335. MessageQueue::get_singleton()->push_callp(node, p_function, p_args, p_argcount);
  336. }
  337. }
  338. } else {
  339. for (int i = 0; i < gr_node_count; i++) {
  340. if (nodes_removed_on_group_call_lock && nodes_removed_on_group_call.has(gr_nodes[i])) {
  341. continue;
  342. }
  343. Node *node = gr_nodes[i];
  344. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  345. Callable::CallError ce;
  346. node->callp(p_function, p_args, p_argcount, ce);
  347. if (unlikely(ce.error != Callable::CallError::CALL_OK && ce.error != Callable::CallError::CALL_ERROR_INVALID_METHOD)) {
  348. ERR_PRINT(vformat("Error calling group method on node \"%s\": %s.", node->get_name(), Variant::get_callable_error_text(Callable(node, p_function), p_args, p_argcount, ce)));
  349. }
  350. } else {
  351. MessageQueue::get_singleton()->push_callp(node, p_function, p_args, p_argcount);
  352. }
  353. }
  354. }
  355. {
  356. _THREAD_SAFE_METHOD_
  357. nodes_removed_on_group_call_lock--;
  358. if (nodes_removed_on_group_call_lock == 0) {
  359. nodes_removed_on_group_call.clear();
  360. }
  361. }
  362. }
  363. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  364. Vector<Node *> nodes_copy;
  365. {
  366. _THREAD_SAFE_METHOD_
  367. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  368. if (!E) {
  369. return;
  370. }
  371. Group &g = E->value;
  372. if (g.nodes.is_empty()) {
  373. return;
  374. }
  375. _update_group_order(g);
  376. nodes_copy = g.nodes;
  377. }
  378. Node **gr_nodes = nodes_copy.ptrw();
  379. int gr_node_count = nodes_copy.size();
  380. {
  381. _THREAD_SAFE_METHOD_
  382. nodes_removed_on_group_call_lock++;
  383. }
  384. if (p_call_flags & GROUP_CALL_REVERSE) {
  385. for (int i = gr_node_count - 1; i >= 0; i--) {
  386. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  387. continue;
  388. }
  389. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  390. gr_nodes[i]->notification(p_notification, true);
  391. } else {
  392. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  393. }
  394. }
  395. } else {
  396. for (int i = 0; i < gr_node_count; i++) {
  397. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  398. continue;
  399. }
  400. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  401. gr_nodes[i]->notification(p_notification);
  402. } else {
  403. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  404. }
  405. }
  406. }
  407. {
  408. _THREAD_SAFE_METHOD_
  409. nodes_removed_on_group_call_lock--;
  410. if (nodes_removed_on_group_call_lock == 0) {
  411. nodes_removed_on_group_call.clear();
  412. }
  413. }
  414. }
  415. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  416. Vector<Node *> nodes_copy;
  417. {
  418. _THREAD_SAFE_METHOD_
  419. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  420. if (!E) {
  421. return;
  422. }
  423. Group &g = E->value;
  424. if (g.nodes.is_empty()) {
  425. return;
  426. }
  427. _update_group_order(g);
  428. nodes_copy = g.nodes;
  429. }
  430. Node **gr_nodes = nodes_copy.ptrw();
  431. int gr_node_count = nodes_copy.size();
  432. {
  433. _THREAD_SAFE_METHOD_
  434. nodes_removed_on_group_call_lock++;
  435. }
  436. if (p_call_flags & GROUP_CALL_REVERSE) {
  437. for (int i = gr_node_count - 1; i >= 0; i--) {
  438. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  439. continue;
  440. }
  441. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  442. gr_nodes[i]->set(p_name, p_value);
  443. } else {
  444. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  445. }
  446. }
  447. } else {
  448. for (int i = 0; i < gr_node_count; i++) {
  449. if (nodes_removed_on_group_call.has(gr_nodes[i])) {
  450. continue;
  451. }
  452. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  453. gr_nodes[i]->set(p_name, p_value);
  454. } else {
  455. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  456. }
  457. }
  458. }
  459. {
  460. _THREAD_SAFE_METHOD_
  461. nodes_removed_on_group_call_lock--;
  462. if (nodes_removed_on_group_call_lock == 0) {
  463. nodes_removed_on_group_call.clear();
  464. }
  465. }
  466. }
  467. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  468. notify_group_flags(GROUP_CALL_DEFAULT, p_group, p_notification);
  469. }
  470. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  471. set_group_flags(GROUP_CALL_DEFAULT, p_group, p_name, p_value);
  472. }
  473. void SceneTree::initialize() {
  474. ERR_FAIL_NULL(root);
  475. MainLoop::initialize();
  476. root->_set_tree(this);
  477. }
  478. void SceneTree::set_physics_interpolation_enabled(bool p_enabled) {
  479. // We never want interpolation in the editor.
  480. if (Engine::get_singleton()->is_editor_hint()) {
  481. p_enabled = false;
  482. }
  483. if (p_enabled == _physics_interpolation_enabled) {
  484. return;
  485. }
  486. _physics_interpolation_enabled = p_enabled;
  487. RenderingServer::get_singleton()->set_physics_interpolation_enabled(p_enabled);
  488. // Perform an auto reset on the root node for convenience for the user.
  489. if (root) {
  490. root->reset_physics_interpolation();
  491. }
  492. }
  493. bool SceneTree::is_physics_interpolation_enabled() const {
  494. return _physics_interpolation_enabled;
  495. }
  496. #ifndef _3D_DISABLED
  497. void SceneTree::client_physics_interpolation_add_node_3d(SelfList<Node3D> *p_elem) {
  498. // This ensures that _update_physics_interpolation_data() will be called at least once every
  499. // physics tick, to ensure the previous and current transforms are kept up to date.
  500. _client_physics_interpolation._node_3d_list.add(p_elem);
  501. }
  502. void SceneTree::client_physics_interpolation_remove_node_3d(SelfList<Node3D> *p_elem) {
  503. _client_physics_interpolation._node_3d_list.remove(p_elem);
  504. }
  505. #endif
  506. void SceneTree::iteration_prepare() {
  507. if (_physics_interpolation_enabled) {
  508. // Make sure any pending transforms from the last tick / frame
  509. // are flushed before pumping the interpolation prev and currents.
  510. flush_transform_notifications();
  511. RenderingServer::get_singleton()->tick();
  512. }
  513. }
  514. bool SceneTree::physics_process(double p_time) {
  515. current_frame++;
  516. flush_transform_notifications();
  517. if (MainLoop::physics_process(p_time)) {
  518. _quit = true;
  519. }
  520. physics_process_time = p_time;
  521. emit_signal(SNAME("physics_frame"));
  522. #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
  523. call_group(SNAME("_picking_viewports"), SNAME("_process_picking"));
  524. #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
  525. _process(true);
  526. _flush_ugc();
  527. MessageQueue::get_singleton()->flush(); //small little hack
  528. process_timers(p_time, true); //go through timers
  529. process_tweens(p_time, true);
  530. flush_transform_notifications();
  531. // This should happen last because any processing that deletes something beforehand might expect the object to be removed in the same frame.
  532. _flush_delete_queue();
  533. _call_idle_callbacks();
  534. return _quit;
  535. }
  536. void SceneTree::iteration_end() {
  537. // When physics interpolation is active, we want all pending transforms
  538. // to be flushed to the RenderingServer before finishing a physics tick.
  539. if (_physics_interpolation_enabled) {
  540. flush_transform_notifications();
  541. #ifndef _3D_DISABLED
  542. // Any objects performing client physics interpolation
  543. // should be given an opportunity to keep their previous transforms
  544. // up to date.
  545. _client_physics_interpolation.physics_process();
  546. #endif
  547. }
  548. }
  549. bool SceneTree::process(double p_time) {
  550. if (MainLoop::process(p_time)) {
  551. _quit = true;
  552. }
  553. process_time = p_time;
  554. if (multiplayer_poll) {
  555. multiplayer->poll();
  556. for (KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  557. E.value->poll();
  558. }
  559. }
  560. emit_signal(SNAME("process_frame"));
  561. MessageQueue::get_singleton()->flush(); //small little hack
  562. flush_transform_notifications();
  563. _process(false);
  564. _flush_ugc();
  565. MessageQueue::get_singleton()->flush(); //small little hack
  566. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  567. if (unlikely(pending_new_scene_id.is_valid())) {
  568. _flush_scene_change();
  569. }
  570. process_timers(p_time, false); //go through timers
  571. process_tweens(p_time, false);
  572. flush_transform_notifications(); // Additional transforms after timers update.
  573. // This should happen last because any processing that deletes something beforehand might expect the object to be removed in the same frame.
  574. _flush_delete_queue();
  575. _flush_accessibility_changes();
  576. _call_idle_callbacks();
  577. #ifdef TOOLS_ENABLED
  578. #ifndef _3D_DISABLED
  579. if (Engine::get_singleton()->is_editor_hint()) {
  580. String env_path = GLOBAL_GET(SNAME("rendering/environment/defaults/default_environment"));
  581. env_path = env_path.strip_edges(); // User may have added a space or two.
  582. bool can_load = true;
  583. if (env_path.begins_with("uid://")) {
  584. // If an uid path, ensure it is mapped to a resource which could not be
  585. // the case if the editor is still scanning the filesystem.
  586. ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(env_path);
  587. can_load = ResourceUID::get_singleton()->has_id(id);
  588. if (can_load) {
  589. env_path = ResourceUID::get_singleton()->get_id_path(id);
  590. }
  591. }
  592. if (can_load) {
  593. String cpath;
  594. Ref<Environment> fallback = get_root()->get_world_3d()->get_fallback_environment();
  595. if (fallback.is_valid()) {
  596. cpath = fallback->get_path();
  597. }
  598. if (cpath != env_path) {
  599. if (!env_path.is_empty()) {
  600. fallback = ResourceLoader::load(env_path);
  601. if (fallback.is_null()) {
  602. //could not load fallback, set as empty
  603. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  604. }
  605. } else {
  606. fallback.unref();
  607. }
  608. get_root()->get_world_3d()->set_fallback_environment(fallback);
  609. }
  610. }
  611. }
  612. #endif // _3D_DISABLED
  613. #endif // TOOLS_ENABLED
  614. if (_physics_interpolation_enabled) {
  615. RenderingServer::get_singleton()->pre_draw(true);
  616. }
  617. return _quit;
  618. }
  619. void SceneTree::process_timers(double p_delta, bool p_physics_frame) {
  620. _THREAD_SAFE_METHOD_
  621. const List<Ref<SceneTreeTimer>>::Element *L = timers.back(); // Last element.
  622. const double unscaled_delta = Engine::get_singleton()->get_process_step();
  623. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
  624. List<Ref<SceneTreeTimer>>::Element *N = E->next();
  625. Ref<SceneTreeTimer> timer = E->get();
  626. if ((paused && !timer->is_process_always()) || (timer->is_process_in_physics() != p_physics_frame)) {
  627. if (E == L) {
  628. break; // Break on last, so if new timers were added during list traversal, ignore them.
  629. }
  630. E = N;
  631. continue;
  632. }
  633. double time_left = timer->get_time_left();
  634. time_left -= timer->is_ignoring_time_scale() ? unscaled_delta : p_delta;
  635. timer->set_time_left(time_left);
  636. if (time_left <= 0) {
  637. E->get()->emit_signal(SNAME("timeout"));
  638. timers.erase(E);
  639. }
  640. if (E == L) {
  641. break; // Break on last, so if new timers were added during list traversal, ignore them.
  642. }
  643. E = N;
  644. }
  645. }
  646. void SceneTree::process_tweens(double p_delta, bool p_physics) {
  647. _THREAD_SAFE_METHOD_
  648. // This methods works similarly to how SceneTreeTimers are handled.
  649. const List<Ref<Tween>>::Element *L = tweens.back();
  650. const double unscaled_delta = Engine::get_singleton()->get_process_step();
  651. for (List<Ref<Tween>>::Element *E = tweens.front(); E;) {
  652. List<Ref<Tween>>::Element *N = E->next();
  653. Ref<Tween> &tween = E->get();
  654. // Don't process if paused or process mode doesn't match.
  655. if (!tween->can_process(paused) || (p_physics == (tween->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
  656. if (E == L) {
  657. break;
  658. }
  659. E = N;
  660. continue;
  661. }
  662. if (!tween->step(tween->is_ignoring_time_scale() ? unscaled_delta : p_delta)) {
  663. tween->clear();
  664. tweens.erase(E);
  665. }
  666. if (E == L) {
  667. break;
  668. }
  669. E = N;
  670. }
  671. }
  672. void SceneTree::finalize() {
  673. _flush_delete_queue();
  674. _flush_ugc();
  675. if (root) {
  676. root->_set_tree(nullptr);
  677. root->_propagate_after_exit_tree();
  678. memdelete(root); //delete root
  679. root = nullptr;
  680. // In case deletion of some objects was queued when destructing the `root`.
  681. // E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
  682. _flush_delete_queue();
  683. }
  684. MainLoop::finalize();
  685. // Cleanup timers.
  686. for (Ref<SceneTreeTimer> &timer : timers) {
  687. timer->release_connections();
  688. }
  689. timers.clear();
  690. // Cleanup tweens.
  691. for (Ref<Tween> &tween : tweens) {
  692. tween->clear();
  693. }
  694. tweens.clear();
  695. }
  696. void SceneTree::quit(int p_exit_code) {
  697. _THREAD_SAFE_METHOD_
  698. OS::get_singleton()->set_exit_code(p_exit_code);
  699. _quit = true;
  700. }
  701. void SceneTree::_main_window_close() {
  702. if (accept_quit) {
  703. _quit = true;
  704. }
  705. }
  706. void SceneTree::_main_window_go_back() {
  707. if (quit_on_go_back) {
  708. _quit = true;
  709. }
  710. }
  711. void SceneTree::_main_window_focus_in() {
  712. Input *id = Input::get_singleton();
  713. if (id) {
  714. id->ensure_touch_mouse_raised();
  715. }
  716. }
  717. void SceneTree::_notification(int p_notification) {
  718. switch (p_notification) {
  719. case NOTIFICATION_TRANSLATION_CHANGED: {
  720. get_root()->propagate_notification(p_notification);
  721. } break;
  722. case NOTIFICATION_OS_MEMORY_WARNING:
  723. case NOTIFICATION_OS_IME_UPDATE:
  724. case NOTIFICATION_WM_ABOUT:
  725. case NOTIFICATION_CRASH:
  726. case NOTIFICATION_APPLICATION_RESUMED:
  727. case NOTIFICATION_APPLICATION_PAUSED:
  728. case NOTIFICATION_APPLICATION_FOCUS_IN:
  729. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  730. // Pass these to nodes, since they are mirrored.
  731. get_root()->propagate_notification(p_notification);
  732. } break;
  733. }
  734. }
  735. bool SceneTree::is_auto_accept_quit() const {
  736. return accept_quit;
  737. }
  738. void SceneTree::set_auto_accept_quit(bool p_enable) {
  739. accept_quit = p_enable;
  740. }
  741. bool SceneTree::is_quit_on_go_back() const {
  742. return quit_on_go_back;
  743. }
  744. void SceneTree::set_quit_on_go_back(bool p_enable) {
  745. quit_on_go_back = p_enable;
  746. }
  747. #ifdef DEBUG_ENABLED
  748. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  749. debug_collisions_hint = p_enabled;
  750. }
  751. bool SceneTree::is_debugging_collisions_hint() const {
  752. return debug_collisions_hint;
  753. }
  754. void SceneTree::set_debug_paths_hint(bool p_enabled) {
  755. debug_paths_hint = p_enabled;
  756. }
  757. bool SceneTree::is_debugging_paths_hint() const {
  758. return debug_paths_hint;
  759. }
  760. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  761. debug_navigation_hint = p_enabled;
  762. }
  763. bool SceneTree::is_debugging_navigation_hint() const {
  764. return debug_navigation_hint;
  765. }
  766. #endif
  767. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  768. debug_collisions_color = p_color;
  769. }
  770. Color SceneTree::get_debug_collisions_color() const {
  771. return debug_collisions_color;
  772. }
  773. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  774. debug_collision_contact_color = p_color;
  775. }
  776. Color SceneTree::get_debug_collision_contact_color() const {
  777. return debug_collision_contact_color;
  778. }
  779. void SceneTree::set_debug_paths_color(const Color &p_color) {
  780. debug_paths_color = p_color;
  781. }
  782. Color SceneTree::get_debug_paths_color() const {
  783. return debug_paths_color;
  784. }
  785. void SceneTree::set_debug_paths_width(float p_width) {
  786. debug_paths_width = p_width;
  787. }
  788. float SceneTree::get_debug_paths_width() const {
  789. return debug_paths_width;
  790. }
  791. Ref<Material> SceneTree::get_debug_paths_material() {
  792. _THREAD_SAFE_METHOD_
  793. if (debug_paths_material.is_valid()) {
  794. return debug_paths_material;
  795. }
  796. Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  797. _debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  798. _debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  799. _debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  800. _debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  801. _debug_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  802. _debug_material->set_albedo(get_debug_paths_color());
  803. debug_paths_material = _debug_material;
  804. return debug_paths_material;
  805. }
  806. Ref<Material> SceneTree::get_debug_collision_material() {
  807. _THREAD_SAFE_METHOD_
  808. if (collision_material.is_valid()) {
  809. return collision_material;
  810. }
  811. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  812. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  813. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  814. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  815. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  816. line_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  817. line_material->set_albedo(get_debug_collisions_color());
  818. collision_material = line_material;
  819. return collision_material;
  820. }
  821. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  822. _THREAD_SAFE_METHOD_
  823. if (debug_contact_mesh.is_valid()) {
  824. return debug_contact_mesh;
  825. }
  826. debug_contact_mesh.instantiate();
  827. Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  828. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  829. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  830. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  831. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  832. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  833. mat->set_albedo(get_debug_collision_contact_color());
  834. Vector3 diamond[6] = {
  835. Vector3(-1, 0, 0),
  836. Vector3(1, 0, 0),
  837. Vector3(0, -1, 0),
  838. Vector3(0, 1, 0),
  839. Vector3(0, 0, -1),
  840. Vector3(0, 0, 1)
  841. };
  842. /* clang-format off */
  843. int diamond_faces[8 * 3] = {
  844. 0, 2, 4,
  845. 0, 3, 4,
  846. 1, 2, 4,
  847. 1, 3, 4,
  848. 0, 2, 5,
  849. 0, 3, 5,
  850. 1, 2, 5,
  851. 1, 3, 5,
  852. };
  853. /* clang-format on */
  854. Vector<int> indices;
  855. for (int i = 0; i < 8 * 3; i++) {
  856. indices.push_back(diamond_faces[i]);
  857. }
  858. Vector<Vector3> vertices;
  859. for (int i = 0; i < 6; i++) {
  860. vertices.push_back(diamond[i] * 0.1);
  861. }
  862. Array arr;
  863. arr.resize(Mesh::ARRAY_MAX);
  864. arr[Mesh::ARRAY_VERTEX] = vertices;
  865. arr[Mesh::ARRAY_INDEX] = indices;
  866. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  867. debug_contact_mesh->surface_set_material(0, mat);
  868. return debug_contact_mesh;
  869. }
  870. void SceneTree::set_pause(bool p_enabled) {
  871. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Pause can only be set from the main thread.");
  872. ERR_FAIL_COND_MSG(suspended, "Pause state cannot be modified while suspended.");
  873. if (p_enabled == paused) {
  874. return;
  875. }
  876. paused = p_enabled;
  877. #ifndef PHYSICS_3D_DISABLED
  878. PhysicsServer3D::get_singleton()->set_active(!p_enabled);
  879. #endif // PHYSICS_3D_DISABLED
  880. #ifndef PHYSICS_2D_DISABLED
  881. PhysicsServer2D::get_singleton()->set_active(!p_enabled);
  882. #endif // PHYSICS_2D_DISABLED
  883. if (get_root()) {
  884. get_root()->_propagate_pause_notification(p_enabled);
  885. }
  886. }
  887. bool SceneTree::is_paused() const {
  888. return paused;
  889. }
  890. void SceneTree::set_suspend(bool p_enabled) {
  891. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Suspend can only be set from the main thread.");
  892. if (p_enabled == suspended) {
  893. return;
  894. }
  895. suspended = p_enabled;
  896. Engine::get_singleton()->set_freeze_time_scale(p_enabled);
  897. #ifndef PHYSICS_3D_DISABLED
  898. PhysicsServer3D::get_singleton()->set_active(!p_enabled && !paused);
  899. #endif // PHYSICS_3D_DISABLED
  900. #ifndef PHYSICS_2D_DISABLED
  901. PhysicsServer2D::get_singleton()->set_active(!p_enabled && !paused);
  902. #endif // PHYSICS_2D_DISABLED
  903. if (get_root()) {
  904. get_root()->_propagate_suspend_notification(p_enabled);
  905. }
  906. }
  907. bool SceneTree::is_suspended() const {
  908. return suspended;
  909. }
  910. void SceneTree::_process_group(ProcessGroup *p_group, bool p_physics) {
  911. // When reading this function, keep in mind that this code must work in a way where
  912. // if any node is removed, this needs to continue working.
  913. p_group->call_queue.flush(); // Flush messages before processing.
  914. Vector<Node *> &nodes = p_physics ? p_group->physics_nodes : p_group->nodes;
  915. if (nodes.is_empty()) {
  916. return;
  917. }
  918. if (p_physics) {
  919. if (p_group->physics_node_order_dirty) {
  920. nodes.sort_custom<Node::ComparatorWithPhysicsPriority>();
  921. p_group->physics_node_order_dirty = false;
  922. }
  923. } else {
  924. if (p_group->node_order_dirty) {
  925. nodes.sort_custom<Node::ComparatorWithPriority>();
  926. p_group->node_order_dirty = false;
  927. }
  928. }
  929. // Make a copy, so if nodes are added/removed from process, this does not break
  930. Vector<Node *> nodes_copy = nodes;
  931. uint32_t node_count = nodes_copy.size();
  932. Node **nodes_ptr = (Node **)nodes_copy.ptr(); // Force cast, pointer will not change.
  933. for (uint32_t i = 0; i < node_count; i++) {
  934. Node *n = nodes_ptr[i];
  935. if (nodes_removed_on_group_call.has(n)) {
  936. // Node may have been removed during process, skip it.
  937. // Keep in mind removals can only happen on the main thread.
  938. continue;
  939. }
  940. if (!n->can_process() || !n->is_inside_tree()) {
  941. continue;
  942. }
  943. if (p_physics) {
  944. if (n->is_physics_processing_internal()) {
  945. n->notification(Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  946. }
  947. if (n->is_physics_processing()) {
  948. n->notification(Node::NOTIFICATION_PHYSICS_PROCESS);
  949. }
  950. } else {
  951. if (n->is_processing_internal()) {
  952. n->notification(Node::NOTIFICATION_INTERNAL_PROCESS);
  953. }
  954. if (n->is_processing()) {
  955. n->notification(Node::NOTIFICATION_PROCESS);
  956. }
  957. }
  958. }
  959. p_group->call_queue.flush(); // Flush messages also after processing (for potential deferred calls).
  960. }
  961. void SceneTree::_process_groups_thread(uint32_t p_index, bool p_physics) {
  962. Node::current_process_thread_group = local_process_group_cache[p_index]->owner;
  963. _process_group(local_process_group_cache[p_index], p_physics);
  964. Node::current_process_thread_group = nullptr;
  965. }
  966. void SceneTree::_process(bool p_physics) {
  967. if (process_groups_dirty) {
  968. {
  969. // First, remove dirty groups.
  970. // This needs to be done when not processing to avoid problems.
  971. ProcessGroup **pg_ptr = (ProcessGroup **)process_groups.ptr(); // discard constness.
  972. uint32_t pg_count = process_groups.size();
  973. for (uint32_t i = 0; i < pg_count; i++) {
  974. if (pg_ptr[i]->removed) {
  975. // Replace removed with last.
  976. pg_ptr[i] = pg_ptr[pg_count - 1];
  977. // Retry
  978. i--;
  979. pg_count--;
  980. }
  981. }
  982. if (pg_count != process_groups.size()) {
  983. process_groups.resize(pg_count);
  984. }
  985. }
  986. {
  987. // Then, re-sort groups.
  988. process_groups.sort_custom<ProcessGroupSort>();
  989. }
  990. process_groups_dirty = false;
  991. }
  992. // Cache the group count, because during processing new groups may be added.
  993. // They will be added at the end, hence for consistency they will be ignored by this process loop.
  994. // No group will be removed from the array during processing (this is done earlier in this function by marking the groups dirty).
  995. uint32_t group_count = process_groups.size();
  996. if (group_count == 0) {
  997. return;
  998. }
  999. process_last_pass++; // Increment pass
  1000. uint32_t from = 0;
  1001. uint32_t process_count = 0;
  1002. nodes_removed_on_group_call_lock++;
  1003. int current_order = process_groups[0]->owner ? process_groups[0]->owner->data.process_thread_group_order : 0;
  1004. bool current_threaded = process_groups[0]->owner ? process_groups[0]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD : false;
  1005. for (uint32_t i = 0; i <= group_count; i++) {
  1006. int order = i < group_count && process_groups[i]->owner ? process_groups[i]->owner->data.process_thread_group_order : 0;
  1007. bool threaded = i < group_count && process_groups[i]->owner ? process_groups[i]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD : false;
  1008. if (i == group_count || current_order != order || current_threaded != threaded) {
  1009. if (process_count > 0) {
  1010. // Proceed to process the group.
  1011. bool using_threads = process_groups[from]->owner && process_groups[from]->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD && !node_threading_disabled;
  1012. if (using_threads) {
  1013. local_process_group_cache.clear();
  1014. }
  1015. for (uint32_t j = from; j < i; j++) {
  1016. if (process_groups[j]->last_pass == process_last_pass) {
  1017. if (using_threads) {
  1018. local_process_group_cache.push_back(process_groups[j]);
  1019. } else {
  1020. _process_group(process_groups[j], p_physics);
  1021. }
  1022. }
  1023. }
  1024. if (using_threads) {
  1025. WorkerThreadPool::GroupID id = WorkerThreadPool::get_singleton()->add_template_group_task(this, &SceneTree::_process_groups_thread, p_physics, local_process_group_cache.size(), -1, true);
  1026. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(id);
  1027. }
  1028. }
  1029. if (i == group_count) {
  1030. // This one is invalid, no longer process
  1031. break;
  1032. }
  1033. from = i;
  1034. current_threaded = threaded;
  1035. current_order = order;
  1036. }
  1037. if (process_groups[i]->removed) {
  1038. continue;
  1039. }
  1040. ProcessGroup *pg = process_groups[i];
  1041. // Validate group for processing
  1042. bool process_valid = false;
  1043. if (p_physics) {
  1044. if (!pg->physics_nodes.is_empty()) {
  1045. process_valid = true;
  1046. } else if ((pg == &default_process_group || (pg->owner != nullptr && pg->owner->data.process_thread_messages.has_flag(Node::FLAG_PROCESS_THREAD_MESSAGES_PHYSICS))) && pg->call_queue.has_messages()) {
  1047. process_valid = true;
  1048. }
  1049. } else {
  1050. if (!pg->nodes.is_empty()) {
  1051. process_valid = true;
  1052. } else if ((pg == &default_process_group || (pg->owner != nullptr && pg->owner->data.process_thread_messages.has_flag(Node::FLAG_PROCESS_THREAD_MESSAGES))) && pg->call_queue.has_messages()) {
  1053. process_valid = true;
  1054. }
  1055. }
  1056. if (process_valid) {
  1057. pg->last_pass = process_last_pass; // Enable for processing
  1058. process_count++;
  1059. }
  1060. }
  1061. nodes_removed_on_group_call_lock--;
  1062. if (nodes_removed_on_group_call_lock == 0) {
  1063. nodes_removed_on_group_call.clear();
  1064. }
  1065. }
  1066. bool SceneTree::ProcessGroupSort::operator()(const ProcessGroup *p_left, const ProcessGroup *p_right) const {
  1067. int left_order = p_left->owner ? p_left->owner->data.process_thread_group_order : 0;
  1068. int right_order = p_right->owner ? p_right->owner->data.process_thread_group_order : 0;
  1069. if (left_order == right_order) {
  1070. int left_threaded = p_left->owner != nullptr && p_left->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD ? 0 : 1;
  1071. int right_threaded = p_right->owner != nullptr && p_right->owner->data.process_thread_group == Node::PROCESS_THREAD_GROUP_SUB_THREAD ? 0 : 1;
  1072. return left_threaded < right_threaded;
  1073. } else {
  1074. return left_order < right_order;
  1075. }
  1076. }
  1077. void SceneTree::_remove_process_group(Node *p_node) {
  1078. _THREAD_SAFE_METHOD_
  1079. ProcessGroup *pg = (ProcessGroup *)p_node->data.process_group;
  1080. ERR_FAIL_NULL(pg);
  1081. ERR_FAIL_COND(pg->removed);
  1082. pg->removed = true;
  1083. pg->owner = nullptr;
  1084. p_node->data.process_group = nullptr;
  1085. process_groups_dirty = true;
  1086. }
  1087. void SceneTree::_add_process_group(Node *p_node) {
  1088. _THREAD_SAFE_METHOD_
  1089. ERR_FAIL_NULL(p_node);
  1090. ProcessGroup *pg = memnew(ProcessGroup);
  1091. pg->owner = p_node;
  1092. p_node->data.process_group = pg;
  1093. process_groups.push_back(pg);
  1094. process_groups_dirty = true;
  1095. }
  1096. void SceneTree::_remove_node_from_process_group(Node *p_node, Node *p_owner) {
  1097. _THREAD_SAFE_METHOD_
  1098. ProcessGroup *pg = p_owner ? (ProcessGroup *)p_owner->data.process_group : &default_process_group;
  1099. if (p_node->is_processing() || p_node->is_processing_internal()) {
  1100. bool found = pg->nodes.erase(p_node);
  1101. ERR_FAIL_COND(!found);
  1102. }
  1103. if (p_node->is_physics_processing() || p_node->is_physics_processing_internal()) {
  1104. bool found = pg->physics_nodes.erase(p_node);
  1105. ERR_FAIL_COND(!found);
  1106. }
  1107. }
  1108. void SceneTree::_add_node_to_process_group(Node *p_node, Node *p_owner) {
  1109. _THREAD_SAFE_METHOD_
  1110. ProcessGroup *pg = p_owner ? (ProcessGroup *)p_owner->data.process_group : &default_process_group;
  1111. if (p_node->is_processing() || p_node->is_processing_internal()) {
  1112. pg->nodes.push_back(p_node);
  1113. pg->node_order_dirty = true;
  1114. }
  1115. if (p_node->is_physics_processing() || p_node->is_physics_processing_internal()) {
  1116. pg->physics_nodes.push_back(p_node);
  1117. pg->physics_node_order_dirty = true;
  1118. }
  1119. }
  1120. void SceneTree::_call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport) {
  1121. Vector<Node *> nodes_copy;
  1122. {
  1123. _THREAD_SAFE_METHOD_
  1124. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1125. if (!E) {
  1126. return;
  1127. }
  1128. Group &g = E->value;
  1129. if (g.nodes.is_empty()) {
  1130. return;
  1131. }
  1132. _update_group_order(g);
  1133. //copy, so copy on write happens in case something is removed from process while being called
  1134. //performance is not lost because only if something is added/removed the vector is copied.
  1135. nodes_copy = g.nodes;
  1136. }
  1137. int gr_node_count = nodes_copy.size();
  1138. Node **gr_nodes = nodes_copy.ptrw();
  1139. {
  1140. _THREAD_SAFE_METHOD_
  1141. nodes_removed_on_group_call_lock++;
  1142. }
  1143. Vector<ObjectID> no_context_node_ids; // Nodes may be deleted due to this shortcut input.
  1144. for (int i = gr_node_count - 1; i >= 0; i--) {
  1145. if (p_viewport->is_input_handled()) {
  1146. break;
  1147. }
  1148. Node *n = gr_nodes[i];
  1149. if (nodes_removed_on_group_call.has(n)) {
  1150. continue;
  1151. }
  1152. if (!n->can_process()) {
  1153. continue;
  1154. }
  1155. switch (p_call_type) {
  1156. case CALL_INPUT_TYPE_INPUT:
  1157. n->_call_input(p_input);
  1158. break;
  1159. case CALL_INPUT_TYPE_SHORTCUT_INPUT: {
  1160. const Control *c = Object::cast_to<Control>(n);
  1161. if (c) {
  1162. // If calling shortcut input on a control, ensure it respects the shortcut context.
  1163. // Shortcut context (based on focus) only makes sense for controls (UI), so don't need to worry about it for nodes
  1164. if (c->get_shortcut_context() == nullptr) {
  1165. no_context_node_ids.append(n->get_instance_id());
  1166. continue;
  1167. }
  1168. if (!c->is_focus_owner_in_shortcut_context()) {
  1169. continue;
  1170. }
  1171. }
  1172. n->_call_shortcut_input(p_input);
  1173. break;
  1174. }
  1175. case CALL_INPUT_TYPE_UNHANDLED_INPUT:
  1176. n->_call_unhandled_input(p_input);
  1177. break;
  1178. case CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT:
  1179. n->_call_unhandled_key_input(p_input);
  1180. break;
  1181. }
  1182. }
  1183. for (const ObjectID &id : no_context_node_ids) {
  1184. if (p_viewport->is_input_handled()) {
  1185. break;
  1186. }
  1187. Node *n = ObjectDB::get_instance<Node>(id);
  1188. if (n) {
  1189. n->_call_shortcut_input(p_input);
  1190. }
  1191. }
  1192. {
  1193. _THREAD_SAFE_METHOD_
  1194. nodes_removed_on_group_call_lock--;
  1195. if (nodes_removed_on_group_call_lock == 0) {
  1196. nodes_removed_on_group_call.clear();
  1197. }
  1198. }
  1199. }
  1200. void SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1201. r_error.error = Callable::CallError::CALL_OK;
  1202. ERR_FAIL_COND(p_argcount < 3);
  1203. ERR_FAIL_COND(!p_args[0]->is_num());
  1204. ERR_FAIL_COND(!p_args[1]->is_string());
  1205. ERR_FAIL_COND(!p_args[2]->is_string());
  1206. int flags = *p_args[0];
  1207. StringName group = *p_args[1];
  1208. StringName method = *p_args[2];
  1209. call_group_flagsp(flags, group, method, p_args + 3, p_argcount - 3);
  1210. }
  1211. void SceneTree::_call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1212. r_error.error = Callable::CallError::CALL_OK;
  1213. ERR_FAIL_COND(p_argcount < 2);
  1214. ERR_FAIL_COND(!p_args[0]->is_string());
  1215. ERR_FAIL_COND(!p_args[1]->is_string());
  1216. StringName group = *p_args[0];
  1217. StringName method = *p_args[1];
  1218. call_group_flagsp(GROUP_CALL_DEFAULT, group, method, p_args + 2, p_argcount - 2);
  1219. }
  1220. int64_t SceneTree::get_frame() const {
  1221. return current_frame;
  1222. }
  1223. TypedArray<Node> SceneTree::_get_nodes_in_group(const StringName &p_group) {
  1224. _THREAD_SAFE_METHOD_
  1225. TypedArray<Node> ret;
  1226. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1227. if (!E) {
  1228. return ret;
  1229. }
  1230. _update_group_order(E->value); //update order just in case
  1231. int nc = E->value.nodes.size();
  1232. if (nc == 0) {
  1233. return ret;
  1234. }
  1235. ret.resize(nc);
  1236. Node **ptr = E->value.nodes.ptrw();
  1237. for (int i = 0; i < nc; i++) {
  1238. ret[i] = ptr[i];
  1239. }
  1240. return ret;
  1241. }
  1242. bool SceneTree::has_group(const StringName &p_identifier) const {
  1243. _THREAD_SAFE_METHOD_
  1244. return group_map.has(p_identifier);
  1245. }
  1246. int SceneTree::get_node_count_in_group(const StringName &p_group) const {
  1247. _THREAD_SAFE_METHOD_
  1248. HashMap<StringName, Group>::ConstIterator E = group_map.find(p_group);
  1249. if (!E) {
  1250. return 0;
  1251. }
  1252. return E->value.nodes.size();
  1253. }
  1254. Node *SceneTree::get_first_node_in_group(const StringName &p_group) {
  1255. _THREAD_SAFE_METHOD_
  1256. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1257. if (!E) {
  1258. return nullptr; // No group.
  1259. }
  1260. _update_group_order(E->value); // Update order just in case.
  1261. if (E->value.nodes.is_empty()) {
  1262. return nullptr;
  1263. }
  1264. return E->value.nodes[0];
  1265. }
  1266. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  1267. _THREAD_SAFE_METHOD_
  1268. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  1269. if (!E) {
  1270. return;
  1271. }
  1272. _update_group_order(E->value); //update order just in case
  1273. int nc = E->value.nodes.size();
  1274. if (nc == 0) {
  1275. return;
  1276. }
  1277. Node **ptr = E->value.nodes.ptrw();
  1278. for (int i = 0; i < nc; i++) {
  1279. p_list->push_back(ptr[i]);
  1280. }
  1281. }
  1282. void SceneTree::_flush_delete_queue() {
  1283. _THREAD_SAFE_METHOD_
  1284. while (delete_queue.size()) {
  1285. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  1286. if (obj) {
  1287. memdelete(obj);
  1288. }
  1289. delete_queue.pop_front();
  1290. }
  1291. }
  1292. void SceneTree::queue_delete(Object *p_object) {
  1293. _THREAD_SAFE_METHOD_
  1294. ERR_FAIL_NULL(p_object);
  1295. p_object->_is_queued_for_deletion = true;
  1296. delete_queue.push_back(p_object->get_instance_id());
  1297. }
  1298. int SceneTree::get_node_count() const {
  1299. return nodes_in_tree_count;
  1300. }
  1301. void SceneTree::set_edited_scene_root(Node *p_node) {
  1302. #ifdef TOOLS_ENABLED
  1303. edited_scene_root = p_node;
  1304. #endif
  1305. }
  1306. Node *SceneTree::get_edited_scene_root() const {
  1307. #ifdef TOOLS_ENABLED
  1308. return edited_scene_root;
  1309. #else
  1310. return nullptr;
  1311. #endif
  1312. }
  1313. void SceneTree::set_current_scene(Node *p_scene) {
  1314. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Changing scene can only be done from the main thread.");
  1315. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  1316. current_scene = p_scene;
  1317. }
  1318. Node *SceneTree::get_current_scene() const {
  1319. return current_scene;
  1320. }
  1321. void SceneTree::_flush_scene_change() {
  1322. if (prev_scene_id.is_valid()) {
  1323. // Might have already been freed externally.
  1324. Node *prev_scene = ObjectDB::get_instance<Node>(prev_scene_id);
  1325. if (prev_scene) {
  1326. memdelete(prev_scene);
  1327. }
  1328. prev_scene_id = ObjectID();
  1329. }
  1330. DEV_ASSERT(pending_new_scene_id.is_valid());
  1331. Node *pending_new_scene = ObjectDB::get_instance<Node>(pending_new_scene_id);
  1332. if (pending_new_scene) {
  1333. // Ensure correct state before `add_child` (might enqueue subsequent scene change).
  1334. current_scene = pending_new_scene;
  1335. pending_new_scene_id = ObjectID();
  1336. root->add_child(pending_new_scene);
  1337. // Update display for cursor instantly.
  1338. root->update_mouse_cursor_state();
  1339. // Only on successful scene change.
  1340. emit_signal(SNAME("scene_changed"));
  1341. } else {
  1342. current_scene = nullptr;
  1343. pending_new_scene_id = ObjectID();
  1344. ERR_PRINT("Scene instance has been freed before becoming the current scene. No current scene is set.");
  1345. }
  1346. }
  1347. Error SceneTree::change_scene_to_file(const String &p_path) {
  1348. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), ERR_INVALID_PARAMETER, "Changing scene can only be done from the main thread.");
  1349. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  1350. if (new_scene.is_null()) {
  1351. return ERR_CANT_OPEN;
  1352. }
  1353. return change_scene_to_packed(new_scene);
  1354. }
  1355. Error SceneTree::change_scene_to_packed(const Ref<PackedScene> &p_scene) {
  1356. ERR_FAIL_COND_V_MSG(p_scene.is_null(), ERR_INVALID_PARAMETER, "Can't change to a null scene. Use unload_current_scene() if you wish to unload it.");
  1357. Node *new_scene = p_scene->instantiate();
  1358. ERR_FAIL_NULL_V(new_scene, ERR_CANT_CREATE);
  1359. // If called again while a change is pending.
  1360. if (pending_new_scene_id.is_valid()) {
  1361. Node *pending_new_scene = ObjectDB::get_instance<Node>(pending_new_scene_id);
  1362. if (pending_new_scene) {
  1363. queue_delete(pending_new_scene);
  1364. }
  1365. pending_new_scene_id = ObjectID();
  1366. }
  1367. if (current_scene) {
  1368. prev_scene_id = current_scene->get_instance_id();
  1369. // Let as many side effects as possible happen or be queued now,
  1370. // so they are run before the scene is actually deleted.
  1371. root->remove_child(current_scene);
  1372. }
  1373. DEV_ASSERT(!current_scene);
  1374. pending_new_scene_id = new_scene->get_instance_id();
  1375. return OK;
  1376. }
  1377. Error SceneTree::reload_current_scene() {
  1378. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), ERR_INVALID_PARAMETER, "Reloading scene can only be done from the main thread.");
  1379. ERR_FAIL_NULL_V(current_scene, ERR_UNCONFIGURED);
  1380. String fname = current_scene->get_scene_file_path();
  1381. return change_scene_to_file(fname);
  1382. }
  1383. void SceneTree::unload_current_scene() {
  1384. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Unloading the current scene can only be done from the main thread.");
  1385. if (current_scene) {
  1386. memdelete(current_scene);
  1387. current_scene = nullptr;
  1388. }
  1389. }
  1390. void SceneTree::add_current_scene(Node *p_current) {
  1391. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Adding a current scene can only be done from the main thread.");
  1392. current_scene = p_current;
  1393. root->add_child(p_current);
  1394. }
  1395. Ref<SceneTreeTimer> SceneTree::create_timer(double p_delay_sec, bool p_process_always, bool p_process_in_physics, bool p_ignore_time_scale) {
  1396. _THREAD_SAFE_METHOD_
  1397. Ref<SceneTreeTimer> stt;
  1398. stt.instantiate();
  1399. stt->set_process_always(p_process_always);
  1400. stt->set_time_left(p_delay_sec);
  1401. stt->set_process_in_physics(p_process_in_physics);
  1402. stt->set_ignore_time_scale(p_ignore_time_scale);
  1403. timers.push_back(stt);
  1404. return stt;
  1405. }
  1406. Ref<Tween> SceneTree::create_tween() {
  1407. _THREAD_SAFE_METHOD_
  1408. Ref<Tween> tween;
  1409. tween.instantiate(this);
  1410. tweens.push_back(tween);
  1411. return tween;
  1412. }
  1413. void SceneTree::remove_tween(const Ref<Tween> &p_tween) {
  1414. _THREAD_SAFE_METHOD_
  1415. for (List<Ref<Tween>>::Element *E = tweens.back(); E; E = E->prev()) {
  1416. if (E->get() == p_tween) {
  1417. E->erase();
  1418. break;
  1419. }
  1420. }
  1421. }
  1422. TypedArray<Tween> SceneTree::get_processed_tweens() {
  1423. _THREAD_SAFE_METHOD_
  1424. TypedArray<Tween> ret;
  1425. ret.resize(tweens.size());
  1426. int i = 0;
  1427. for (const Ref<Tween> &tween : tweens) {
  1428. ret[i] = tween;
  1429. i++;
  1430. }
  1431. return ret;
  1432. }
  1433. Ref<MultiplayerAPI> SceneTree::get_multiplayer(const NodePath &p_for_path) const {
  1434. ERR_FAIL_COND_V_MSG(!Thread::is_main_thread(), Ref<MultiplayerAPI>(), "Multiplayer can only be manipulated from the main thread.");
  1435. if (p_for_path.is_empty()) {
  1436. return multiplayer;
  1437. }
  1438. const Vector<StringName> tnames = p_for_path.get_names();
  1439. const StringName *nptr = tnames.ptr();
  1440. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  1441. const Vector<StringName> snames = E.key.get_names();
  1442. if (tnames.size() < snames.size()) {
  1443. continue;
  1444. }
  1445. const StringName *sptr = snames.ptr();
  1446. bool valid = true;
  1447. for (int i = 0; i < snames.size(); i++) {
  1448. if (sptr[i] != nptr[i]) {
  1449. valid = false;
  1450. break;
  1451. }
  1452. }
  1453. if (valid) {
  1454. return E.value;
  1455. }
  1456. }
  1457. return multiplayer;
  1458. }
  1459. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path) {
  1460. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Multiplayer can only be manipulated from the main thread.");
  1461. if (p_root_path.is_empty()) {
  1462. ERR_FAIL_COND(p_multiplayer.is_null());
  1463. if (multiplayer.is_valid()) {
  1464. multiplayer->object_configuration_remove(nullptr, NodePath("/" + root->get_name()));
  1465. }
  1466. multiplayer = p_multiplayer;
  1467. multiplayer->object_configuration_add(nullptr, NodePath("/" + root->get_name()));
  1468. } else {
  1469. if (custom_multiplayers.has(p_root_path)) {
  1470. custom_multiplayers[p_root_path]->object_configuration_remove(nullptr, p_root_path);
  1471. } else if (p_multiplayer.is_valid()) {
  1472. const Vector<StringName> tnames = p_root_path.get_names();
  1473. const StringName *nptr = tnames.ptr();
  1474. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  1475. const Vector<StringName> snames = E.key.get_names();
  1476. if (tnames.size() < snames.size()) {
  1477. continue;
  1478. }
  1479. const StringName *sptr = snames.ptr();
  1480. bool valid = true;
  1481. for (int i = 0; i < snames.size(); i++) {
  1482. if (sptr[i] != nptr[i]) {
  1483. valid = false;
  1484. break;
  1485. }
  1486. }
  1487. ERR_FAIL_COND_MSG(valid, "Multiplayer is already configured for a parent of this path: '" + p_root_path + "' in '" + E.key + "'.");
  1488. }
  1489. }
  1490. if (p_multiplayer.is_valid()) {
  1491. custom_multiplayers[p_root_path] = p_multiplayer;
  1492. p_multiplayer->object_configuration_add(nullptr, p_root_path);
  1493. } else {
  1494. custom_multiplayers.erase(p_root_path);
  1495. }
  1496. }
  1497. }
  1498. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1499. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Multiplayer can only be manipulated from the main thread.");
  1500. multiplayer_poll = p_enabled;
  1501. }
  1502. bool SceneTree::is_multiplayer_poll_enabled() const {
  1503. return multiplayer_poll;
  1504. }
  1505. void SceneTree::_bind_methods() {
  1506. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1507. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1508. ClassDB::bind_method(D_METHOD("is_accessibility_enabled"), &SceneTree::is_accessibility_enabled);
  1509. ClassDB::bind_method(D_METHOD("is_accessibility_supported"), &SceneTree::is_accessibility_supported);
  1510. ClassDB::bind_method(D_METHOD("is_auto_accept_quit"), &SceneTree::is_auto_accept_quit);
  1511. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1512. ClassDB::bind_method(D_METHOD("is_quit_on_go_back"), &SceneTree::is_quit_on_go_back);
  1513. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1514. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1515. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1516. ClassDB::bind_method(D_METHOD("set_debug_paths_hint", "enable"), &SceneTree::set_debug_paths_hint);
  1517. ClassDB::bind_method(D_METHOD("is_debugging_paths_hint"), &SceneTree::is_debugging_paths_hint);
  1518. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1519. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1520. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1521. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1522. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1523. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1524. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "process_always", "process_in_physics", "ignore_time_scale"), &SceneTree::create_timer, DEFVAL(true), DEFVAL(false), DEFVAL(false));
  1525. ClassDB::bind_method(D_METHOD("create_tween"), &SceneTree::create_tween);
  1526. ClassDB::bind_method(D_METHOD("get_processed_tweens"), &SceneTree::get_processed_tweens);
  1527. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1528. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1529. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(EXIT_SUCCESS));
  1530. ClassDB::bind_method(D_METHOD("set_physics_interpolation_enabled", "enabled"), &SceneTree::set_physics_interpolation_enabled);
  1531. ClassDB::bind_method(D_METHOD("is_physics_interpolation_enabled"), &SceneTree::is_physics_interpolation_enabled);
  1532. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1533. MethodInfo mi;
  1534. mi.name = "call_group_flags";
  1535. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1536. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1537. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1538. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1539. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1540. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1541. MethodInfo mi2;
  1542. mi2.name = "call_group";
  1543. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1544. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1545. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1546. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1547. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1548. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1549. ClassDB::bind_method(D_METHOD("get_first_node_in_group", "group"), &SceneTree::get_first_node_in_group);
  1550. ClassDB::bind_method(D_METHOD("get_node_count_in_group", "group"), &SceneTree::get_node_count_in_group);
  1551. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1552. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1553. ClassDB::bind_method(D_METHOD("change_scene_to_file", "path"), &SceneTree::change_scene_to_file);
  1554. ClassDB::bind_method(D_METHOD("change_scene_to_packed", "packed_scene"), &SceneTree::change_scene_to_packed);
  1555. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1556. ClassDB::bind_method(D_METHOD("unload_current_scene"), &SceneTree::unload_current_scene);
  1557. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer", "root_path"), &SceneTree::set_multiplayer, DEFVAL(NodePath()));
  1558. ClassDB::bind_method(D_METHOD("get_multiplayer", "for_path"), &SceneTree::get_multiplayer, DEFVAL(NodePath()));
  1559. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1560. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1561. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit");
  1562. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back");
  1563. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1564. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_paths_hint"), "set_debug_paths_hint", "is_debugging_paths_hint");
  1565. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1566. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1567. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_edited_scene_root", "get_edited_scene_root");
  1568. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_current_scene", "get_current_scene");
  1569. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "", "get_root");
  1570. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1571. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_interpolation"), "set_physics_interpolation_enabled", "is_physics_interpolation_enabled");
  1572. ADD_SIGNAL(MethodInfo("tree_changed"));
  1573. ADD_SIGNAL(MethodInfo("scene_changed"));
  1574. ADD_SIGNAL(MethodInfo("tree_process_mode_changed")); //editor only signal, but due to API hash it can't be removed in run-time
  1575. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1576. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1577. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1578. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1579. ADD_SIGNAL(MethodInfo("process_frame"));
  1580. ADD_SIGNAL(MethodInfo("physics_frame"));
  1581. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1582. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1583. BIND_ENUM_CONSTANT(GROUP_CALL_DEFERRED);
  1584. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1585. }
  1586. SceneTree *SceneTree::singleton = nullptr;
  1587. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1588. int SceneTree::idle_callback_count = 0;
  1589. void SceneTree::_call_idle_callbacks() {
  1590. for (int i = 0; i < idle_callback_count; i++) {
  1591. idle_callbacks[i]();
  1592. }
  1593. }
  1594. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1595. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1596. idle_callbacks[idle_callback_count++] = p_callback;
  1597. }
  1598. #ifdef TOOLS_ENABLED
  1599. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1600. const String pf = p_function;
  1601. bool add_options = false;
  1602. if (p_idx == 0) {
  1603. add_options = pf == "get_nodes_in_group" || pf == "has_group" || pf == "get_first_node_in_group" || pf == "set_group" || pf == "notify_group" || pf == "call_group" || pf == "add_to_group";
  1604. } else if (p_idx == 1) {
  1605. add_options = pf == "set_group_flags" || pf == "call_group_flags" || pf == "notify_group_flags";
  1606. }
  1607. if (add_options) {
  1608. HashMap<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
  1609. for (const KeyValue<StringName, String> &E : global_groups) {
  1610. r_options->push_back(E.key.operator String().quote());
  1611. }
  1612. }
  1613. MainLoop::get_argument_options(p_function, p_idx, r_options);
  1614. }
  1615. #endif
  1616. void SceneTree::set_disable_node_threading(bool p_disable) {
  1617. node_threading_disabled = p_disable;
  1618. }
  1619. SceneTree::SceneTree() {
  1620. if (singleton == nullptr) {
  1621. singleton = this;
  1622. }
  1623. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42));
  1624. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1625. debug_paths_color = GLOBAL_DEF("debug/shapes/paths/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1626. debug_paths_width = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "debug/shapes/paths/geometry_width", PROPERTY_HINT_RANGE, "0.01,10,0.001,or_greater"), 2.0);
  1627. collision_debug_contacts = GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1"), 10000);
  1628. accessibility_upd_per_sec = GLOBAL_GET(SNAME("accessibility/general/updates_per_second"));
  1629. GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  1630. process_group_call_queue_allocator = memnew(CallQueue::Allocator(64));
  1631. Math::randomize();
  1632. // Create with mainloop.
  1633. root = memnew(Window);
  1634. root->set_min_size(Size2i(64, 64)); // Define a very small minimum window size to prevent bugs such as GH-37242.
  1635. root->set_process_mode(Node::PROCESS_MODE_PAUSABLE);
  1636. root->set_auto_translate_mode(GLOBAL_GET("internationalization/rendering/root_node_auto_translate") ? Node::AUTO_TRANSLATE_MODE_ALWAYS : Node::AUTO_TRANSLATE_MODE_DISABLED);
  1637. root->set_name("root");
  1638. root->set_title(GLOBAL_GET("application/config/name"));
  1639. if (Engine::get_singleton()->is_editor_hint()) {
  1640. root->set_wrap_controls(true);
  1641. }
  1642. #ifndef _3D_DISABLED
  1643. if (root->get_world_3d().is_null()) {
  1644. root->set_world_3d(Ref<World3D>(memnew(World3D)));
  1645. }
  1646. root->set_as_audio_listener_3d(true);
  1647. #endif // _3D_DISABLED
  1648. set_physics_interpolation_enabled(GLOBAL_DEF("physics/common/physics_interpolation", false));
  1649. // Always disable jitter fix if physics interpolation is enabled -
  1650. // Jitter fix will interfere with interpolation, and is not necessary
  1651. // when interpolation is active.
  1652. if (is_physics_interpolation_enabled()) {
  1653. Engine::get_singleton()->set_physics_jitter_fix(0);
  1654. }
  1655. // Initialize network state.
  1656. set_multiplayer(MultiplayerAPI::create_default_interface());
  1657. root->set_as_audio_listener_2d(true);
  1658. current_scene = nullptr;
  1659. const int msaa_mode_2d = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_2d");
  1660. root->set_msaa_2d(Viewport::MSAA(msaa_mode_2d));
  1661. const int msaa_mode_3d = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_3d");
  1662. root->set_msaa_3d(Viewport::MSAA(msaa_mode_3d));
  1663. const bool transparent_background = GLOBAL_DEF("rendering/viewport/transparent_background", false);
  1664. root->set_transparent_background(transparent_background);
  1665. const bool use_hdr_2d = GLOBAL_GET("rendering/viewport/hdr_2d");
  1666. root->set_use_hdr_2d(use_hdr_2d);
  1667. const int ssaa_mode = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast)"), 0);
  1668. root->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  1669. const bool use_taa = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/use_taa", false);
  1670. root->set_use_taa(use_taa);
  1671. const bool use_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
  1672. root->set_use_debanding(use_debanding);
  1673. const bool use_occlusion_culling = GLOBAL_DEF("rendering/occlusion_culling/use_occlusion_culling", false);
  1674. root->set_use_occlusion_culling(use_occlusion_culling);
  1675. float mesh_lod_threshold = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "rendering/mesh_lod/lod_change/threshold_pixels", PROPERTY_HINT_RANGE, "0,1024,0.1"), 1.0);
  1676. root->set_mesh_lod_threshold(mesh_lod_threshold);
  1677. bool snap_2d_transforms = GLOBAL_DEF_BASIC("rendering/2d/snap/snap_2d_transforms_to_pixel", false);
  1678. root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
  1679. bool snap_2d_vertices = GLOBAL_DEF("rendering/2d/snap/snap_2d_vertices_to_pixel", false);
  1680. root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
  1681. // We setup VRS for the main viewport here, in the editor this will have little effect.
  1682. const int vrs_mode = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/vrs/mode", PROPERTY_HINT_ENUM, String::utf8("Disabled,Texture,XR")), 0);
  1683. root->set_vrs_mode(Viewport::VRSMode(vrs_mode));
  1684. const String vrs_texture_path = String(GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/vrs/texture", PROPERTY_HINT_FILE, "*.bmp,*.png,*.tga,*.webp"), String())).strip_edges();
  1685. if (vrs_mode == 1 && !vrs_texture_path.is_empty()) {
  1686. Ref<Image> vrs_image;
  1687. vrs_image.instantiate();
  1688. Error load_err = ImageLoader::load_image(vrs_texture_path, vrs_image);
  1689. if (load_err) {
  1690. ERR_PRINT("Non-existing or invalid VRS texture at '" + vrs_texture_path + "'.");
  1691. } else {
  1692. Ref<ImageTexture> vrs_texture;
  1693. vrs_texture.instantiate();
  1694. vrs_texture->create_from_image(vrs_image);
  1695. root->set_vrs_texture(vrs_texture);
  1696. }
  1697. }
  1698. int shadowmap_size = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_size", PROPERTY_HINT_RANGE, "256,16384"), 4096);
  1699. GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_size.mobile", 2048);
  1700. bool shadowmap_16_bits = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_16_bits");
  1701. int atlas_q0 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1702. int atlas_q1 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1703. int atlas_q2 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 3);
  1704. int atlas_q3 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 4);
  1705. root->set_positional_shadow_atlas_size(shadowmap_size);
  1706. root->set_positional_shadow_atlas_16_bits(shadowmap_16_bits);
  1707. root->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0));
  1708. root->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1));
  1709. root->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2));
  1710. root->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3));
  1711. Viewport::SDFOversize sdf_oversize = Viewport::SDFOversize(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"), 1)));
  1712. root->set_sdf_oversize(sdf_oversize);
  1713. Viewport::SDFScale sdf_scale = Viewport::SDFScale(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/scale", PROPERTY_HINT_ENUM, "100%,50%,25%"), 1)));
  1714. root->set_sdf_scale(sdf_scale);
  1715. #ifndef _3D_DISABLED
  1716. { // Load default fallback environment.
  1717. // Get possible extensions.
  1718. List<String> exts;
  1719. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1720. String ext_hint;
  1721. for (const String &E : exts) {
  1722. if (!ext_hint.is_empty()) {
  1723. ext_hint += ",";
  1724. }
  1725. ext_hint += "*." + E;
  1726. }
  1727. // Get path.
  1728. String env_path = GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/environment/defaults/default_environment", PROPERTY_HINT_FILE, ext_hint), "");
  1729. // Setup property.
  1730. env_path = env_path.strip_edges();
  1731. if (!env_path.is_empty()) {
  1732. Ref<Environment> env = ResourceLoader::load(env_path);
  1733. if (env.is_valid()) {
  1734. root->get_world_3d()->set_fallback_environment(env);
  1735. } else {
  1736. if (Engine::get_singleton()->is_editor_hint()) {
  1737. // File was erased, clear the field.
  1738. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  1739. } else {
  1740. // File was erased, notify user.
  1741. ERR_PRINT("Default Environment as specified in the project setting \"rendering/environment/defaults/default_environment\" could not be loaded.");
  1742. }
  1743. }
  1744. }
  1745. }
  1746. #endif // _3D_DISABLED
  1747. #if !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
  1748. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1749. #endif // !defined(PHYSICS_2D_DISABLED) || !defined(PHYSICS_3D_DISABLED)
  1750. root->connect("close_requested", callable_mp(this, &SceneTree::_main_window_close));
  1751. root->connect("go_back_requested", callable_mp(this, &SceneTree::_main_window_go_back));
  1752. root->connect(SceneStringName(focus_entered), callable_mp(this, &SceneTree::_main_window_focus_in));
  1753. #ifdef TOOLS_ENABLED
  1754. edited_scene_root = nullptr;
  1755. #endif
  1756. process_groups.push_back(&default_process_group);
  1757. }
  1758. SceneTree::~SceneTree() {
  1759. if (prev_scene_id.is_valid()) {
  1760. Node *prev_scene = ObjectDB::get_instance<Node>(prev_scene_id);
  1761. if (prev_scene) {
  1762. memdelete(prev_scene);
  1763. }
  1764. prev_scene_id = ObjectID();
  1765. }
  1766. if (pending_new_scene_id.is_valid()) {
  1767. Node *pending_new_scene = ObjectDB::get_instance<Node>(pending_new_scene_id);
  1768. if (pending_new_scene) {
  1769. memdelete(pending_new_scene);
  1770. }
  1771. pending_new_scene_id = ObjectID();
  1772. }
  1773. if (root) {
  1774. root->_set_tree(nullptr);
  1775. root->_propagate_after_exit_tree();
  1776. memdelete(root);
  1777. }
  1778. // Process groups are not deleted immediately, they may remain around. Delete them now.
  1779. for (uint32_t i = 0; i < process_groups.size(); i++) {
  1780. if (process_groups[i] != &default_process_group) {
  1781. memdelete(process_groups[i]);
  1782. }
  1783. }
  1784. memdelete(process_group_call_queue_allocator);
  1785. if (singleton == this) {
  1786. singleton = nullptr;
  1787. }
  1788. }