scene_tree.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. /*************************************************************************/
  2. /* scene_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/io/marshalls.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/message_queue.h"
  34. #include "core/os/dir_access.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/os/os.h"
  37. #include "core/print_string.h"
  38. #include "core/project_settings.h"
  39. #include "core/script_debugger_remote.h"
  40. #include "main/input_default.h"
  41. #include "node.h"
  42. #include "scene/debugger/scene_debugger.h"
  43. #include "scene/resources/dynamic_font.h"
  44. #include "scene/resources/material.h"
  45. #include "scene/resources/mesh.h"
  46. #include "scene/resources/packed_scene.h"
  47. #include "scene/scene_string_names.h"
  48. #include "servers/navigation_server.h"
  49. #include "servers/physics_2d_server.h"
  50. #include "servers/physics_server.h"
  51. #include "viewport.h"
  52. #include <stdio.h>
  53. void SceneTreeTimer::_bind_methods() {
  54. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  55. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  56. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left"), "set_time_left", "get_time_left");
  57. ADD_SIGNAL(MethodInfo("timeout"));
  58. }
  59. void SceneTreeTimer::set_time_left(float p_time) {
  60. time_left = p_time;
  61. }
  62. float SceneTreeTimer::get_time_left() const {
  63. return time_left;
  64. }
  65. void SceneTreeTimer::set_pause_mode_process(bool p_pause_mode_process) {
  66. process_pause = p_pause_mode_process;
  67. }
  68. bool SceneTreeTimer::is_pause_mode_process() {
  69. return process_pause;
  70. }
  71. void SceneTreeTimer::release_connections() {
  72. List<Connection> connections;
  73. get_all_signal_connections(&connections);
  74. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  75. Connection const &connection = E->get();
  76. disconnect(connection.signal.get_name(), connection.callable);
  77. }
  78. }
  79. SceneTreeTimer::SceneTreeTimer() {
  80. time_left = 0;
  81. process_pause = true;
  82. }
  83. void SceneTree::tree_changed() {
  84. tree_version++;
  85. emit_signal(tree_changed_name);
  86. }
  87. void SceneTree::node_added(Node *p_node) {
  88. emit_signal(node_added_name, p_node);
  89. }
  90. void SceneTree::node_removed(Node *p_node) {
  91. if (current_scene == p_node) {
  92. current_scene = NULL;
  93. }
  94. emit_signal(node_removed_name, p_node);
  95. if (call_lock > 0)
  96. call_skip.insert(p_node);
  97. }
  98. void SceneTree::node_renamed(Node *p_node) {
  99. emit_signal(node_renamed_name, p_node);
  100. }
  101. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  102. Map<StringName, Group>::Element *E = group_map.find(p_group);
  103. if (!E) {
  104. E = group_map.insert(p_group, Group());
  105. }
  106. ERR_FAIL_COND_V_MSG(E->get().nodes.find(p_node) != -1, &E->get(), "Already in group: " + p_group + ".");
  107. E->get().nodes.push_back(p_node);
  108. //E->get().last_tree_version=0;
  109. E->get().changed = true;
  110. return &E->get();
  111. }
  112. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  113. Map<StringName, Group>::Element *E = group_map.find(p_group);
  114. ERR_FAIL_COND(!E);
  115. E->get().nodes.erase(p_node);
  116. if (E->get().nodes.empty())
  117. group_map.erase(E);
  118. }
  119. void SceneTree::make_group_changed(const StringName &p_group) {
  120. Map<StringName, Group>::Element *E = group_map.find(p_group);
  121. if (E)
  122. E->get().changed = true;
  123. }
  124. void SceneTree::flush_transform_notifications() {
  125. SelfList<Node> *n = xform_change_list.first();
  126. while (n) {
  127. Node *node = n->self();
  128. SelfList<Node> *nx = n->next();
  129. xform_change_list.remove(n);
  130. n = nx;
  131. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  132. }
  133. }
  134. void SceneTree::_flush_ugc() {
  135. ugc_locked = true;
  136. while (unique_group_calls.size()) {
  137. Map<UGCall, Vector<Variant> >::Element *E = unique_group_calls.front();
  138. Variant v[VARIANT_ARG_MAX];
  139. for (int i = 0; i < E->get().size(); i++)
  140. v[i] = E->get()[i];
  141. call_group_flags(GROUP_CALL_REALTIME, E->key().group, E->key().call, v[0], v[1], v[2], v[3], v[4]);
  142. unique_group_calls.erase(E);
  143. }
  144. ugc_locked = false;
  145. }
  146. void SceneTree::_update_group_order(Group &g, bool p_use_priority) {
  147. if (!g.changed)
  148. return;
  149. if (g.nodes.empty())
  150. return;
  151. Node **nodes = g.nodes.ptrw();
  152. int node_count = g.nodes.size();
  153. if (p_use_priority) {
  154. SortArray<Node *, Node::ComparatorWithPriority> node_sort;
  155. node_sort.sort(nodes, node_count);
  156. } else {
  157. SortArray<Node *, Node::Comparator> node_sort;
  158. node_sort.sort(nodes, node_count);
  159. }
  160. g.changed = false;
  161. }
  162. void SceneTree::call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  163. Map<StringName, Group>::Element *E = group_map.find(p_group);
  164. if (!E)
  165. return;
  166. Group &g = E->get();
  167. if (g.nodes.empty())
  168. return;
  169. if (p_call_flags & GROUP_CALL_UNIQUE && !(p_call_flags & GROUP_CALL_REALTIME)) {
  170. ERR_FAIL_COND(ugc_locked);
  171. UGCall ug;
  172. ug.call = p_function;
  173. ug.group = p_group;
  174. if (unique_group_calls.has(ug))
  175. return;
  176. VARIANT_ARGPTRS;
  177. Vector<Variant> args;
  178. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  179. if (argptr[i]->get_type() == Variant::NIL)
  180. break;
  181. args.push_back(*argptr[i]);
  182. }
  183. unique_group_calls[ug] = args;
  184. return;
  185. }
  186. _update_group_order(g);
  187. Vector<Node *> nodes_copy = g.nodes;
  188. Node **nodes = nodes_copy.ptrw();
  189. int node_count = nodes_copy.size();
  190. call_lock++;
  191. if (p_call_flags & GROUP_CALL_REVERSE) {
  192. for (int i = node_count - 1; i >= 0; i--) {
  193. if (call_lock && call_skip.has(nodes[i]))
  194. continue;
  195. if (p_call_flags & GROUP_CALL_REALTIME) {
  196. if (p_call_flags & GROUP_CALL_MULTILEVEL)
  197. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  198. else
  199. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  200. } else
  201. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  202. }
  203. } else {
  204. for (int i = 0; i < node_count; i++) {
  205. if (call_lock && call_skip.has(nodes[i]))
  206. continue;
  207. if (p_call_flags & GROUP_CALL_REALTIME) {
  208. if (p_call_flags & GROUP_CALL_MULTILEVEL)
  209. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  210. else
  211. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  212. } else
  213. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  214. }
  215. }
  216. call_lock--;
  217. if (call_lock == 0)
  218. call_skip.clear();
  219. }
  220. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  221. Map<StringName, Group>::Element *E = group_map.find(p_group);
  222. if (!E)
  223. return;
  224. Group &g = E->get();
  225. if (g.nodes.empty())
  226. return;
  227. _update_group_order(g);
  228. Vector<Node *> nodes_copy = g.nodes;
  229. Node **nodes = nodes_copy.ptrw();
  230. int node_count = nodes_copy.size();
  231. call_lock++;
  232. if (p_call_flags & GROUP_CALL_REVERSE) {
  233. for (int i = node_count - 1; i >= 0; i--) {
  234. if (call_lock && call_skip.has(nodes[i]))
  235. continue;
  236. if (p_call_flags & GROUP_CALL_REALTIME)
  237. nodes[i]->notification(p_notification);
  238. else
  239. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  240. }
  241. } else {
  242. for (int i = 0; i < node_count; i++) {
  243. if (call_lock && call_skip.has(nodes[i]))
  244. continue;
  245. if (p_call_flags & GROUP_CALL_REALTIME)
  246. nodes[i]->notification(p_notification);
  247. else
  248. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  249. }
  250. }
  251. call_lock--;
  252. if (call_lock == 0)
  253. call_skip.clear();
  254. }
  255. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  256. Map<StringName, Group>::Element *E = group_map.find(p_group);
  257. if (!E)
  258. return;
  259. Group &g = E->get();
  260. if (g.nodes.empty())
  261. return;
  262. _update_group_order(g);
  263. Vector<Node *> nodes_copy = g.nodes;
  264. Node **nodes = nodes_copy.ptrw();
  265. int node_count = nodes_copy.size();
  266. call_lock++;
  267. if (p_call_flags & GROUP_CALL_REVERSE) {
  268. for (int i = node_count - 1; i >= 0; i--) {
  269. if (call_lock && call_skip.has(nodes[i]))
  270. continue;
  271. if (p_call_flags & GROUP_CALL_REALTIME)
  272. nodes[i]->set(p_name, p_value);
  273. else
  274. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  275. }
  276. } else {
  277. for (int i = 0; i < node_count; i++) {
  278. if (call_lock && call_skip.has(nodes[i]))
  279. continue;
  280. if (p_call_flags & GROUP_CALL_REALTIME)
  281. nodes[i]->set(p_name, p_value);
  282. else
  283. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  284. }
  285. }
  286. call_lock--;
  287. if (call_lock == 0)
  288. call_skip.clear();
  289. }
  290. void SceneTree::call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  291. call_group_flags(0, p_group, p_function, VARIANT_ARG_PASS);
  292. }
  293. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  294. notify_group_flags(0, p_group, p_notification);
  295. }
  296. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  297. set_group_flags(0, p_group, p_name, p_value);
  298. }
  299. void SceneTree::set_input_as_handled() {
  300. input_handled = true;
  301. }
  302. void SceneTree::input_text(const String &p_text) {
  303. root_lock++;
  304. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input_text", p_text); //special one for GUI, as controls use their own process check
  305. root_lock--;
  306. }
  307. bool SceneTree::is_input_handled() {
  308. return input_handled;
  309. }
  310. void SceneTree::input_event(const Ref<InputEvent> &p_event) {
  311. if (Engine::get_singleton()->is_editor_hint() && (Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventJoypadMotion>(*p_event)))
  312. return; //avoid joy input on editor
  313. current_event++;
  314. root_lock++;
  315. input_handled = false;
  316. // Don't make const ref unless you can find and fix what caused GH-34691.
  317. Ref<InputEvent> ev = p_event;
  318. MainLoop::input_event(ev);
  319. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input", ev); //special one for GUI, as controls use their own process check
  320. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_remote()) {
  321. //quit from game window using F8
  322. Ref<InputEventKey> k = ev;
  323. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == KEY_F8) {
  324. ScriptDebugger::get_singleton()->request_quit();
  325. }
  326. }
  327. _flush_ugc();
  328. root_lock--;
  329. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  330. root_lock++;
  331. if (!input_handled) {
  332. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_unhandled_input", ev); //special one for GUI, as controls use their own process check
  333. _flush_ugc();
  334. // input_handled = true; - no reason to set this as handled
  335. root_lock--;
  336. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  337. } else {
  338. // input_handled = true; - no reason to set this as handled
  339. root_lock--;
  340. }
  341. _call_idle_callbacks();
  342. }
  343. void SceneTree::init() {
  344. initialized = true;
  345. root->_set_tree(this);
  346. MainLoop::init();
  347. }
  348. bool SceneTree::iteration(float p_time) {
  349. root_lock++;
  350. current_frame++;
  351. flush_transform_notifications();
  352. MainLoop::iteration(p_time);
  353. physics_process_time = p_time;
  354. emit_signal("physics_frame");
  355. _notify_group_pause("physics_process_internal", Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  356. _notify_group_pause("physics_process", Node::NOTIFICATION_PHYSICS_PROCESS);
  357. _flush_ugc();
  358. MessageQueue::get_singleton()->flush(); //small little hack
  359. flush_transform_notifications();
  360. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  361. root_lock--;
  362. _flush_delete_queue();
  363. _call_idle_callbacks();
  364. return _quit;
  365. }
  366. void SceneTree::_update_font_oversampling(float p_ratio) {
  367. if (use_font_oversampling) {
  368. DynamicFontAtSize::font_oversampling = p_ratio;
  369. DynamicFont::update_oversampling();
  370. }
  371. }
  372. bool SceneTree::idle(float p_time) {
  373. //print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
  374. //print_line("node count: "+itos(get_node_count()));
  375. //print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
  376. root_lock++;
  377. MainLoop::idle(p_time);
  378. idle_process_time = p_time;
  379. if (multiplayer_poll) {
  380. multiplayer->poll();
  381. }
  382. emit_signal("idle_frame");
  383. MessageQueue::get_singleton()->flush(); //small little hack
  384. flush_transform_notifications();
  385. _notify_group_pause("idle_process_internal", Node::NOTIFICATION_INTERNAL_PROCESS);
  386. _notify_group_pause("idle_process", Node::NOTIFICATION_PROCESS);
  387. Size2 win_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  388. if (win_size != last_screen_size) {
  389. last_screen_size = win_size;
  390. _update_root_rect();
  391. emit_signal("screen_resized");
  392. }
  393. _flush_ugc();
  394. MessageQueue::get_singleton()->flush(); //small little hack
  395. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  396. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  397. root_lock--;
  398. _flush_delete_queue();
  399. //go through timers
  400. List<Ref<SceneTreeTimer> >::Element *L = timers.back(); //last element
  401. for (List<Ref<SceneTreeTimer> >::Element *E = timers.front(); E;) {
  402. List<Ref<SceneTreeTimer> >::Element *N = E->next();
  403. if (pause && !E->get()->is_pause_mode_process()) {
  404. if (E == L) {
  405. break; //break on last, so if new timers were added during list traversal, ignore them.
  406. }
  407. E = N;
  408. continue;
  409. }
  410. float time_left = E->get()->get_time_left();
  411. time_left -= p_time;
  412. E->get()->set_time_left(time_left);
  413. if (time_left < 0) {
  414. E->get()->emit_signal("timeout");
  415. timers.erase(E);
  416. }
  417. if (E == L) {
  418. break; //break on last, so if new timers were added during list traversal, ignore them.
  419. }
  420. E = N;
  421. }
  422. flush_transform_notifications(); //additional transforms after timers update
  423. _call_idle_callbacks();
  424. #ifdef TOOLS_ENABLED
  425. if (Engine::get_singleton()->is_editor_hint()) {
  426. //simple hack to reload fallback environment if it changed from editor
  427. String env_path = ProjectSettings::get_singleton()->get("rendering/environment/default_environment");
  428. env_path = env_path.strip_edges(); //user may have added a space or two
  429. String cpath;
  430. Ref<Environment> fallback = get_root()->get_world()->get_fallback_environment();
  431. if (fallback.is_valid()) {
  432. cpath = fallback->get_path();
  433. }
  434. if (cpath != env_path) {
  435. if (env_path != String()) {
  436. fallback = ResourceLoader::load(env_path);
  437. if (fallback.is_null()) {
  438. //could not load fallback, set as empty
  439. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  440. }
  441. } else {
  442. fallback.unref();
  443. }
  444. get_root()->get_world()->set_fallback_environment(fallback);
  445. }
  446. }
  447. #endif
  448. return _quit;
  449. }
  450. void SceneTree::finish() {
  451. _flush_delete_queue();
  452. _flush_ugc();
  453. initialized = false;
  454. MainLoop::finish();
  455. if (root) {
  456. root->_set_tree(NULL);
  457. root->_propagate_after_exit_tree();
  458. memdelete(root); //delete root
  459. root = NULL;
  460. }
  461. // cleanup timers
  462. for (List<Ref<SceneTreeTimer> >::Element *E = timers.front(); E; E = E->next()) {
  463. E->get()->release_connections();
  464. }
  465. timers.clear();
  466. }
  467. void SceneTree::quit(int p_exit_code) {
  468. if (p_exit_code >= 0) {
  469. // Override the exit code if a positive argument is given (the default is `-1`).
  470. // This is a shorthand for calling `set_exit_code()` on the OS singleton then quitting.
  471. OS::get_singleton()->set_exit_code(p_exit_code);
  472. }
  473. _quit = true;
  474. }
  475. void SceneTree::_notification(int p_notification) {
  476. switch (p_notification) {
  477. case NOTIFICATION_WM_QUIT_REQUEST: {
  478. get_root()->propagate_notification(p_notification);
  479. if (accept_quit) {
  480. _quit = true;
  481. break;
  482. }
  483. } break;
  484. case NOTIFICATION_WM_GO_BACK_REQUEST: {
  485. get_root()->propagate_notification(p_notification);
  486. if (quit_on_go_back) {
  487. _quit = true;
  488. break;
  489. }
  490. } break;
  491. case NOTIFICATION_WM_FOCUS_IN: {
  492. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  493. if (id) {
  494. id->ensure_touch_mouse_raised();
  495. }
  496. get_root()->propagate_notification(p_notification);
  497. } break;
  498. case NOTIFICATION_TRANSLATION_CHANGED: {
  499. if (!Engine::get_singleton()->is_editor_hint()) {
  500. get_root()->propagate_notification(p_notification);
  501. }
  502. } break;
  503. case NOTIFICATION_WM_UNFOCUS_REQUEST: {
  504. notify_group_flags(GROUP_CALL_REALTIME | GROUP_CALL_MULTILEVEL, "input", NOTIFICATION_WM_UNFOCUS_REQUEST);
  505. get_root()->propagate_notification(p_notification);
  506. } break;
  507. case NOTIFICATION_OS_MEMORY_WARNING:
  508. case NOTIFICATION_OS_IME_UPDATE:
  509. case NOTIFICATION_WM_MOUSE_ENTER:
  510. case NOTIFICATION_WM_MOUSE_EXIT:
  511. case NOTIFICATION_WM_FOCUS_OUT:
  512. case NOTIFICATION_WM_ABOUT:
  513. case NOTIFICATION_CRASH:
  514. case NOTIFICATION_APP_RESUMED:
  515. case NOTIFICATION_APP_PAUSED: {
  516. get_root()->propagate_notification(p_notification);
  517. } break;
  518. default:
  519. break;
  520. };
  521. };
  522. void SceneTree::set_auto_accept_quit(bool p_enable) {
  523. accept_quit = p_enable;
  524. }
  525. void SceneTree::set_quit_on_go_back(bool p_enable) {
  526. quit_on_go_back = p_enable;
  527. }
  528. #ifdef TOOLS_ENABLED
  529. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  530. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_a_parent_of(p_node) || edited_scene_root == p_node);
  531. }
  532. #endif
  533. #ifdef DEBUG_ENABLED
  534. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  535. debug_collisions_hint = p_enabled;
  536. }
  537. bool SceneTree::is_debugging_collisions_hint() const {
  538. return debug_collisions_hint;
  539. }
  540. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  541. debug_navigation_hint = p_enabled;
  542. }
  543. bool SceneTree::is_debugging_navigation_hint() const {
  544. return debug_navigation_hint;
  545. }
  546. #endif
  547. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  548. debug_collisions_color = p_color;
  549. }
  550. Color SceneTree::get_debug_collisions_color() const {
  551. return debug_collisions_color;
  552. }
  553. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  554. debug_collision_contact_color = p_color;
  555. }
  556. Color SceneTree::get_debug_collision_contact_color() const {
  557. return debug_collision_contact_color;
  558. }
  559. void SceneTree::set_debug_navigation_color(const Color &p_color) {
  560. debug_navigation_color = p_color;
  561. }
  562. Color SceneTree::get_debug_navigation_color() const {
  563. return debug_navigation_color;
  564. }
  565. void SceneTree::set_debug_navigation_disabled_color(const Color &p_color) {
  566. debug_navigation_disabled_color = p_color;
  567. }
  568. Color SceneTree::get_debug_navigation_disabled_color() const {
  569. return debug_navigation_disabled_color;
  570. }
  571. Ref<Material> SceneTree::get_debug_navigation_material() {
  572. if (navigation_material.is_valid())
  573. return navigation_material;
  574. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  575. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  576. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  577. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  578. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  579. line_material->set_albedo(get_debug_navigation_color());
  580. navigation_material = line_material;
  581. return navigation_material;
  582. }
  583. Ref<Material> SceneTree::get_debug_navigation_disabled_material() {
  584. if (navigation_disabled_material.is_valid())
  585. return navigation_disabled_material;
  586. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  587. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  588. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  589. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  590. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  591. line_material->set_albedo(get_debug_navigation_disabled_color());
  592. navigation_disabled_material = line_material;
  593. return navigation_disabled_material;
  594. }
  595. Ref<Material> SceneTree::get_debug_collision_material() {
  596. if (collision_material.is_valid())
  597. return collision_material;
  598. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  599. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  600. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  601. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  602. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  603. line_material->set_albedo(get_debug_collisions_color());
  604. collision_material = line_material;
  605. return collision_material;
  606. }
  607. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  608. if (debug_contact_mesh.is_valid())
  609. return debug_contact_mesh;
  610. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  611. Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  612. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  613. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  614. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  615. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  616. mat->set_albedo(get_debug_collision_contact_color());
  617. Vector3 diamond[6] = {
  618. Vector3(-1, 0, 0),
  619. Vector3(1, 0, 0),
  620. Vector3(0, -1, 0),
  621. Vector3(0, 1, 0),
  622. Vector3(0, 0, -1),
  623. Vector3(0, 0, 1)
  624. };
  625. /* clang-format off */
  626. int diamond_faces[8 * 3] = {
  627. 0, 2, 4,
  628. 0, 3, 4,
  629. 1, 2, 4,
  630. 1, 3, 4,
  631. 0, 2, 5,
  632. 0, 3, 5,
  633. 1, 2, 5,
  634. 1, 3, 5,
  635. };
  636. /* clang-format on */
  637. Vector<int> indices;
  638. for (int i = 0; i < 8 * 3; i++)
  639. indices.push_back(diamond_faces[i]);
  640. Vector<Vector3> vertices;
  641. for (int i = 0; i < 6; i++)
  642. vertices.push_back(diamond[i] * 0.1);
  643. Array arr;
  644. arr.resize(Mesh::ARRAY_MAX);
  645. arr[Mesh::ARRAY_VERTEX] = vertices;
  646. arr[Mesh::ARRAY_INDEX] = indices;
  647. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  648. debug_contact_mesh->surface_set_material(0, mat);
  649. return debug_contact_mesh;
  650. }
  651. void SceneTree::set_pause(bool p_enabled) {
  652. if (p_enabled == pause)
  653. return;
  654. pause = p_enabled;
  655. NavigationServer::get_singleton()->set_active(!p_enabled);
  656. PhysicsServer::get_singleton()->set_active(!p_enabled);
  657. Physics2DServer::get_singleton()->set_active(!p_enabled);
  658. if (get_root())
  659. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  660. }
  661. bool SceneTree::is_paused() const {
  662. return pause;
  663. }
  664. void SceneTree::_call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input) {
  665. Map<StringName, Group>::Element *E = group_map.find(p_group);
  666. if (!E)
  667. return;
  668. Group &g = E->get();
  669. if (g.nodes.empty())
  670. return;
  671. _update_group_order(g);
  672. //copy, so copy on write happens in case something is removed from process while being called
  673. //performance is not lost because only if something is added/removed the vector is copied.
  674. Vector<Node *> nodes_copy = g.nodes;
  675. int node_count = nodes_copy.size();
  676. Node **nodes = nodes_copy.ptrw();
  677. Variant arg = p_input;
  678. const Variant *v[1] = { &arg };
  679. call_lock++;
  680. for (int i = node_count - 1; i >= 0; i--) {
  681. if (input_handled)
  682. break;
  683. Node *n = nodes[i];
  684. if (call_lock && call_skip.has(n))
  685. continue;
  686. if (!n->can_process())
  687. continue;
  688. n->call_multilevel(p_method, (const Variant **)v, 1);
  689. //ERR_FAIL_COND(node_count != g.nodes.size());
  690. }
  691. call_lock--;
  692. if (call_lock == 0)
  693. call_skip.clear();
  694. }
  695. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  696. Map<StringName, Group>::Element *E = group_map.find(p_group);
  697. if (!E)
  698. return;
  699. Group &g = E->get();
  700. if (g.nodes.empty())
  701. return;
  702. _update_group_order(g, p_notification == Node::NOTIFICATION_PROCESS || p_notification == Node::NOTIFICATION_INTERNAL_PROCESS || p_notification == Node::NOTIFICATION_PHYSICS_PROCESS || p_notification == Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  703. //copy, so copy on write happens in case something is removed from process while being called
  704. //performance is not lost because only if something is added/removed the vector is copied.
  705. Vector<Node *> nodes_copy = g.nodes;
  706. int node_count = nodes_copy.size();
  707. Node **nodes = nodes_copy.ptrw();
  708. call_lock++;
  709. for (int i = 0; i < node_count; i++) {
  710. Node *n = nodes[i];
  711. if (call_lock && call_skip.has(n))
  712. continue;
  713. if (!n->can_process())
  714. continue;
  715. if (!n->can_process_notification(p_notification))
  716. continue;
  717. n->notification(p_notification);
  718. //ERR_FAIL_COND(node_count != g.nodes.size());
  719. }
  720. call_lock--;
  721. if (call_lock == 0)
  722. call_skip.clear();
  723. }
  724. /*
  725. void SceneMainLoop::_update_listener_2d() {
  726. if (listener_2d.is_valid()) {
  727. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  728. }
  729. }
  730. */
  731. Variant SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  732. r_error.error = Callable::CallError::CALL_OK;
  733. ERR_FAIL_COND_V(p_argcount < 3, Variant());
  734. ERR_FAIL_COND_V(!p_args[0]->is_num(), Variant());
  735. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING, Variant());
  736. ERR_FAIL_COND_V(p_args[2]->get_type() != Variant::STRING_NAME && p_args[2]->get_type() != Variant::STRING, Variant());
  737. int flags = *p_args[0];
  738. StringName group = *p_args[1];
  739. StringName method = *p_args[2];
  740. Variant v[VARIANT_ARG_MAX];
  741. for (int i = 0; i < MIN(p_argcount - 3, 5); i++) {
  742. v[i] = *p_args[i + 3];
  743. }
  744. call_group_flags(flags, group, method, v[0], v[1], v[2], v[3], v[4]);
  745. return Variant();
  746. }
  747. Variant SceneTree::_call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  748. r_error.error = Callable::CallError::CALL_OK;
  749. ERR_FAIL_COND_V(p_argcount < 2, Variant());
  750. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING, Variant());
  751. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING, Variant());
  752. StringName group = *p_args[0];
  753. StringName method = *p_args[1];
  754. Variant v[VARIANT_ARG_MAX];
  755. for (int i = 0; i < MIN(p_argcount - 2, 5); i++) {
  756. v[i] = *p_args[i + 2];
  757. }
  758. call_group_flags(0, group, method, v[0], v[1], v[2], v[3], v[4]);
  759. return Variant();
  760. }
  761. int64_t SceneTree::get_frame() const {
  762. return current_frame;
  763. }
  764. int64_t SceneTree::get_event_count() const {
  765. return current_event;
  766. }
  767. Array SceneTree::_get_nodes_in_group(const StringName &p_group) {
  768. Array ret;
  769. Map<StringName, Group>::Element *E = group_map.find(p_group);
  770. if (!E)
  771. return ret;
  772. _update_group_order(E->get()); //update order just in case
  773. int nc = E->get().nodes.size();
  774. if (nc == 0)
  775. return ret;
  776. ret.resize(nc);
  777. Node **ptr = E->get().nodes.ptrw();
  778. for (int i = 0; i < nc; i++) {
  779. ret[i] = ptr[i];
  780. }
  781. return ret;
  782. }
  783. bool SceneTree::has_group(const StringName &p_identifier) const {
  784. return group_map.has(p_identifier);
  785. }
  786. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  787. Map<StringName, Group>::Element *E = group_map.find(p_group);
  788. if (!E)
  789. return;
  790. _update_group_order(E->get()); //update order just in case
  791. int nc = E->get().nodes.size();
  792. if (nc == 0)
  793. return;
  794. Node **ptr = E->get().nodes.ptrw();
  795. for (int i = 0; i < nc; i++) {
  796. p_list->push_back(ptr[i]);
  797. }
  798. }
  799. void SceneTree::_flush_delete_queue() {
  800. _THREAD_SAFE_METHOD_
  801. while (delete_queue.size()) {
  802. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  803. if (obj) {
  804. memdelete(obj);
  805. }
  806. delete_queue.pop_front();
  807. }
  808. }
  809. void SceneTree::queue_delete(Object *p_object) {
  810. _THREAD_SAFE_METHOD_
  811. ERR_FAIL_NULL(p_object);
  812. p_object->_is_queued_for_deletion = true;
  813. delete_queue.push_back(p_object->get_instance_id());
  814. }
  815. int SceneTree::get_node_count() const {
  816. return node_count;
  817. }
  818. void SceneTree::_update_root_rect() {
  819. if (stretch_mode == STRETCH_MODE_DISABLED) {
  820. _update_font_oversampling(1.0);
  821. root->set_size((last_screen_size / stretch_shrink).floor());
  822. root->set_attach_to_screen_rect(Rect2(Point2(), last_screen_size));
  823. root->set_size_override_stretch(false);
  824. root->set_size_override(false, Size2());
  825. root->update_canvas_items();
  826. return; //user will take care
  827. }
  828. //actual screen video mode
  829. Size2 video_mode = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  830. Size2 desired_res = stretch_min;
  831. Size2 viewport_size;
  832. Size2 screen_size;
  833. float viewport_aspect = desired_res.aspect();
  834. float video_mode_aspect = video_mode.aspect();
  835. if (use_font_oversampling && stretch_aspect == STRETCH_ASPECT_IGNORE) {
  836. WARN_PRINT("Font oversampling only works with the resize modes 'Keep Width', 'Keep Height', and 'Expand'.");
  837. }
  838. if (stretch_aspect == STRETCH_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  839. //same aspect or ignore aspect
  840. viewport_size = desired_res;
  841. screen_size = video_mode;
  842. } else if (viewport_aspect < video_mode_aspect) {
  843. // screen ratio is smaller vertically
  844. if (stretch_aspect == STRETCH_ASPECT_KEEP_HEIGHT || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  845. //will stretch horizontally
  846. viewport_size.x = desired_res.y * video_mode_aspect;
  847. viewport_size.y = desired_res.y;
  848. screen_size = video_mode;
  849. } else {
  850. //will need black bars
  851. viewport_size = desired_res;
  852. screen_size.x = video_mode.y * viewport_aspect;
  853. screen_size.y = video_mode.y;
  854. }
  855. } else {
  856. //screen ratio is smaller horizontally
  857. if (stretch_aspect == STRETCH_ASPECT_KEEP_WIDTH || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  858. //will stretch horizontally
  859. viewport_size.x = desired_res.x;
  860. viewport_size.y = desired_res.x / video_mode_aspect;
  861. screen_size = video_mode;
  862. } else {
  863. //will need black bars
  864. viewport_size = desired_res;
  865. screen_size.x = video_mode.x;
  866. screen_size.y = video_mode.x / viewport_aspect;
  867. }
  868. }
  869. screen_size = screen_size.floor();
  870. viewport_size = viewport_size.floor();
  871. Size2 margin;
  872. Size2 offset;
  873. //black bars and margin
  874. if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  875. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  876. VisualServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  877. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  878. } else if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  879. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  880. VisualServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  881. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  882. } else {
  883. VisualServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  884. }
  885. switch (stretch_mode) {
  886. case STRETCH_MODE_DISABLED: {
  887. // Already handled above
  888. _update_font_oversampling(1.0);
  889. } break;
  890. case STRETCH_MODE_2D: {
  891. _update_font_oversampling(screen_size.x / viewport_size.x); //screen / viewport radio drives oversampling
  892. root->set_size((screen_size / stretch_shrink).floor());
  893. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  894. root->set_size_override_stretch(true);
  895. root->set_size_override(true, (viewport_size / stretch_shrink).floor());
  896. root->update_canvas_items(); //force them to update just in case
  897. } break;
  898. case STRETCH_MODE_VIEWPORT: {
  899. _update_font_oversampling(1.0);
  900. root->set_size((viewport_size / stretch_shrink).floor());
  901. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  902. root->set_size_override_stretch(false);
  903. root->set_size_override(false, Size2());
  904. root->update_canvas_items(); //force them to update just in case
  905. if (use_font_oversampling) {
  906. WARN_PRINT("Font oversampling does not work in 'Viewport' stretch mode, only '2D'.");
  907. }
  908. } break;
  909. }
  910. }
  911. void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_shrink) {
  912. stretch_mode = p_mode;
  913. stretch_aspect = p_aspect;
  914. stretch_min = p_minsize;
  915. stretch_shrink = p_shrink;
  916. _update_root_rect();
  917. }
  918. void SceneTree::set_edited_scene_root(Node *p_node) {
  919. #ifdef TOOLS_ENABLED
  920. edited_scene_root = p_node;
  921. #endif
  922. }
  923. Node *SceneTree::get_edited_scene_root() const {
  924. #ifdef TOOLS_ENABLED
  925. return edited_scene_root;
  926. #else
  927. return NULL;
  928. #endif
  929. }
  930. void SceneTree::set_current_scene(Node *p_scene) {
  931. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  932. current_scene = p_scene;
  933. }
  934. Node *SceneTree::get_current_scene() const {
  935. return current_scene;
  936. }
  937. void SceneTree::_change_scene(Node *p_to) {
  938. if (current_scene) {
  939. memdelete(current_scene);
  940. current_scene = NULL;
  941. }
  942. // If we're quitting, abort.
  943. if (unlikely(_quit)) {
  944. if (p_to) { // Prevent memory leak.
  945. memdelete(p_to);
  946. }
  947. return;
  948. }
  949. if (p_to) {
  950. current_scene = p_to;
  951. root->add_child(p_to);
  952. }
  953. }
  954. Error SceneTree::change_scene(const String &p_path) {
  955. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  956. if (new_scene.is_null())
  957. return ERR_CANT_OPEN;
  958. return change_scene_to(new_scene);
  959. }
  960. Error SceneTree::change_scene_to(const Ref<PackedScene> &p_scene) {
  961. Node *new_scene = NULL;
  962. if (p_scene.is_valid()) {
  963. new_scene = p_scene->instance();
  964. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  965. }
  966. call_deferred("_change_scene", new_scene);
  967. return OK;
  968. }
  969. Error SceneTree::reload_current_scene() {
  970. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  971. String fname = current_scene->get_filename();
  972. return change_scene(fname);
  973. }
  974. void SceneTree::add_current_scene(Node *p_current) {
  975. current_scene = p_current;
  976. root->add_child(p_current);
  977. }
  978. void SceneTree::drop_files(const Vector<String> &p_files, int p_from_screen) {
  979. emit_signal("files_dropped", p_files, p_from_screen);
  980. MainLoop::drop_files(p_files, p_from_screen);
  981. }
  982. void SceneTree::global_menu_action(const Variant &p_id, const Variant &p_meta) {
  983. emit_signal("global_menu_action", p_id, p_meta);
  984. MainLoop::global_menu_action(p_id, p_meta);
  985. }
  986. Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec, bool p_process_pause) {
  987. Ref<SceneTreeTimer> stt;
  988. stt.instance();
  989. stt->set_pause_mode_process(p_process_pause);
  990. stt->set_time_left(p_delay_sec);
  991. timers.push_back(stt);
  992. return stt;
  993. }
  994. void SceneTree::_network_peer_connected(int p_id) {
  995. emit_signal("network_peer_connected", p_id);
  996. }
  997. void SceneTree::_network_peer_disconnected(int p_id) {
  998. emit_signal("network_peer_disconnected", p_id);
  999. }
  1000. void SceneTree::_connected_to_server() {
  1001. emit_signal("connected_to_server");
  1002. }
  1003. void SceneTree::_connection_failed() {
  1004. emit_signal("connection_failed");
  1005. }
  1006. void SceneTree::_server_disconnected() {
  1007. emit_signal("server_disconnected");
  1008. }
  1009. Ref<MultiplayerAPI> SceneTree::get_multiplayer() const {
  1010. return multiplayer;
  1011. }
  1012. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1013. multiplayer_poll = p_enabled;
  1014. }
  1015. bool SceneTree::is_multiplayer_poll_enabled() const {
  1016. return multiplayer_poll;
  1017. }
  1018. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  1019. ERR_FAIL_COND(!p_multiplayer.is_valid());
  1020. if (multiplayer.is_valid()) {
  1021. multiplayer->disconnect("network_peer_connected", callable_mp(this, &SceneTree::_network_peer_connected));
  1022. multiplayer->disconnect("network_peer_disconnected", callable_mp(this, &SceneTree::_network_peer_disconnected));
  1023. multiplayer->disconnect("connected_to_server", callable_mp(this, &SceneTree::_connected_to_server));
  1024. multiplayer->disconnect("connection_failed", callable_mp(this, &SceneTree::_connection_failed));
  1025. multiplayer->disconnect("server_disconnected", callable_mp(this, &SceneTree::_server_disconnected));
  1026. }
  1027. multiplayer = p_multiplayer;
  1028. multiplayer->set_root_node(root);
  1029. multiplayer->connect("network_peer_connected", callable_mp(this, &SceneTree::_network_peer_connected));
  1030. multiplayer->connect("network_peer_disconnected", callable_mp(this, &SceneTree::_network_peer_disconnected));
  1031. multiplayer->connect("connected_to_server", callable_mp(this, &SceneTree::_connected_to_server));
  1032. multiplayer->connect("connection_failed", callable_mp(this, &SceneTree::_connection_failed));
  1033. multiplayer->connect("server_disconnected", callable_mp(this, &SceneTree::_server_disconnected));
  1034. }
  1035. void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer) {
  1036. multiplayer->set_network_peer(p_network_peer);
  1037. }
  1038. Ref<NetworkedMultiplayerPeer> SceneTree::get_network_peer() const {
  1039. return multiplayer->get_network_peer();
  1040. }
  1041. bool SceneTree::is_network_server() const {
  1042. return multiplayer->is_network_server();
  1043. }
  1044. bool SceneTree::has_network_peer() const {
  1045. return multiplayer->has_network_peer();
  1046. }
  1047. int SceneTree::get_network_unique_id() const {
  1048. return multiplayer->get_network_unique_id();
  1049. }
  1050. Vector<int> SceneTree::get_network_connected_peers() const {
  1051. return multiplayer->get_network_connected_peers();
  1052. }
  1053. int SceneTree::get_rpc_sender_id() const {
  1054. return multiplayer->get_rpc_sender_id();
  1055. }
  1056. void SceneTree::set_refuse_new_network_connections(bool p_refuse) {
  1057. multiplayer->set_refuse_new_network_connections(p_refuse);
  1058. }
  1059. bool SceneTree::is_refusing_new_network_connections() const {
  1060. return multiplayer->is_refusing_new_network_connections();
  1061. }
  1062. void SceneTree::_bind_methods() {
  1063. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1064. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1065. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1066. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1067. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1068. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1069. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1070. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1071. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1072. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1073. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1074. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1075. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &SceneTree::set_input_as_handled);
  1076. ClassDB::bind_method(D_METHOD("is_input_handled"), &SceneTree::is_input_handled);
  1077. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "pause_mode_process"), &SceneTree::create_timer, DEFVAL(true));
  1078. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1079. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1080. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(-1));
  1081. ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "shrink"), &SceneTree::set_screen_stretch, DEFVAL(1));
  1082. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1083. MethodInfo mi;
  1084. mi.name = "call_group_flags";
  1085. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1086. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1087. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1088. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1089. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1090. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1091. MethodInfo mi2;
  1092. mi2.name = "call_group";
  1093. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1094. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1095. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1096. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1097. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1098. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1099. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1100. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1101. ClassDB::bind_method(D_METHOD("change_scene", "path"), &SceneTree::change_scene);
  1102. ClassDB::bind_method(D_METHOD("change_scene_to", "packed_scene"), &SceneTree::change_scene_to);
  1103. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1104. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1105. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer"), &SceneTree::set_multiplayer);
  1106. ClassDB::bind_method(D_METHOD("get_multiplayer"), &SceneTree::get_multiplayer);
  1107. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1108. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1109. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &SceneTree::set_network_peer);
  1110. ClassDB::bind_method(D_METHOD("get_network_peer"), &SceneTree::get_network_peer);
  1111. ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server);
  1112. ClassDB::bind_method(D_METHOD("has_network_peer"), &SceneTree::has_network_peer);
  1113. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &SceneTree::get_network_connected_peers);
  1114. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &SceneTree::get_network_unique_id);
  1115. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &SceneTree::get_rpc_sender_id);
  1116. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &SceneTree::set_refuse_new_network_connections);
  1117. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &SceneTree::is_refusing_new_network_connections);
  1118. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &SceneTree::set_use_font_oversampling);
  1119. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &SceneTree::is_using_font_oversampling);
  1120. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1122. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  1124. ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false);
  1125. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_font_oversampling"), "set_use_font_oversampling", "is_using_font_oversampling");
  1126. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_edited_scene_root", "get_edited_scene_root");
  1127. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_current_scene", "get_current_scene");
  1128. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  1129. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "", "get_root");
  1130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer", "get_multiplayer");
  1131. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1132. ADD_SIGNAL(MethodInfo("tree_changed"));
  1133. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1134. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1135. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1136. ADD_SIGNAL(MethodInfo("screen_resized"));
  1137. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1138. ADD_SIGNAL(MethodInfo("idle_frame"));
  1139. ADD_SIGNAL(MethodInfo("physics_frame"));
  1140. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "screen")));
  1141. ADD_SIGNAL(MethodInfo("global_menu_action", PropertyInfo(Variant::NIL, "id"), PropertyInfo(Variant::NIL, "meta")));
  1142. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  1143. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  1144. ADD_SIGNAL(MethodInfo("connected_to_server"));
  1145. ADD_SIGNAL(MethodInfo("connection_failed"));
  1146. ADD_SIGNAL(MethodInfo("server_disconnected"));
  1147. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1148. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1149. BIND_ENUM_CONSTANT(GROUP_CALL_REALTIME);
  1150. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1151. BIND_ENUM_CONSTANT(STRETCH_MODE_DISABLED);
  1152. BIND_ENUM_CONSTANT(STRETCH_MODE_2D);
  1153. BIND_ENUM_CONSTANT(STRETCH_MODE_VIEWPORT);
  1154. BIND_ENUM_CONSTANT(STRETCH_ASPECT_IGNORE);
  1155. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP);
  1156. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_WIDTH);
  1157. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_HEIGHT);
  1158. BIND_ENUM_CONSTANT(STRETCH_ASPECT_EXPAND);
  1159. }
  1160. SceneTree *SceneTree::singleton = NULL;
  1161. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1162. int SceneTree::idle_callback_count = 0;
  1163. void SceneTree::_call_idle_callbacks() {
  1164. for (int i = 0; i < idle_callback_count; i++) {
  1165. idle_callbacks[i]();
  1166. }
  1167. }
  1168. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1169. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1170. idle_callbacks[idle_callback_count++] = p_callback;
  1171. }
  1172. void SceneTree::set_use_font_oversampling(bool p_oversampling) {
  1173. if (use_font_oversampling == p_oversampling)
  1174. return;
  1175. use_font_oversampling = p_oversampling;
  1176. _update_root_rect();
  1177. }
  1178. bool SceneTree::is_using_font_oversampling() const {
  1179. return use_font_oversampling;
  1180. }
  1181. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1182. if (p_function == "change_scene") {
  1183. DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1184. List<String> directories;
  1185. directories.push_back(dir_access->get_current_dir());
  1186. while (!directories.empty()) {
  1187. dir_access->change_dir(directories.back()->get());
  1188. directories.pop_back();
  1189. dir_access->list_dir_begin();
  1190. String filename = dir_access->get_next();
  1191. while (filename != "") {
  1192. if (filename == "." || filename == "..") {
  1193. filename = dir_access->get_next();
  1194. continue;
  1195. }
  1196. if (dir_access->dir_exists(filename)) {
  1197. directories.push_back(dir_access->get_current_dir().plus_file(filename));
  1198. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1199. r_options->push_back("\"" + dir_access->get_current_dir().plus_file(filename) + "\"");
  1200. }
  1201. filename = dir_access->get_next();
  1202. }
  1203. }
  1204. }
  1205. }
  1206. SceneTree::SceneTree() {
  1207. if (singleton == NULL) singleton = this;
  1208. _quit = false;
  1209. accept_quit = true;
  1210. quit_on_go_back = true;
  1211. initialized = false;
  1212. use_font_oversampling = false;
  1213. #ifdef DEBUG_ENABLED
  1214. debug_collisions_hint = false;
  1215. debug_navigation_hint = false;
  1216. #endif
  1217. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.5));
  1218. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1219. debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1220. debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4));
  1221. collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
  1222. ProjectSettings::get_singleton()->set_custom_property_info("debug/shapes/collision/max_contacts_displayed", PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1")); // No negative
  1223. tree_version = 1;
  1224. physics_process_time = 1;
  1225. idle_process_time = 1;
  1226. root = NULL;
  1227. input_handled = false;
  1228. pause = false;
  1229. current_frame = 0;
  1230. current_event = 0;
  1231. tree_changed_name = "tree_changed";
  1232. node_added_name = "node_added";
  1233. node_removed_name = "node_removed";
  1234. node_renamed_name = "node_renamed";
  1235. ugc_locked = false;
  1236. call_lock = 0;
  1237. root_lock = 0;
  1238. node_count = 0;
  1239. //create with mainloop
  1240. root = memnew(Viewport);
  1241. root->set_name("root");
  1242. root->set_handle_input_locally(false);
  1243. if (!root->get_world().is_valid())
  1244. root->set_world(Ref<World>(memnew(World)));
  1245. // Initialize network state
  1246. multiplayer_poll = true;
  1247. set_multiplayer(Ref<MultiplayerAPI>(memnew(MultiplayerAPI)));
  1248. //root->set_world_2d( Ref<World2D>( memnew( World2D )));
  1249. root->set_as_audio_listener(true);
  1250. root->set_as_audio_listener_2d(true);
  1251. current_scene = NULL;
  1252. int msaa_mode = GLOBAL_DEF("rendering/quality/filters/msaa", 0);
  1253. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/filters/msaa", PropertyInfo(Variant::INT, "rendering/quality/filters/msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"));
  1254. root->set_msaa(Viewport::MSAA(msaa_mode));
  1255. { //load default fallback environment
  1256. //get possible extensions
  1257. List<String> exts;
  1258. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1259. String ext_hint;
  1260. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  1261. if (ext_hint != String())
  1262. ext_hint += ",";
  1263. ext_hint += "*." + E->get();
  1264. }
  1265. //get path
  1266. String env_path = GLOBAL_DEF("rendering/environment/default_environment", "");
  1267. //setup property
  1268. ProjectSettings::get_singleton()->set_custom_property_info("rendering/environment/default_environment", PropertyInfo(Variant::STRING, "rendering/viewport/default_environment", PROPERTY_HINT_FILE, ext_hint));
  1269. env_path = env_path.strip_edges();
  1270. if (env_path != String()) {
  1271. Ref<Environment> env = ResourceLoader::load(env_path);
  1272. if (env.is_valid()) {
  1273. root->get_world()->set_fallback_environment(env);
  1274. } else {
  1275. if (Engine::get_singleton()->is_editor_hint()) {
  1276. //file was erased, clear the field.
  1277. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  1278. } else {
  1279. //file was erased, notify user.
  1280. ERR_PRINT(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1281. }
  1282. }
  1283. }
  1284. }
  1285. stretch_mode = STRETCH_MODE_DISABLED;
  1286. stretch_aspect = STRETCH_ASPECT_IGNORE;
  1287. stretch_shrink = 1;
  1288. last_screen_size = Size2(OS::get_singleton()->get_window_size().width, OS::get_singleton()->get_window_size().height);
  1289. _update_root_rect();
  1290. if (ScriptDebugger::get_singleton()) {
  1291. ScriptDebugger::get_singleton()->set_multiplayer(multiplayer);
  1292. }
  1293. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1294. #ifdef TOOLS_ENABLED
  1295. edited_scene_root = NULL;
  1296. #endif
  1297. }
  1298. SceneTree::~SceneTree() {
  1299. if (root) {
  1300. root->_set_tree(NULL);
  1301. root->_propagate_after_exit_tree();
  1302. memdelete(root);
  1303. }
  1304. if (singleton == this) singleton = NULL;
  1305. }