scene_tree.cpp 68 KB

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