scene_tree.cpp 73 KB

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