scene_tree.cpp 72 KB

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