scene_tree.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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/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 "main/input_default.h"
  40. #include "node.h"
  41. #include "scene/animation/scene_tree_tween.h"
  42. #include "scene/debugger/script_debugger_remote.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 "modules/modules_enabled.gen.h" // For freetype.
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. void SceneTreeTimer::_bind_methods() {
  56. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  57. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  58. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_left"), "set_time_left", "get_time_left");
  59. ADD_SIGNAL(MethodInfo("timeout"));
  60. }
  61. void SceneTreeTimer::set_time_left(float p_time) {
  62. time_left = p_time;
  63. }
  64. float SceneTreeTimer::get_time_left() const {
  65. return time_left;
  66. }
  67. void SceneTreeTimer::set_pause_mode_process(bool p_pause_mode_process) {
  68. process_pause = p_pause_mode_process;
  69. }
  70. bool SceneTreeTimer::is_pause_mode_process() {
  71. return process_pause;
  72. }
  73. void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
  74. ignore_time_scale = p_ignore;
  75. }
  76. bool SceneTreeTimer::is_ignore_time_scale() {
  77. return ignore_time_scale;
  78. }
  79. void SceneTreeTimer::release_connections() {
  80. List<Connection> connections;
  81. get_all_signal_connections(&connections);
  82. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  83. Connection const &connection = E->get();
  84. disconnect(connection.signal, connection.target, connection.method);
  85. }
  86. }
  87. SceneTreeTimer::SceneTreeTimer() {
  88. time_left = 0;
  89. process_pause = true;
  90. }
  91. // This should be called once per physics tick, to make sure the transform previous and current
  92. // is kept up to date on the few spatials that are using client side physics interpolation
  93. void SceneTree::ClientPhysicsInterpolation::physics_process() {
  94. for (SelfList<Spatial> *E = _spatials_list.first(); E;) {
  95. Spatial *spatial = E->self();
  96. SelfList<Spatial> *current = E;
  97. // get the next element here BEFORE we potentially delete one
  98. E = E->next();
  99. // This will return false if the spatial has timed out ..
  100. // i.e. If get_global_transform_interpolated() has not been called
  101. // for a few seconds, we can delete from the list to keep processing
  102. // to a minimum.
  103. if (!spatial->update_client_physics_interpolation_data()) {
  104. _spatials_list.remove(current);
  105. }
  106. }
  107. }
  108. void SceneTree::tree_changed() {
  109. tree_version++;
  110. emit_signal(tree_changed_name);
  111. }
  112. void SceneTree::node_added(Node *p_node) {
  113. emit_signal(node_added_name, p_node);
  114. }
  115. void SceneTree::node_removed(Node *p_node) {
  116. if (current_scene == p_node) {
  117. current_scene = nullptr;
  118. }
  119. emit_signal(node_removed_name, p_node);
  120. if (call_lock > 0) {
  121. call_skip.insert(p_node);
  122. }
  123. }
  124. void SceneTree::node_renamed(Node *p_node) {
  125. emit_signal(node_renamed_name, p_node);
  126. }
  127. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  128. Map<StringName, Group>::Element *E = group_map.find(p_group);
  129. if (!E) {
  130. E = group_map.insert(p_group, Group());
  131. }
  132. ERR_FAIL_COND_V_MSG(E->get().nodes.find(p_node) != -1, &E->get(), "Already in group: " + p_group + ".");
  133. E->get().nodes.push_back(p_node);
  134. //E->get().last_tree_version=0;
  135. E->get().changed = true;
  136. return &E->get();
  137. }
  138. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  139. Map<StringName, Group>::Element *E = group_map.find(p_group);
  140. ERR_FAIL_COND(!E);
  141. E->get().nodes.erase(p_node);
  142. if (E->get().nodes.empty()) {
  143. group_map.erase(E);
  144. }
  145. }
  146. void SceneTree::make_group_changed(const StringName &p_group) {
  147. Map<StringName, Group>::Element *E = group_map.find(p_group);
  148. if (E) {
  149. E->get().changed = true;
  150. }
  151. }
  152. void SceneTree::flush_transform_notifications() {
  153. SelfList<Node> *n = xform_change_list.first();
  154. while (n) {
  155. Node *node = n->self();
  156. SelfList<Node> *nx = n->next();
  157. xform_change_list.remove(n);
  158. n = nx;
  159. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  160. }
  161. }
  162. void SceneTree::_flush_ugc() {
  163. ugc_locked = true;
  164. while (unique_group_calls.size()) {
  165. Map<UGCall, Vector<Variant>>::Element *E = unique_group_calls.front();
  166. Variant v[VARIANT_ARG_MAX];
  167. for (int i = 0; i < E->get().size(); i++) {
  168. v[i] = E->get()[i];
  169. }
  170. static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
  171. call_group_flags(GROUP_CALL_REALTIME, E->key().group, E->key().call, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  172. unique_group_calls.erase(E);
  173. }
  174. ugc_locked = false;
  175. }
  176. void SceneTree::_update_group_order(Group &g, bool p_use_priority) {
  177. if (!g.changed) {
  178. return;
  179. }
  180. if (g.nodes.empty()) {
  181. return;
  182. }
  183. Node **nodes = g.nodes.ptrw();
  184. int node_count = g.nodes.size();
  185. if (p_use_priority) {
  186. SortArray<Node *, Node::ComparatorWithPriority> node_sort;
  187. node_sort.sort(nodes, node_count);
  188. } else {
  189. SortArray<Node *, Node::Comparator> node_sort;
  190. node_sort.sort(nodes, node_count);
  191. }
  192. g.changed = false;
  193. }
  194. void SceneTree::call_group_flags(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  195. Map<StringName, Group>::Element *E = group_map.find(p_group);
  196. if (!E) {
  197. return;
  198. }
  199. Group &g = E->get();
  200. if (g.nodes.empty()) {
  201. return;
  202. }
  203. if (p_call_flags & GROUP_CALL_UNIQUE && !(p_call_flags & GROUP_CALL_REALTIME)) {
  204. ERR_FAIL_COND(ugc_locked);
  205. UGCall ug;
  206. ug.call = p_function;
  207. ug.group = p_group;
  208. if (unique_group_calls.has(ug)) {
  209. return;
  210. }
  211. VARIANT_ARGPTRS;
  212. Vector<Variant> args;
  213. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  214. if (argptr[i]->get_type() == Variant::NIL) {
  215. break;
  216. }
  217. args.push_back(*argptr[i]);
  218. }
  219. unique_group_calls[ug] = args;
  220. return;
  221. }
  222. _update_group_order(g);
  223. Vector<Node *> nodes_copy = g.nodes;
  224. Node **nodes = nodes_copy.ptrw();
  225. int node_count = nodes_copy.size();
  226. call_lock++;
  227. if (p_call_flags & GROUP_CALL_REVERSE) {
  228. for (int i = node_count - 1; i >= 0; i--) {
  229. if (call_lock && call_skip.has(nodes[i])) {
  230. continue;
  231. }
  232. if (p_call_flags & GROUP_CALL_REALTIME) {
  233. if (p_call_flags & GROUP_CALL_MULTILEVEL) {
  234. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  235. } else {
  236. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  237. }
  238. } else {
  239. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  240. }
  241. }
  242. } else {
  243. for (int i = 0; i < node_count; i++) {
  244. if (call_lock && call_skip.has(nodes[i])) {
  245. continue;
  246. }
  247. if (p_call_flags & GROUP_CALL_REALTIME) {
  248. if (p_call_flags & GROUP_CALL_MULTILEVEL) {
  249. nodes[i]->call_multilevel(p_function, VARIANT_ARG_PASS);
  250. } else {
  251. nodes[i]->call(p_function, VARIANT_ARG_PASS);
  252. }
  253. } else {
  254. MessageQueue::get_singleton()->push_call(nodes[i], p_function, VARIANT_ARG_PASS);
  255. }
  256. }
  257. }
  258. call_lock--;
  259. if (call_lock == 0) {
  260. call_skip.clear();
  261. }
  262. }
  263. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  264. Map<StringName, Group>::Element *E = group_map.find(p_group);
  265. if (!E) {
  266. return;
  267. }
  268. Group &g = E->get();
  269. if (g.nodes.empty()) {
  270. return;
  271. }
  272. _update_group_order(g);
  273. Vector<Node *> nodes_copy = g.nodes;
  274. Node **nodes = nodes_copy.ptrw();
  275. int node_count = nodes_copy.size();
  276. call_lock++;
  277. if (p_call_flags & GROUP_CALL_REVERSE) {
  278. for (int i = node_count - 1; i >= 0; i--) {
  279. if (call_lock && call_skip.has(nodes[i])) {
  280. continue;
  281. }
  282. if (p_call_flags & GROUP_CALL_REALTIME) {
  283. nodes[i]->notification(p_notification);
  284. } else {
  285. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  286. }
  287. }
  288. } else {
  289. for (int i = 0; i < node_count; i++) {
  290. if (call_lock && call_skip.has(nodes[i])) {
  291. continue;
  292. }
  293. if (p_call_flags & GROUP_CALL_REALTIME) {
  294. nodes[i]->notification(p_notification);
  295. } else {
  296. MessageQueue::get_singleton()->push_notification(nodes[i], p_notification);
  297. }
  298. }
  299. }
  300. call_lock--;
  301. if (call_lock == 0) {
  302. call_skip.clear();
  303. }
  304. }
  305. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  306. Map<StringName, Group>::Element *E = group_map.find(p_group);
  307. if (!E) {
  308. return;
  309. }
  310. Group &g = E->get();
  311. if (g.nodes.empty()) {
  312. return;
  313. }
  314. _update_group_order(g);
  315. Vector<Node *> nodes_copy = g.nodes;
  316. Node **nodes = nodes_copy.ptrw();
  317. int node_count = nodes_copy.size();
  318. call_lock++;
  319. if (p_call_flags & GROUP_CALL_REVERSE) {
  320. for (int i = node_count - 1; i >= 0; i--) {
  321. if (call_lock && call_skip.has(nodes[i])) {
  322. continue;
  323. }
  324. if (p_call_flags & GROUP_CALL_REALTIME) {
  325. nodes[i]->set(p_name, p_value);
  326. } else {
  327. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  328. }
  329. }
  330. } else {
  331. for (int i = 0; i < node_count; i++) {
  332. if (call_lock && call_skip.has(nodes[i])) {
  333. continue;
  334. }
  335. if (p_call_flags & GROUP_CALL_REALTIME) {
  336. nodes[i]->set(p_name, p_value);
  337. } else {
  338. MessageQueue::get_singleton()->push_set(nodes[i], p_name, p_value);
  339. }
  340. }
  341. }
  342. call_lock--;
  343. if (call_lock == 0) {
  344. call_skip.clear();
  345. }
  346. }
  347. void SceneTree::call_group(const StringName &p_group, const StringName &p_function, VARIANT_ARG_DECLARE) {
  348. call_group_flags(0, p_group, p_function, VARIANT_ARG_PASS);
  349. }
  350. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  351. notify_group_flags(0, p_group, p_notification);
  352. }
  353. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  354. set_group_flags(0, p_group, p_name, p_value);
  355. }
  356. void SceneTree::set_input_as_handled() {
  357. input_handled = true;
  358. }
  359. void SceneTree::input_text(const String &p_text) {
  360. root_lock++;
  361. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input_text", p_text); //special one for GUI, as controls use their own process check
  362. root_lock--;
  363. }
  364. bool SceneTree::is_input_handled() {
  365. return input_handled;
  366. }
  367. void SceneTree::input_event(const Ref<InputEvent> &p_event) {
  368. if (Engine::get_singleton()->is_editor_hint() && (Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventJoypadMotion>(*p_event))) {
  369. return; //avoid joy input on editor
  370. }
  371. current_event++;
  372. root_lock++;
  373. input_handled = false;
  374. // Don't make const ref unless you can find and fix what caused GH-34691.
  375. Ref<InputEvent> ev = p_event;
  376. MainLoop::input_event(ev);
  377. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_input", ev); //special one for GUI, as controls use their own process check
  378. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_remote()) {
  379. //quit from game window using F8
  380. Ref<InputEventKey> k = ev;
  381. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_scancode() == KEY_F8) {
  382. ScriptDebugger::get_singleton()->request_quit();
  383. }
  384. }
  385. _flush_ugc();
  386. root_lock--;
  387. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  388. root_lock++;
  389. if (!input_handled) {
  390. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_vp_unhandled_input", ev); //special one for GUI, as controls use their own process check
  391. _flush_ugc();
  392. // input_handled = true; - no reason to set this as handled
  393. root_lock--;
  394. //MessageQueue::get_singleton()->flush(); //flushing here causes UI and other places slowness
  395. } else {
  396. // input_handled = true; - no reason to set this as handled
  397. root_lock--;
  398. }
  399. _call_idle_callbacks();
  400. }
  401. void SceneTree::init() {
  402. ERR_FAIL_COND(!root);
  403. initialized = true;
  404. root->_set_tree(this);
  405. MainLoop::init();
  406. }
  407. void SceneTree::set_physics_interpolation_enabled(bool p_enabled) {
  408. // disallow interpolation in editor
  409. if (Engine::get_singleton()->is_editor_hint()) {
  410. p_enabled = false;
  411. }
  412. if (p_enabled == _physics_interpolation_enabled) {
  413. return;
  414. }
  415. _physics_interpolation_enabled = p_enabled;
  416. VisualServer::get_singleton()->set_physics_interpolation_enabled(p_enabled);
  417. }
  418. bool SceneTree::is_physics_interpolation_enabled() const {
  419. return _physics_interpolation_enabled;
  420. }
  421. void SceneTree::client_physics_interpolation_add_spatial(SelfList<Spatial> *p_elem) {
  422. // This ensures that _update_physics_interpolation_data() will be called at least once every
  423. // physics tick, to ensure the previous and current transforms are kept up to date.
  424. _client_physics_interpolation._spatials_list.add(p_elem);
  425. }
  426. void SceneTree::client_physics_interpolation_remove_spatial(SelfList<Spatial> *p_elem) {
  427. _client_physics_interpolation._spatials_list.remove(p_elem);
  428. }
  429. void SceneTree::iteration_end() {
  430. // When physics interpolation is active, we want all pending transforms
  431. // to be flushed to the VisualServer before finishing a physics tick.
  432. if (_physics_interpolation_enabled) {
  433. flush_transform_notifications();
  434. }
  435. }
  436. bool SceneTree::iteration(float p_time) {
  437. root_lock++;
  438. current_frame++;
  439. if (_physics_interpolation_enabled) {
  440. VisualServer::get_singleton()->tick();
  441. }
  442. // Any objects performing client physics interpolation
  443. // should be given an opportunity to keep their previous transforms
  444. // up to take before each new physics tick.
  445. _client_physics_interpolation.physics_process();
  446. flush_transform_notifications();
  447. MainLoop::iteration(p_time);
  448. physics_process_time = p_time;
  449. emit_signal("physics_frame");
  450. _notify_group_pause("physics_process_internal", Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  451. if (GLOBAL_GET("physics/common/enable_pause_aware_picking")) {
  452. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "_process_picking", true);
  453. }
  454. _notify_group_pause("physics_process", Node::NOTIFICATION_PHYSICS_PROCESS);
  455. _flush_ugc();
  456. MessageQueue::get_singleton()->flush(); //small little hack
  457. process_tweens(p_time, true);
  458. flush_transform_notifications();
  459. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  460. root_lock--;
  461. _flush_delete_queue();
  462. _call_idle_callbacks();
  463. return _quit;
  464. }
  465. void SceneTree::_update_font_oversampling(float p_ratio) {
  466. #ifdef MODULE_FREETYPE_ENABLED
  467. if (use_font_oversampling) {
  468. DynamicFontAtSize::font_oversampling = p_ratio;
  469. DynamicFont::update_oversampling();
  470. }
  471. #endif // MODULE_FREETYPE_ENABLED
  472. }
  473. bool SceneTree::idle(float p_time) {
  474. //print_line("ram: "+itos(OS::get_singleton()->get_static_memory_usage())+" sram: "+itos(OS::get_singleton()->get_dynamic_memory_usage()));
  475. //print_line("node count: "+itos(get_node_count()));
  476. //print_line("TEXTURE RAM: "+itos(VS::get_singleton()->get_render_info(VS::INFO_TEXTURE_MEM_USED)));
  477. root_lock++;
  478. MainLoop::idle(p_time);
  479. idle_process_time = p_time;
  480. if (multiplayer_poll) {
  481. multiplayer->poll();
  482. }
  483. emit_signal("idle_frame");
  484. MessageQueue::get_singleton()->flush(); //small little hack
  485. flush_transform_notifications();
  486. _notify_group_pause("idle_process_internal", Node::NOTIFICATION_INTERNAL_PROCESS);
  487. _notify_group_pause("idle_process", Node::NOTIFICATION_PROCESS);
  488. Size2 win_size = OS::get_singleton()->get_window_size();
  489. if (win_size != last_screen_size) {
  490. last_screen_size = win_size;
  491. _update_root_rect();
  492. emit_signal("screen_resized");
  493. }
  494. _flush_ugc();
  495. MessageQueue::get_singleton()->flush(); //small little hack
  496. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  497. call_group_flags(GROUP_CALL_REALTIME, "_viewports", "update_worlds");
  498. root_lock--;
  499. _flush_delete_queue();
  500. //go through timers
  501. List<Ref<SceneTreeTimer>>::Element *L = timers.back(); //last element
  502. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
  503. List<Ref<SceneTreeTimer>>::Element *N = E->next();
  504. if (pause && !E->get()->is_pause_mode_process()) {
  505. if (E == L) {
  506. break; //break on last, so if new timers were added during list traversal, ignore them.
  507. }
  508. E = N;
  509. continue;
  510. }
  511. float time_left = E->get()->get_time_left();
  512. if (E->get()->is_ignore_time_scale()) {
  513. time_left -= Engine::get_singleton()->get_idle_frame_step();
  514. } else {
  515. time_left -= p_time;
  516. }
  517. E->get()->set_time_left(time_left);
  518. if (time_left < 0) {
  519. E->get()->emit_signal("timeout");
  520. timers.erase(E);
  521. }
  522. if (E == L) {
  523. break; //break on last, so if new timers were added during list traversal, ignore them.
  524. }
  525. E = N;
  526. }
  527. process_tweens(p_time, false);
  528. flush_transform_notifications(); //additional transforms after timers update
  529. _call_idle_callbacks();
  530. ProjectSettings::get_singleton()->update();
  531. #ifdef TOOLS_ENABLED
  532. if (Engine::get_singleton()->is_editor_hint()) {
  533. //simple hack to reload fallback environment if it changed from editor
  534. String env_path = ProjectSettings::get_singleton()->get("rendering/environment/default_environment");
  535. env_path = env_path.strip_edges(); //user may have added a space or two
  536. String cpath;
  537. Ref<Environment> fallback = get_root()->get_world()->get_fallback_environment();
  538. if (fallback.is_valid()) {
  539. cpath = fallback->get_path();
  540. }
  541. if (cpath != env_path) {
  542. if (env_path != String()) {
  543. fallback = ResourceLoader::load(env_path);
  544. if (fallback.is_null()) {
  545. //could not load fallback, set as empty
  546. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  547. }
  548. } else {
  549. fallback.unref();
  550. }
  551. get_root()->get_world()->set_fallback_environment(fallback);
  552. }
  553. }
  554. #endif
  555. if (_physics_interpolation_enabled) {
  556. VisualServer::get_singleton()->pre_draw(true);
  557. }
  558. return _quit;
  559. }
  560. void SceneTree::process_tweens(float p_delta, bool p_physics) {
  561. // This methods works similarly to how SceneTreeTimers are handled.
  562. List<Ref<SceneTreeTween>>::Element *L = tweens.back();
  563. for (List<Ref<SceneTreeTween>>::Element *E = tweens.front(); E;) {
  564. List<Ref<SceneTreeTween>>::Element *N = E->next();
  565. // Don't process if paused or process mode doesn't match.
  566. if (!E->get()->can_process(pause) || (p_physics == (E->get()->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
  567. if (E == L) {
  568. break;
  569. }
  570. E = N;
  571. continue;
  572. }
  573. if (!E->get()->step(p_delta)) {
  574. E->get()->clear();
  575. tweens.erase(E);
  576. }
  577. if (E == L) {
  578. break;
  579. }
  580. E = N;
  581. }
  582. }
  583. void SceneTree::finish() {
  584. _flush_delete_queue();
  585. _flush_ugc();
  586. initialized = false;
  587. MainLoop::finish();
  588. if (root) {
  589. root->_set_tree(nullptr);
  590. root->_propagate_after_exit_branch(true);
  591. memdelete(root); //delete root
  592. root = nullptr;
  593. }
  594. // In case deletion of some objects was queued when destructing the `root`.
  595. // E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
  596. _flush_delete_queue();
  597. // Cleanup timers.
  598. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E; E = E->next()) {
  599. E->get()->release_connections();
  600. }
  601. timers.clear();
  602. // Cleanup tweens.
  603. for (List<Ref<SceneTreeTween>>::Element *E = tweens.front(); E; E = E->next()) {
  604. E->get()->clear();
  605. }
  606. tweens.clear();
  607. }
  608. void SceneTree::quit(int p_exit_code) {
  609. if (p_exit_code >= 0) {
  610. // Override the exit code if a positive argument is given (the default is `-1`).
  611. // This is a shorthand for calling `set_exit_code()` on the OS singleton then quitting.
  612. OS::get_singleton()->set_exit_code(p_exit_code);
  613. } else if (!OS::get_singleton()->is_custom_exit_code()) {
  614. // Must customize exit code, otherwise it will default to a non-zero value
  615. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  616. }
  617. _quit = true;
  618. }
  619. void SceneTree::_notification(int p_notification) {
  620. switch (p_notification) {
  621. case NOTIFICATION_WM_QUIT_REQUEST: {
  622. get_root()->propagate_notification(p_notification);
  623. if (accept_quit) {
  624. _quit = true;
  625. break;
  626. }
  627. } break;
  628. case NOTIFICATION_WM_GO_BACK_REQUEST: {
  629. get_root()->propagate_notification(p_notification);
  630. if (quit_on_go_back) {
  631. _quit = true;
  632. break;
  633. }
  634. } break;
  635. case NOTIFICATION_WM_FOCUS_IN: {
  636. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  637. if (id) {
  638. id->ensure_touch_mouse_raised();
  639. }
  640. get_root()->propagate_notification(p_notification);
  641. } break;
  642. case NOTIFICATION_TRANSLATION_CHANGED: {
  643. if (!Engine::get_singleton()->is_editor_hint()) {
  644. get_root()->propagate_notification(p_notification);
  645. }
  646. } break;
  647. case NOTIFICATION_WM_UNFOCUS_REQUEST: {
  648. notify_group_flags(GROUP_CALL_REALTIME | GROUP_CALL_MULTILEVEL, "input", NOTIFICATION_WM_UNFOCUS_REQUEST);
  649. get_root()->propagate_notification(p_notification);
  650. } break;
  651. case NOTIFICATION_OS_MEMORY_WARNING:
  652. case NOTIFICATION_OS_IME_UPDATE:
  653. case NOTIFICATION_WM_MOUSE_ENTER:
  654. case NOTIFICATION_WM_MOUSE_EXIT:
  655. case NOTIFICATION_WM_FOCUS_OUT:
  656. case NOTIFICATION_WM_ABOUT:
  657. case NOTIFICATION_CRASH:
  658. case NOTIFICATION_APP_RESUMED:
  659. case NOTIFICATION_APP_PAUSED: {
  660. get_root()->propagate_notification(p_notification);
  661. } break;
  662. default:
  663. break;
  664. };
  665. };
  666. bool SceneTree::is_auto_accept_quit() const {
  667. return accept_quit;
  668. }
  669. void SceneTree::set_auto_accept_quit(bool p_enable) {
  670. accept_quit = p_enable;
  671. }
  672. bool SceneTree::is_quit_on_go_back() const {
  673. return quit_on_go_back;
  674. }
  675. void SceneTree::set_quit_on_go_back(bool p_enable) {
  676. quit_on_go_back = p_enable;
  677. }
  678. #ifdef TOOLS_ENABLED
  679. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  680. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_a_parent_of(p_node) || edited_scene_root == p_node);
  681. }
  682. #endif
  683. #ifdef DEBUG_ENABLED
  684. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  685. debug_collisions_hint = p_enabled;
  686. }
  687. bool SceneTree::is_debugging_collisions_hint() const {
  688. return debug_collisions_hint;
  689. }
  690. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  691. debug_navigation_hint = p_enabled;
  692. }
  693. bool SceneTree::is_debugging_navigation_hint() const {
  694. return debug_navigation_hint;
  695. }
  696. #endif
  697. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  698. debug_collisions_color = p_color;
  699. }
  700. Color SceneTree::get_debug_collisions_color() const {
  701. return debug_collisions_color;
  702. }
  703. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  704. debug_collision_contact_color = p_color;
  705. }
  706. Color SceneTree::get_debug_collision_contact_color() const {
  707. return debug_collision_contact_color;
  708. }
  709. void SceneTree::set_debug_navigation_color(const Color &p_color) {
  710. debug_navigation_color = p_color;
  711. }
  712. Color SceneTree::get_debug_navigation_color() const {
  713. return debug_navigation_color;
  714. }
  715. void SceneTree::set_debug_navigation_disabled_color(const Color &p_color) {
  716. debug_navigation_disabled_color = p_color;
  717. }
  718. Color SceneTree::get_debug_navigation_disabled_color() const {
  719. return debug_navigation_disabled_color;
  720. }
  721. Ref<Material> SceneTree::get_debug_navigation_material() {
  722. if (navigation_material.is_valid()) {
  723. return navigation_material;
  724. }
  725. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  726. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  727. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  728. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  729. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  730. line_material->set_albedo(get_debug_navigation_color());
  731. navigation_material = line_material;
  732. return navigation_material;
  733. }
  734. Ref<Material> SceneTree::get_debug_navigation_disabled_material() {
  735. if (navigation_disabled_material.is_valid()) {
  736. return navigation_disabled_material;
  737. }
  738. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  739. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  740. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  741. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  742. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  743. line_material->set_albedo(get_debug_navigation_disabled_color());
  744. navigation_disabled_material = line_material;
  745. return navigation_disabled_material;
  746. }
  747. Ref<Material> SceneTree::get_debug_collision_material() {
  748. if (collision_material.is_valid()) {
  749. return collision_material;
  750. }
  751. Ref<SpatialMaterial> line_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  752. line_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  753. line_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  754. line_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  755. line_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  756. line_material->set_albedo(get_debug_collisions_color());
  757. collision_material = line_material;
  758. return collision_material;
  759. }
  760. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  761. if (debug_contact_mesh.is_valid()) {
  762. return debug_contact_mesh;
  763. }
  764. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  765. Ref<SpatialMaterial> mat = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  766. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  767. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  768. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  769. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  770. mat->set_albedo(get_debug_collision_contact_color());
  771. Vector3 diamond[6] = {
  772. Vector3(-1, 0, 0),
  773. Vector3(1, 0, 0),
  774. Vector3(0, -1, 0),
  775. Vector3(0, 1, 0),
  776. Vector3(0, 0, -1),
  777. Vector3(0, 0, 1)
  778. };
  779. /* clang-format off */
  780. int diamond_faces[8 * 3] = {
  781. 0, 2, 4,
  782. 0, 3, 4,
  783. 1, 2, 4,
  784. 1, 3, 4,
  785. 0, 2, 5,
  786. 0, 3, 5,
  787. 1, 2, 5,
  788. 1, 3, 5,
  789. };
  790. /* clang-format on */
  791. PoolVector<int> indices;
  792. for (int i = 0; i < 8 * 3; i++) {
  793. indices.push_back(diamond_faces[i]);
  794. }
  795. PoolVector<Vector3> vertices;
  796. for (int i = 0; i < 6; i++) {
  797. vertices.push_back(diamond[i] * 0.1);
  798. }
  799. Array arr;
  800. arr.resize(Mesh::ARRAY_MAX);
  801. arr[Mesh::ARRAY_VERTEX] = vertices;
  802. arr[Mesh::ARRAY_INDEX] = indices;
  803. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  804. debug_contact_mesh->surface_set_material(0, mat);
  805. return debug_contact_mesh;
  806. }
  807. void SceneTree::set_pause(bool p_enabled) {
  808. if (p_enabled == pause) {
  809. return;
  810. }
  811. pause = p_enabled;
  812. NavigationServer::get_singleton()->set_active(!p_enabled);
  813. PhysicsServer::get_singleton()->set_active(!p_enabled);
  814. Physics2DServer::get_singleton()->set_active(!p_enabled);
  815. if (get_root()) {
  816. get_root()->propagate_notification(p_enabled ? Node::NOTIFICATION_PAUSED : Node::NOTIFICATION_UNPAUSED);
  817. }
  818. }
  819. bool SceneTree::is_paused() const {
  820. return pause;
  821. }
  822. void SceneTree::_call_input_pause(const StringName &p_group, const StringName &p_method, const Ref<InputEvent> &p_input) {
  823. Map<StringName, Group>::Element *E = group_map.find(p_group);
  824. if (!E) {
  825. return;
  826. }
  827. Group &g = E->get();
  828. if (g.nodes.empty()) {
  829. return;
  830. }
  831. _update_group_order(g);
  832. //copy, so copy on write happens in case something is removed from process while being called
  833. //performance is not lost because only if something is added/removed the vector is copied.
  834. Vector<Node *> nodes_copy = g.nodes;
  835. int node_count = nodes_copy.size();
  836. Node **nodes = nodes_copy.ptrw();
  837. Variant arg = p_input;
  838. const Variant *v[1] = { &arg };
  839. call_lock++;
  840. for (int i = node_count - 1; i >= 0; i--) {
  841. if (input_handled) {
  842. break;
  843. }
  844. Node *n = nodes[i];
  845. if (call_lock && call_skip.has(n)) {
  846. continue;
  847. }
  848. if (!n->can_process()) {
  849. continue;
  850. }
  851. n->call_multilevel(p_method, (const Variant **)v, 1);
  852. //ERR_FAIL_COND(node_count != g.nodes.size());
  853. }
  854. call_lock--;
  855. if (call_lock == 0) {
  856. call_skip.clear();
  857. }
  858. }
  859. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  860. Map<StringName, Group>::Element *E = group_map.find(p_group);
  861. if (!E) {
  862. return;
  863. }
  864. Group &g = E->get();
  865. if (g.nodes.empty()) {
  866. return;
  867. }
  868. _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);
  869. //copy, so copy on write happens in case something is removed from process while being called
  870. //performance is not lost because only if something is added/removed the vector is copied.
  871. Vector<Node *> nodes_copy = g.nodes;
  872. int node_count = nodes_copy.size();
  873. Node **nodes = nodes_copy.ptrw();
  874. call_lock++;
  875. for (int i = 0; i < node_count; i++) {
  876. Node *n = nodes[i];
  877. if (call_lock && call_skip.has(n)) {
  878. continue;
  879. }
  880. if (!n->can_process()) {
  881. continue;
  882. }
  883. if (!n->can_process_notification(p_notification)) {
  884. continue;
  885. }
  886. n->notification(p_notification);
  887. //ERR_FAIL_COND(node_count != g.nodes.size());
  888. }
  889. call_lock--;
  890. if (call_lock == 0) {
  891. call_skip.clear();
  892. }
  893. }
  894. /*
  895. void SceneMainLoop::_update_listener_2d() {
  896. if (listener_2d.is_valid()) {
  897. SpatialSound2DServer::get_singleton()->listener_set_space( listener_2d, world_2d->get_sound_space() );
  898. }
  899. }
  900. */
  901. Variant SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  902. r_error.error = Variant::CallError::CALL_OK;
  903. ERR_FAIL_COND_V(p_argcount < 3, Variant());
  904. ERR_FAIL_COND_V(!p_args[0]->is_num(), Variant());
  905. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  906. ERR_FAIL_COND_V(p_args[2]->get_type() != Variant::STRING, Variant());
  907. int flags = *p_args[0];
  908. StringName group = *p_args[1];
  909. StringName method = *p_args[2];
  910. Variant v[VARIANT_ARG_MAX];
  911. for (int i = 0; i < MIN(p_argcount - 3, VARIANT_ARG_MAX); i++) {
  912. v[i] = *p_args[i + 3];
  913. }
  914. static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
  915. call_group_flags(flags, group, method, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  916. return Variant();
  917. }
  918. Variant SceneTree::_call_group(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  919. r_error.error = Variant::CallError::CALL_OK;
  920. ERR_FAIL_COND_V(p_argcount < 2, Variant());
  921. ERR_FAIL_COND_V(p_args[0]->get_type() != Variant::STRING, Variant());
  922. ERR_FAIL_COND_V(p_args[1]->get_type() != Variant::STRING, Variant());
  923. StringName group = *p_args[0];
  924. StringName method = *p_args[1];
  925. Variant v[VARIANT_ARG_MAX];
  926. for (int i = 0; i < MIN(p_argcount - 2, 5); i++) {
  927. v[i] = *p_args[i + 2];
  928. }
  929. call_group_flags(0, group, method, v[0], v[1], v[2], v[3], v[4]);
  930. return Variant();
  931. }
  932. int64_t SceneTree::get_frame() const {
  933. return current_frame;
  934. }
  935. int64_t SceneTree::get_event_count() const {
  936. return current_event;
  937. }
  938. Array SceneTree::_get_nodes_in_group(const StringName &p_group) {
  939. Array ret;
  940. Map<StringName, Group>::Element *E = group_map.find(p_group);
  941. if (!E) {
  942. return ret;
  943. }
  944. _update_group_order(E->get()); //update order just in case
  945. int nc = E->get().nodes.size();
  946. if (nc == 0) {
  947. return ret;
  948. }
  949. ret.resize(nc);
  950. Node **ptr = E->get().nodes.ptrw();
  951. for (int i = 0; i < nc; i++) {
  952. ret[i] = ptr[i];
  953. }
  954. return ret;
  955. }
  956. bool SceneTree::has_group(const StringName &p_identifier) const {
  957. return group_map.has(p_identifier);
  958. }
  959. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  960. Map<StringName, Group>::Element *E = group_map.find(p_group);
  961. if (!E) {
  962. return;
  963. }
  964. _update_group_order(E->get()); //update order just in case
  965. int nc = E->get().nodes.size();
  966. if (nc == 0) {
  967. return;
  968. }
  969. Node **ptr = E->get().nodes.ptrw();
  970. for (int i = 0; i < nc; i++) {
  971. p_list->push_back(ptr[i]);
  972. }
  973. }
  974. void SceneTree::_flush_delete_queue() {
  975. _THREAD_SAFE_METHOD_
  976. while (delete_queue.size()) {
  977. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  978. if (obj) {
  979. memdelete(obj);
  980. }
  981. delete_queue.pop_front();
  982. }
  983. }
  984. void SceneTree::queue_delete(Object *p_object) {
  985. _THREAD_SAFE_METHOD_
  986. ERR_FAIL_NULL(p_object);
  987. p_object->_is_queued_for_deletion = true;
  988. delete_queue.push_back(p_object->get_instance_id());
  989. }
  990. int SceneTree::get_node_count() const {
  991. return node_count;
  992. }
  993. void SceneTree::_update_root_rect() {
  994. if (stretch_mode == STRETCH_MODE_DISABLED) {
  995. _update_font_oversampling(stretch_scale);
  996. root->set_size(last_screen_size.floor());
  997. root->set_attach_to_screen_rect(Rect2(Point2(), last_screen_size));
  998. root->set_size_override_stretch(true);
  999. root->set_size_override(true, (last_screen_size / stretch_scale).floor());
  1000. root->update_canvas_items();
  1001. return; //user will take care
  1002. }
  1003. //actual screen video mode
  1004. Size2 video_mode = OS::get_singleton()->get_window_size();
  1005. Size2 desired_res = stretch_min;
  1006. Size2 viewport_size;
  1007. Size2 screen_size;
  1008. float viewport_aspect = desired_res.aspect();
  1009. float video_mode_aspect = video_mode.aspect();
  1010. if (stretch_aspect == STRETCH_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  1011. //same aspect or ignore aspect
  1012. viewport_size = desired_res;
  1013. screen_size = video_mode;
  1014. } else if (viewport_aspect < video_mode_aspect) {
  1015. // screen ratio is smaller vertically
  1016. if (stretch_aspect == STRETCH_ASPECT_KEEP_HEIGHT || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  1017. //will stretch horizontally
  1018. viewport_size.x = desired_res.y * video_mode_aspect;
  1019. viewport_size.y = desired_res.y;
  1020. screen_size = video_mode;
  1021. } else {
  1022. //will need black bars
  1023. viewport_size = desired_res;
  1024. screen_size.x = video_mode.y * viewport_aspect;
  1025. screen_size.y = video_mode.y;
  1026. }
  1027. } else {
  1028. //screen ratio is smaller horizontally
  1029. if (stretch_aspect == STRETCH_ASPECT_KEEP_WIDTH || stretch_aspect == STRETCH_ASPECT_EXPAND) {
  1030. //will stretch horizontally
  1031. viewport_size.x = desired_res.x;
  1032. viewport_size.y = desired_res.x / video_mode_aspect;
  1033. screen_size = video_mode;
  1034. } else {
  1035. //will need black bars
  1036. viewport_size = desired_res;
  1037. screen_size.x = video_mode.x;
  1038. screen_size.y = video_mode.x / viewport_aspect;
  1039. }
  1040. }
  1041. screen_size = screen_size.floor();
  1042. viewport_size = viewport_size.floor();
  1043. Size2 margin;
  1044. Size2 offset;
  1045. //black bars and margin
  1046. if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  1047. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  1048. VisualServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  1049. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  1050. } else if (stretch_aspect != STRETCH_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  1051. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  1052. VisualServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  1053. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  1054. } else {
  1055. VisualServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  1056. }
  1057. switch (stretch_mode) {
  1058. case STRETCH_MODE_DISABLED: {
  1059. // Already handled above
  1060. } break;
  1061. case STRETCH_MODE_2D: {
  1062. _update_font_oversampling((screen_size.x / viewport_size.x) * stretch_scale); //screen / viewport ratio drives oversampling
  1063. root->set_size(screen_size.floor());
  1064. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  1065. root->set_size_override_stretch(true);
  1066. root->set_size_override(true, (viewport_size / stretch_scale).floor());
  1067. root->update_canvas_items(); //force them to update just in case
  1068. } break;
  1069. case STRETCH_MODE_VIEWPORT: {
  1070. _update_font_oversampling(1.0);
  1071. root->set_size((viewport_size / stretch_scale).floor());
  1072. root->set_attach_to_screen_rect(Rect2(margin, screen_size));
  1073. root->set_size_override_stretch(false);
  1074. root->set_size_override(false, Size2());
  1075. root->update_canvas_items(); //force them to update just in case
  1076. } break;
  1077. }
  1078. }
  1079. void SceneTree::set_screen_stretch(StretchMode p_mode, StretchAspect p_aspect, const Size2 &p_minsize, real_t p_scale) {
  1080. stretch_mode = p_mode;
  1081. stretch_aspect = p_aspect;
  1082. stretch_min = p_minsize;
  1083. stretch_scale = p_scale;
  1084. _update_root_rect();
  1085. }
  1086. void SceneTree::set_edited_scene_root(Node *p_node) {
  1087. #ifdef TOOLS_ENABLED
  1088. edited_scene_root = p_node;
  1089. #endif
  1090. }
  1091. Node *SceneTree::get_edited_scene_root() const {
  1092. #ifdef TOOLS_ENABLED
  1093. return edited_scene_root;
  1094. #else
  1095. return NULL;
  1096. #endif
  1097. }
  1098. void SceneTree::set_current_scene(Node *p_scene) {
  1099. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  1100. current_scene = p_scene;
  1101. }
  1102. Node *SceneTree::get_current_scene() const {
  1103. return current_scene;
  1104. }
  1105. void SceneTree::_change_scene(Node *p_to) {
  1106. if (current_scene) {
  1107. memdelete(current_scene);
  1108. current_scene = nullptr;
  1109. }
  1110. // If we're quitting, abort.
  1111. if (unlikely(_quit)) {
  1112. if (p_to) { // Prevent memory leak.
  1113. memdelete(p_to);
  1114. }
  1115. return;
  1116. }
  1117. if (p_to) {
  1118. current_scene = p_to;
  1119. root->add_child(p_to);
  1120. }
  1121. }
  1122. Error SceneTree::change_scene(const String &p_path) {
  1123. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  1124. if (new_scene.is_null()) {
  1125. return ERR_CANT_OPEN;
  1126. }
  1127. return change_scene_to(new_scene);
  1128. }
  1129. Error SceneTree::change_scene_to(const Ref<PackedScene> &p_scene) {
  1130. Node *new_scene = nullptr;
  1131. if (p_scene.is_valid()) {
  1132. new_scene = p_scene->instance();
  1133. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  1134. }
  1135. call_deferred("_change_scene", new_scene);
  1136. return OK;
  1137. }
  1138. Error SceneTree::reload_current_scene() {
  1139. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  1140. String fname = current_scene->get_filename();
  1141. return change_scene(fname);
  1142. }
  1143. void SceneTree::add_current_scene(Node *p_current) {
  1144. current_scene = p_current;
  1145. root->add_child(p_current);
  1146. }
  1147. #ifdef DEBUG_ENABLED
  1148. static void _fill_array(Node *p_node, Array &array, int p_level) {
  1149. array.push_back(p_node->get_child_count());
  1150. array.push_back(p_node->get_name());
  1151. array.push_back(p_node->get_class());
  1152. array.push_back(p_node->get_instance_id());
  1153. for (int i = 0; i < p_node->get_child_count(); i++) {
  1154. _fill_array(p_node->get_child(i), array, p_level + 1);
  1155. }
  1156. }
  1157. void SceneTree::_debugger_request_tree() {
  1158. Array arr;
  1159. _fill_array(root, arr, 0);
  1160. ScriptDebugger::get_singleton()->send_message("scene_tree", arr);
  1161. }
  1162. void SceneTree::_live_edit_node_path_func(const NodePath &p_path, int p_id) {
  1163. live_edit_node_path_cache[p_id] = p_path;
  1164. }
  1165. void SceneTree::_live_edit_res_path_func(const String &p_path, int p_id) {
  1166. live_edit_resource_cache[p_id] = p_path;
  1167. }
  1168. void SceneTree::_live_edit_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  1169. if (!live_edit_node_path_cache.has(p_id)) {
  1170. return;
  1171. }
  1172. NodePath np = live_edit_node_path_cache[p_id];
  1173. Node *base = nullptr;
  1174. if (root->has_node(live_edit_root)) {
  1175. base = root->get_node(live_edit_root);
  1176. }
  1177. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1178. if (!E) {
  1179. return; //scene not editable
  1180. }
  1181. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1182. Node *n = F->get();
  1183. if (base && !base->is_a_parent_of(n)) {
  1184. continue;
  1185. }
  1186. if (!n->has_node(np)) {
  1187. continue;
  1188. }
  1189. Node *n2 = n->get_node(np);
  1190. n2->set(p_prop, p_value);
  1191. }
  1192. }
  1193. void SceneTree::_live_edit_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  1194. RES r = ResourceLoader::load(p_value);
  1195. if (!r.is_valid()) {
  1196. return;
  1197. }
  1198. _live_edit_node_set_func(p_id, p_prop, r);
  1199. }
  1200. void SceneTree::_live_edit_node_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  1201. if (!live_edit_node_path_cache.has(p_id)) {
  1202. return;
  1203. }
  1204. NodePath np = live_edit_node_path_cache[p_id];
  1205. Node *base = nullptr;
  1206. if (root->has_node(live_edit_root)) {
  1207. base = root->get_node(live_edit_root);
  1208. }
  1209. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1210. if (!E) {
  1211. return; //scene not editable
  1212. }
  1213. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1214. Node *n = F->get();
  1215. if (base && !base->is_a_parent_of(n)) {
  1216. continue;
  1217. }
  1218. if (!n->has_node(np)) {
  1219. continue;
  1220. }
  1221. Node *n2 = n->get_node(np);
  1222. n2->call(p_method, VARIANT_ARG_PASS);
  1223. }
  1224. }
  1225. void SceneTree::_live_edit_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  1226. if (!live_edit_resource_cache.has(p_id)) {
  1227. return;
  1228. }
  1229. String resp = live_edit_resource_cache[p_id];
  1230. if (!ResourceCache::has(resp)) {
  1231. return;
  1232. }
  1233. RES r = ResourceCache::get(resp);
  1234. if (!r.is_valid()) {
  1235. return;
  1236. }
  1237. r->set(p_prop, p_value);
  1238. }
  1239. void SceneTree::_live_edit_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  1240. RES r = ResourceLoader::load(p_value);
  1241. if (!r.is_valid()) {
  1242. return;
  1243. }
  1244. _live_edit_res_set_func(p_id, p_prop, r);
  1245. }
  1246. void SceneTree::_live_edit_res_call_func(int p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  1247. if (!live_edit_resource_cache.has(p_id)) {
  1248. return;
  1249. }
  1250. String resp = live_edit_resource_cache[p_id];
  1251. if (!ResourceCache::has(resp)) {
  1252. return;
  1253. }
  1254. RES r = ResourceCache::get(resp);
  1255. if (!r.is_valid()) {
  1256. return;
  1257. }
  1258. r->call(p_method, VARIANT_ARG_PASS);
  1259. }
  1260. void SceneTree::_live_edit_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  1261. live_edit_root = p_scene_path;
  1262. live_edit_scene = p_scene_from;
  1263. }
  1264. void SceneTree::_live_edit_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1265. Node *base = nullptr;
  1266. if (root->has_node(live_edit_root)) {
  1267. base = root->get_node(live_edit_root);
  1268. }
  1269. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1270. if (!E) {
  1271. return; //scene not editable
  1272. }
  1273. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1274. Node *n = F->get();
  1275. if (base && !base->is_a_parent_of(n)) {
  1276. continue;
  1277. }
  1278. if (!n->has_node(p_parent)) {
  1279. continue;
  1280. }
  1281. Node *n2 = n->get_node(p_parent);
  1282. Node *no = Object::cast_to<Node>(ClassDB::instance(p_type));
  1283. if (!no) {
  1284. continue;
  1285. }
  1286. no->set_name(p_name);
  1287. n2->add_child(no);
  1288. }
  1289. }
  1290. void SceneTree::_live_edit_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1291. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  1292. if (!ps.is_valid()) {
  1293. return;
  1294. }
  1295. Node *base = nullptr;
  1296. if (root->has_node(live_edit_root)) {
  1297. base = root->get_node(live_edit_root);
  1298. }
  1299. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1300. if (!E) {
  1301. return; //scene not editable
  1302. }
  1303. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1304. Node *n = F->get();
  1305. if (base && !base->is_a_parent_of(n)) {
  1306. continue;
  1307. }
  1308. if (!n->has_node(p_parent)) {
  1309. continue;
  1310. }
  1311. Node *n2 = n->get_node(p_parent);
  1312. Node *no = ps->instance();
  1313. if (!no) {
  1314. continue;
  1315. }
  1316. no->set_name(p_name);
  1317. n2->add_child(no);
  1318. }
  1319. }
  1320. void SceneTree::_live_edit_remove_node_func(const NodePath &p_at) {
  1321. Node *base = nullptr;
  1322. if (root->has_node(live_edit_root)) {
  1323. base = root->get_node(live_edit_root);
  1324. }
  1325. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1326. if (!E) {
  1327. return; //scene not editable
  1328. }
  1329. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1330. Set<Node *>::Element *N = F->next();
  1331. Node *n = F->get();
  1332. if (base && !base->is_a_parent_of(n)) {
  1333. continue;
  1334. }
  1335. if (!n->has_node(p_at)) {
  1336. continue;
  1337. }
  1338. Node *n2 = n->get_node(p_at);
  1339. memdelete(n2);
  1340. F = N;
  1341. }
  1342. }
  1343. void SceneTree::_live_edit_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  1344. Node *base = nullptr;
  1345. if (root->has_node(live_edit_root)) {
  1346. base = root->get_node(live_edit_root);
  1347. }
  1348. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1349. if (!E) {
  1350. return; //scene not editable
  1351. }
  1352. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1353. Set<Node *>::Element *N = F->next();
  1354. Node *n = F->get();
  1355. if (base && !base->is_a_parent_of(n)) {
  1356. continue;
  1357. }
  1358. if (!n->has_node(p_at)) {
  1359. continue;
  1360. }
  1361. Node *n2 = n->get_node(p_at);
  1362. n2->get_parent()->remove_child(n2);
  1363. live_edit_remove_list[n][p_keep_id] = n2;
  1364. F = N;
  1365. }
  1366. }
  1367. void SceneTree::_live_edit_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1368. Node *base = nullptr;
  1369. if (root->has_node(live_edit_root)) {
  1370. base = root->get_node(live_edit_root);
  1371. }
  1372. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1373. if (!E) {
  1374. return; //scene not editable
  1375. }
  1376. for (Set<Node *>::Element *F = E->get().front(); F;) {
  1377. Set<Node *>::Element *N = F->next();
  1378. Node *n = F->get();
  1379. if (base && !base->is_a_parent_of(n)) {
  1380. continue;
  1381. }
  1382. if (!n->has_node(p_at)) {
  1383. continue;
  1384. }
  1385. Node *n2 = n->get_node(p_at);
  1386. Map<Node *, Map<ObjectID, Node *>>::Element *EN = live_edit_remove_list.find(n);
  1387. if (!EN) {
  1388. continue;
  1389. }
  1390. Map<ObjectID, Node *>::Element *FN = EN->get().find(p_id);
  1391. if (!FN) {
  1392. continue;
  1393. }
  1394. n2->add_child(FN->get());
  1395. EN->get().erase(FN);
  1396. if (EN->get().size() == 0) {
  1397. live_edit_remove_list.erase(EN);
  1398. }
  1399. F = N;
  1400. }
  1401. }
  1402. void SceneTree::_live_edit_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  1403. Node *base = nullptr;
  1404. if (root->has_node(live_edit_root)) {
  1405. base = root->get_node(live_edit_root);
  1406. }
  1407. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1408. if (!E) {
  1409. return; //scene not editable
  1410. }
  1411. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1412. Node *n = F->get();
  1413. if (base && !base->is_a_parent_of(n)) {
  1414. continue;
  1415. }
  1416. if (!n->has_node(p_at)) {
  1417. continue;
  1418. }
  1419. Node *n2 = n->get_node(p_at);
  1420. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  1421. if (!dup) {
  1422. continue;
  1423. }
  1424. dup->set_name(p_new_name);
  1425. n2->get_parent()->add_child(dup);
  1426. }
  1427. }
  1428. void SceneTree::_live_edit_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1429. Node *base = nullptr;
  1430. if (root->has_node(live_edit_root)) {
  1431. base = root->get_node(live_edit_root);
  1432. }
  1433. Map<String, Set<Node *>>::Element *E = live_scene_edit_cache.find(live_edit_scene);
  1434. if (!E) {
  1435. return; //scene not editable
  1436. }
  1437. for (Set<Node *>::Element *F = E->get().front(); F; F = F->next()) {
  1438. Node *n = F->get();
  1439. if (base && !base->is_a_parent_of(n)) {
  1440. continue;
  1441. }
  1442. if (!n->has_node(p_at)) {
  1443. continue;
  1444. }
  1445. Node *nfrom = n->get_node(p_at);
  1446. if (!n->has_node(p_new_place)) {
  1447. continue;
  1448. }
  1449. Node *nto = n->get_node(p_new_place);
  1450. nfrom->get_parent()->remove_child(nfrom);
  1451. nfrom->set_name(p_new_name);
  1452. nto->add_child(nfrom);
  1453. if (p_at_pos >= 0) {
  1454. nto->move_child(nfrom, p_at_pos);
  1455. }
  1456. }
  1457. }
  1458. #endif
  1459. void SceneTree::drop_files(const Vector<String> &p_files, int p_from_screen) {
  1460. emit_signal("files_dropped", p_files, p_from_screen);
  1461. MainLoop::drop_files(p_files, p_from_screen);
  1462. }
  1463. void SceneTree::global_menu_action(const Variant &p_id, const Variant &p_meta) {
  1464. emit_signal("global_menu_action", p_id, p_meta);
  1465. MainLoop::global_menu_action(p_id, p_meta);
  1466. }
  1467. Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec, bool p_process_pause) {
  1468. Ref<SceneTreeTimer> stt;
  1469. stt.instance();
  1470. stt->set_pause_mode_process(p_process_pause);
  1471. stt->set_time_left(p_delay_sec);
  1472. timers.push_back(stt);
  1473. return stt;
  1474. }
  1475. Ref<SceneTreeTween> SceneTree::create_tween() {
  1476. Ref<SceneTreeTween> tween = memnew(SceneTreeTween(true));
  1477. tweens.push_back(tween);
  1478. return tween;
  1479. }
  1480. Array SceneTree::get_processed_tweens() {
  1481. Array ret;
  1482. ret.resize(tweens.size());
  1483. for (int i = 0; i < tweens.size(); i++) {
  1484. ret[i] = tweens[i];
  1485. }
  1486. return ret;
  1487. }
  1488. void SceneTree::_network_peer_connected(int p_id) {
  1489. emit_signal("network_peer_connected", p_id);
  1490. }
  1491. void SceneTree::_network_peer_disconnected(int p_id) {
  1492. emit_signal("network_peer_disconnected", p_id);
  1493. }
  1494. void SceneTree::_connected_to_server() {
  1495. emit_signal("connected_to_server");
  1496. }
  1497. void SceneTree::_connection_failed() {
  1498. emit_signal("connection_failed");
  1499. }
  1500. void SceneTree::_server_disconnected() {
  1501. emit_signal("server_disconnected");
  1502. }
  1503. Ref<MultiplayerAPI> SceneTree::get_multiplayer() const {
  1504. return multiplayer;
  1505. }
  1506. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1507. multiplayer_poll = p_enabled;
  1508. }
  1509. bool SceneTree::is_multiplayer_poll_enabled() const {
  1510. return multiplayer_poll;
  1511. }
  1512. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  1513. ERR_FAIL_COND(!p_multiplayer.is_valid());
  1514. if (multiplayer.is_valid()) {
  1515. multiplayer->disconnect("network_peer_connected", this, "_network_peer_connected");
  1516. multiplayer->disconnect("network_peer_disconnected", this, "_network_peer_disconnected");
  1517. multiplayer->disconnect("connected_to_server", this, "_connected_to_server");
  1518. multiplayer->disconnect("connection_failed", this, "_connection_failed");
  1519. multiplayer->disconnect("server_disconnected", this, "_server_disconnected");
  1520. }
  1521. multiplayer = p_multiplayer;
  1522. multiplayer->set_root_node(root);
  1523. multiplayer->connect("network_peer_connected", this, "_network_peer_connected");
  1524. multiplayer->connect("network_peer_disconnected", this, "_network_peer_disconnected");
  1525. multiplayer->connect("connected_to_server", this, "_connected_to_server");
  1526. multiplayer->connect("connection_failed", this, "_connection_failed");
  1527. multiplayer->connect("server_disconnected", this, "_server_disconnected");
  1528. }
  1529. void SceneTree::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_network_peer) {
  1530. multiplayer->set_network_peer(p_network_peer);
  1531. }
  1532. Ref<NetworkedMultiplayerPeer> SceneTree::get_network_peer() const {
  1533. return multiplayer->get_network_peer();
  1534. }
  1535. bool SceneTree::is_network_server() const {
  1536. return multiplayer->is_network_server();
  1537. }
  1538. bool SceneTree::has_network_peer() const {
  1539. return multiplayer->has_network_peer();
  1540. }
  1541. int SceneTree::get_network_unique_id() const {
  1542. return multiplayer->get_network_unique_id();
  1543. }
  1544. Vector<int> SceneTree::get_network_connected_peers() const {
  1545. return multiplayer->get_network_connected_peers();
  1546. }
  1547. int SceneTree::get_rpc_sender_id() const {
  1548. return multiplayer->get_rpc_sender_id();
  1549. }
  1550. void SceneTree::set_refuse_new_network_connections(bool p_refuse) {
  1551. multiplayer->set_refuse_new_network_connections(p_refuse);
  1552. }
  1553. bool SceneTree::is_refusing_new_network_connections() const {
  1554. return multiplayer->is_refusing_new_network_connections();
  1555. }
  1556. void SceneTree::_bind_methods() {
  1557. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1558. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1559. ClassDB::bind_method(D_METHOD("is_auto_accept_quit"), &SceneTree::is_auto_accept_quit);
  1560. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1561. ClassDB::bind_method(D_METHOD("is_quit_on_go_back"), &SceneTree::is_quit_on_go_back);
  1562. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1563. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1564. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1565. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1566. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1567. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1568. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1569. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1570. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1571. ClassDB::bind_method(D_METHOD("set_input_as_handled"), &SceneTree::set_input_as_handled);
  1572. ClassDB::bind_method(D_METHOD("is_input_handled"), &SceneTree::is_input_handled);
  1573. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "pause_mode_process"), &SceneTree::create_timer, DEFVAL(true));
  1574. ClassDB::bind_method(D_METHOD("create_tween"), &SceneTree::create_tween);
  1575. ClassDB::bind_method(D_METHOD("get_processed_tweens"), &SceneTree::get_processed_tweens);
  1576. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1577. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1578. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(-1));
  1579. ClassDB::bind_method(D_METHOD("set_screen_stretch", "mode", "aspect", "minsize", "scale"), &SceneTree::set_screen_stretch, DEFVAL(1));
  1580. ClassDB::bind_method(D_METHOD("set_physics_interpolation_enabled", "enabled"), &SceneTree::set_physics_interpolation_enabled);
  1581. ClassDB::bind_method(D_METHOD("is_physics_interpolation_enabled"), &SceneTree::is_physics_interpolation_enabled);
  1582. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1583. MethodInfo mi;
  1584. mi.name = "call_group_flags";
  1585. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1586. mi.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1587. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1588. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1589. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1590. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1591. MethodInfo mi2;
  1592. mi2.name = "call_group";
  1593. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "group"));
  1594. mi2.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  1595. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1596. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1597. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1598. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1599. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1600. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1601. ClassDB::bind_method(D_METHOD("change_scene", "path"), &SceneTree::change_scene);
  1602. ClassDB::bind_method(D_METHOD("change_scene_to", "packed_scene"), &SceneTree::change_scene_to);
  1603. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1604. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1605. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer"), &SceneTree::set_multiplayer);
  1606. ClassDB::bind_method(D_METHOD("get_multiplayer"), &SceneTree::get_multiplayer);
  1607. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1608. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1609. ClassDB::bind_method(D_METHOD("set_network_peer", "peer"), &SceneTree::set_network_peer);
  1610. ClassDB::bind_method(D_METHOD("get_network_peer"), &SceneTree::get_network_peer);
  1611. ClassDB::bind_method(D_METHOD("is_network_server"), &SceneTree::is_network_server);
  1612. ClassDB::bind_method(D_METHOD("has_network_peer"), &SceneTree::has_network_peer);
  1613. ClassDB::bind_method(D_METHOD("get_network_connected_peers"), &SceneTree::get_network_connected_peers);
  1614. ClassDB::bind_method(D_METHOD("get_network_unique_id"), &SceneTree::get_network_unique_id);
  1615. ClassDB::bind_method(D_METHOD("get_rpc_sender_id"), &SceneTree::get_rpc_sender_id);
  1616. ClassDB::bind_method(D_METHOD("set_refuse_new_network_connections", "refuse"), &SceneTree::set_refuse_new_network_connections);
  1617. ClassDB::bind_method(D_METHOD("is_refusing_new_network_connections"), &SceneTree::is_refusing_new_network_connections);
  1618. ClassDB::bind_method(D_METHOD("_network_peer_connected"), &SceneTree::_network_peer_connected);
  1619. ClassDB::bind_method(D_METHOD("_network_peer_disconnected"), &SceneTree::_network_peer_disconnected);
  1620. ClassDB::bind_method(D_METHOD("_connected_to_server"), &SceneTree::_connected_to_server);
  1621. ClassDB::bind_method(D_METHOD("_connection_failed"), &SceneTree::_connection_failed);
  1622. ClassDB::bind_method(D_METHOD("_server_disconnected"), &SceneTree::_server_disconnected);
  1623. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &SceneTree::set_use_font_oversampling);
  1624. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &SceneTree::is_using_font_oversampling);
  1625. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit");
  1626. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back");
  1627. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1628. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1629. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1630. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections");
  1631. ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false);
  1632. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_font_oversampling"), "set_use_font_oversampling", "is_using_font_oversampling");
  1633. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_edited_scene_root", "get_edited_scene_root");
  1634. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_current_scene", "get_current_scene");
  1635. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer");
  1636. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "", "get_root");
  1637. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_multiplayer", "get_multiplayer");
  1638. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1639. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "physics_interpolation"), "set_physics_interpolation_enabled", "is_physics_interpolation_enabled");
  1640. ADD_SIGNAL(MethodInfo("tree_changed"));
  1641. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1642. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1643. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1644. ADD_SIGNAL(MethodInfo("screen_resized"));
  1645. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1646. ADD_SIGNAL(MethodInfo("idle_frame"));
  1647. ADD_SIGNAL(MethodInfo("physics_frame"));
  1648. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "screen")));
  1649. ADD_SIGNAL(MethodInfo("global_menu_action",
  1650. PropertyInfo(Variant::NIL, "id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT),
  1651. PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1652. ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id")));
  1653. ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id")));
  1654. ADD_SIGNAL(MethodInfo("connected_to_server"));
  1655. ADD_SIGNAL(MethodInfo("connection_failed"));
  1656. ADD_SIGNAL(MethodInfo("server_disconnected"));
  1657. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1658. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1659. BIND_ENUM_CONSTANT(GROUP_CALL_REALTIME);
  1660. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1661. BIND_ENUM_CONSTANT(STRETCH_MODE_DISABLED);
  1662. BIND_ENUM_CONSTANT(STRETCH_MODE_2D);
  1663. BIND_ENUM_CONSTANT(STRETCH_MODE_VIEWPORT);
  1664. BIND_ENUM_CONSTANT(STRETCH_ASPECT_IGNORE);
  1665. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP);
  1666. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_WIDTH);
  1667. BIND_ENUM_CONSTANT(STRETCH_ASPECT_KEEP_HEIGHT);
  1668. BIND_ENUM_CONSTANT(STRETCH_ASPECT_EXPAND);
  1669. }
  1670. SceneTree *SceneTree::singleton = nullptr;
  1671. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1672. int SceneTree::idle_callback_count = 0;
  1673. void SceneTree::_call_idle_callbacks() {
  1674. for (int i = 0; i < idle_callback_count; i++) {
  1675. idle_callbacks[i]();
  1676. }
  1677. }
  1678. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1679. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1680. idle_callbacks[idle_callback_count++] = p_callback;
  1681. }
  1682. void SceneTree::set_use_font_oversampling(bool p_oversampling) {
  1683. if (use_font_oversampling == p_oversampling) {
  1684. return;
  1685. }
  1686. use_font_oversampling = p_oversampling;
  1687. _update_root_rect();
  1688. }
  1689. bool SceneTree::is_using_font_oversampling() const {
  1690. return use_font_oversampling;
  1691. }
  1692. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1693. if (p_function == "change_scene") {
  1694. DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1695. List<String> directories;
  1696. directories.push_back(dir_access->get_current_dir());
  1697. while (!directories.empty()) {
  1698. dir_access->change_dir(directories.back()->get());
  1699. directories.pop_back();
  1700. dir_access->list_dir_begin();
  1701. String filename = dir_access->get_next();
  1702. while (filename != "") {
  1703. if (filename == "." || filename == "..") {
  1704. filename = dir_access->get_next();
  1705. continue;
  1706. }
  1707. if (dir_access->dir_exists(filename)) {
  1708. directories.push_back(dir_access->get_current_dir().plus_file(filename));
  1709. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1710. r_options->push_back("\"" + dir_access->get_current_dir().plus_file(filename) + "\"");
  1711. }
  1712. filename = dir_access->get_next();
  1713. }
  1714. }
  1715. }
  1716. }
  1717. SceneTree::SceneTree() {
  1718. if (singleton == nullptr) {
  1719. singleton = this;
  1720. }
  1721. _quit = false;
  1722. accept_quit = true;
  1723. quit_on_go_back = true;
  1724. initialized = false;
  1725. use_font_oversampling = false;
  1726. #ifdef DEBUG_ENABLED
  1727. debug_collisions_hint = false;
  1728. debug_navigation_hint = false;
  1729. #endif
  1730. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42));
  1731. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1732. debug_navigation_color = GLOBAL_DEF("debug/shapes/navigation/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1733. debug_navigation_disabled_color = GLOBAL_DEF("debug/shapes/navigation/disabled_geometry_color", Color(1.0, 0.7, 0.1, 0.4));
  1734. collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
  1735. 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
  1736. GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  1737. tree_version = 1;
  1738. physics_process_time = 0.f;
  1739. idle_process_time = 0.f;
  1740. root = nullptr;
  1741. input_handled = false;
  1742. pause = false;
  1743. current_frame = 0;
  1744. current_event = 0;
  1745. tree_changed_name = "tree_changed";
  1746. node_added_name = "node_added";
  1747. node_removed_name = "node_removed";
  1748. node_renamed_name = "node_renamed";
  1749. ugc_locked = false;
  1750. call_lock = 0;
  1751. root_lock = 0;
  1752. node_count = 0;
  1753. _physics_interpolation_enabled = false;
  1754. //create with mainloop
  1755. root = memnew(Viewport);
  1756. root->set_name("root");
  1757. root->set_handle_input_locally(false);
  1758. if (!root->get_world().is_valid()) {
  1759. root->set_world(Ref<World>(memnew(World)));
  1760. }
  1761. set_physics_interpolation_enabled(GLOBAL_DEF("physics/common/physics_interpolation", false));
  1762. // Always disable jitter fix if physics interpolation is enabled -
  1763. // Jitter fix will interfere with interpolation, and is not necessary
  1764. // when interpolation is active.
  1765. if (is_physics_interpolation_enabled()) {
  1766. Engine::get_singleton()->set_physics_jitter_fix(0);
  1767. }
  1768. // Initialize network state
  1769. multiplayer_poll = true;
  1770. set_multiplayer(Ref<MultiplayerAPI>(memnew(MultiplayerAPI)));
  1771. root->set_as_audio_listener(true);
  1772. root->set_as_audio_listener_2d(true);
  1773. current_scene = nullptr;
  1774. int ref_atlas_size = GLOBAL_DEF_RST("rendering/quality/reflections/atlas_size", 2048);
  1775. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/reflections/atlas_size", PropertyInfo(Variant::INT, "rendering/quality/reflections/atlas_size", PROPERTY_HINT_RANGE, "0,8192,1,or_greater")); // next_power_of_2 will return a 0 as min value.
  1776. int ref_atlas_subdiv = GLOBAL_DEF_RST("rendering/quality/reflections/atlas_subdiv", 8);
  1777. ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/reflections/atlas_subdiv", PropertyInfo(Variant::INT, "rendering/quality/reflections/atlas_subdiv", PROPERTY_HINT_RANGE, "0,32,1,or_greater")); // next_power_of_2 will return a 0 as min value.
  1778. int msaa_mode = GLOBAL_DEF("rendering/quality/filters/msaa", 0);
  1779. 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"));
  1780. root->set_msaa(Viewport::MSAA(msaa_mode));
  1781. const bool use_fxaa = GLOBAL_DEF("rendering/quality/filters/use_fxaa", false);
  1782. root->set_use_fxaa(use_fxaa);
  1783. const bool use_debanding = GLOBAL_DEF("rendering/quality/filters/use_debanding", false);
  1784. root->set_use_debanding(use_debanding);
  1785. const float sharpen_intensity = GLOBAL_GET("rendering/quality/filters/sharpen_intensity");
  1786. root->set_sharpen_intensity(sharpen_intensity);
  1787. GLOBAL_DEF("rendering/quality/depth/hdr", true);
  1788. GLOBAL_DEF("rendering/quality/depth/hdr.mobile", false);
  1789. const bool hdr = GLOBAL_GET("rendering/quality/depth/hdr");
  1790. root->set_hdr(hdr);
  1791. GLOBAL_DEF("rendering/quality/depth/use_32_bpc_depth", false);
  1792. const bool use_32_bpc_depth = GLOBAL_GET("rendering/quality/depth/use_32_bpc_depth");
  1793. root->set_use_32_bpc_depth(use_32_bpc_depth);
  1794. VS::get_singleton()->scenario_set_reflection_atlas_size(root->get_world()->get_scenario(), ref_atlas_size, ref_atlas_subdiv);
  1795. { //load default fallback environment
  1796. //get possible extensions
  1797. List<String> exts;
  1798. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1799. String ext_hint;
  1800. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  1801. if (ext_hint != String()) {
  1802. ext_hint += ",";
  1803. }
  1804. ext_hint += "*." + E->get();
  1805. }
  1806. //get path
  1807. String env_path = GLOBAL_DEF("rendering/environment/default_environment", "");
  1808. //setup property
  1809. ProjectSettings::get_singleton()->set_custom_property_info("rendering/environment/default_environment", PropertyInfo(Variant::STRING, "rendering/viewport/default_environment", PROPERTY_HINT_FILE, ext_hint));
  1810. env_path = env_path.strip_edges();
  1811. if (env_path != String()) {
  1812. Ref<Environment> env = ResourceLoader::load(env_path);
  1813. if (env.is_valid()) {
  1814. root->get_world()->set_fallback_environment(env);
  1815. } else {
  1816. if (Engine::get_singleton()->is_editor_hint()) {
  1817. //file was erased, clear the field.
  1818. ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
  1819. } else {
  1820. //file was erased, notify user.
  1821. ERR_PRINT(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1822. }
  1823. }
  1824. }
  1825. }
  1826. stretch_mode = STRETCH_MODE_DISABLED;
  1827. stretch_aspect = STRETCH_ASPECT_IGNORE;
  1828. stretch_scale = 1.0;
  1829. last_screen_size = OS::get_singleton()->get_window_size();
  1830. _update_root_rect();
  1831. if (ScriptDebugger::get_singleton()) {
  1832. if (ScriptDebugger::get_singleton()->is_remote()) {
  1833. ScriptDebuggerRemote *remote_debugger = static_cast<ScriptDebuggerRemote *>(ScriptDebugger::get_singleton());
  1834. remote_debugger->set_scene_tree(this);
  1835. }
  1836. ScriptDebugger::get_singleton()->set_multiplayer(multiplayer);
  1837. }
  1838. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1839. #ifdef TOOLS_ENABLED
  1840. edited_scene_root = nullptr;
  1841. #endif
  1842. #ifdef DEBUG_ENABLED
  1843. live_edit_root = NodePath("/root");
  1844. #endif
  1845. }
  1846. SceneTree::~SceneTree() {
  1847. if (root) {
  1848. root->_set_tree(nullptr);
  1849. root->_propagate_after_exit_branch(true);
  1850. memdelete(root);
  1851. }
  1852. if (singleton == this) {
  1853. singleton = nullptr;
  1854. }
  1855. }