scene_tree.cpp 71 KB

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