scene_tree.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /**************************************************************************/
  2. /* scene_tree.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "scene_tree.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/input/input.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/image_loader.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/io/resource_loader.h"
  38. #include "core/object/message_queue.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/os/os.h"
  41. #include "core/string/print_string.h"
  42. #include "node.h"
  43. #include "scene/animation/tween.h"
  44. #include "scene/debugger/scene_debugger.h"
  45. #include "scene/gui/control.h"
  46. #include "scene/main/multiplayer_api.h"
  47. #include "scene/main/viewport.h"
  48. #include "scene/resources/environment.h"
  49. #include "scene/resources/font.h"
  50. #include "scene/resources/material.h"
  51. #include "scene/resources/mesh.h"
  52. #include "scene/resources/packed_scene.h"
  53. #include "scene/resources/world_2d.h"
  54. #include "scene/resources/world_3d.h"
  55. #include "scene/scene_string_names.h"
  56. #include "servers/display_server.h"
  57. #include "servers/navigation_server_3d.h"
  58. #include "servers/physics_server_2d.h"
  59. #include "servers/physics_server_3d.h"
  60. #include "window.h"
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. void SceneTreeTimer::_bind_methods() {
  64. ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
  65. ClassDB::bind_method(D_METHOD("get_time_left"), &SceneTreeTimer::get_time_left);
  66. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s"), "set_time_left", "get_time_left");
  67. ADD_SIGNAL(MethodInfo("timeout"));
  68. }
  69. void SceneTreeTimer::set_time_left(double p_time) {
  70. time_left = p_time;
  71. }
  72. double SceneTreeTimer::get_time_left() const {
  73. return time_left;
  74. }
  75. void SceneTreeTimer::set_process_always(bool p_process_always) {
  76. process_always = p_process_always;
  77. }
  78. bool SceneTreeTimer::is_process_always() {
  79. return process_always;
  80. }
  81. void SceneTreeTimer::set_process_in_physics(bool p_process_in_physics) {
  82. process_in_physics = p_process_in_physics;
  83. }
  84. bool SceneTreeTimer::is_process_in_physics() {
  85. return process_in_physics;
  86. }
  87. void SceneTreeTimer::set_ignore_time_scale(bool p_ignore) {
  88. ignore_time_scale = p_ignore;
  89. }
  90. bool SceneTreeTimer::is_ignore_time_scale() {
  91. return ignore_time_scale;
  92. }
  93. void SceneTreeTimer::release_connections() {
  94. List<Connection> signal_connections;
  95. get_all_signal_connections(&signal_connections);
  96. for (const Connection &connection : signal_connections) {
  97. disconnect(connection.signal.get_name(), connection.callable);
  98. }
  99. }
  100. SceneTreeTimer::SceneTreeTimer() {}
  101. void SceneTree::tree_changed() {
  102. tree_version++;
  103. emit_signal(tree_changed_name);
  104. }
  105. void SceneTree::node_added(Node *p_node) {
  106. emit_signal(node_added_name, p_node);
  107. }
  108. void SceneTree::node_removed(Node *p_node) {
  109. if (current_scene == p_node) {
  110. current_scene = nullptr;
  111. }
  112. emit_signal(node_removed_name, p_node);
  113. if (call_lock > 0) {
  114. call_skip.insert(p_node);
  115. }
  116. }
  117. void SceneTree::node_renamed(Node *p_node) {
  118. emit_signal(node_renamed_name, p_node);
  119. }
  120. SceneTree::Group *SceneTree::add_to_group(const StringName &p_group, Node *p_node) {
  121. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  122. if (!E) {
  123. E = group_map.insert(p_group, Group());
  124. }
  125. ERR_FAIL_COND_V_MSG(E->value.nodes.has(p_node), &E->value, "Already in group: " + p_group + ".");
  126. E->value.nodes.push_back(p_node);
  127. //E->value.last_tree_version=0;
  128. E->value.changed = true;
  129. return &E->value;
  130. }
  131. void SceneTree::remove_from_group(const StringName &p_group, Node *p_node) {
  132. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  133. ERR_FAIL_COND(!E);
  134. E->value.nodes.erase(p_node);
  135. if (E->value.nodes.is_empty()) {
  136. group_map.remove(E);
  137. }
  138. }
  139. void SceneTree::make_group_changed(const StringName &p_group) {
  140. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  141. if (E) {
  142. E->value.changed = true;
  143. }
  144. }
  145. void SceneTree::flush_transform_notifications() {
  146. SelfList<Node> *n = xform_change_list.first();
  147. while (n) {
  148. Node *node = n->self();
  149. SelfList<Node> *nx = n->next();
  150. xform_change_list.remove(n);
  151. n = nx;
  152. node->notification(NOTIFICATION_TRANSFORM_CHANGED);
  153. }
  154. }
  155. void SceneTree::_flush_ugc() {
  156. ugc_locked = true;
  157. while (unique_group_calls.size()) {
  158. HashMap<UGCall, Vector<Variant>, UGCall>::Iterator E = unique_group_calls.begin();
  159. const Variant **argptrs = (const Variant **)alloca(E->value.size() * sizeof(Variant *));
  160. for (int i = 0; i < E->value.size(); i++) {
  161. argptrs[i] = &E->value[i];
  162. }
  163. call_group_flagsp(GROUP_CALL_DEFAULT, E->key.group, E->key.call, argptrs, E->value.size());
  164. unique_group_calls.remove(E);
  165. }
  166. ugc_locked = false;
  167. }
  168. void SceneTree::_update_group_order(Group &g, bool p_use_priority) {
  169. if (!g.changed) {
  170. return;
  171. }
  172. if (g.nodes.is_empty()) {
  173. return;
  174. }
  175. Node **gr_nodes = g.nodes.ptrw();
  176. int gr_node_count = g.nodes.size();
  177. if (p_use_priority) {
  178. SortArray<Node *, Node::ComparatorWithPriority> node_sort;
  179. node_sort.sort(gr_nodes, gr_node_count);
  180. } else {
  181. SortArray<Node *, Node::Comparator> node_sort;
  182. node_sort.sort(gr_nodes, gr_node_count);
  183. }
  184. g.changed = false;
  185. }
  186. void SceneTree::call_group_flagsp(uint32_t p_call_flags, const StringName &p_group, const StringName &p_function, const Variant **p_args, int p_argcount) {
  187. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  188. if (!E) {
  189. return;
  190. }
  191. Group &g = E->value;
  192. if (g.nodes.is_empty()) {
  193. return;
  194. }
  195. if (p_call_flags & GROUP_CALL_UNIQUE && p_call_flags & GROUP_CALL_DEFERRED) {
  196. ERR_FAIL_COND(ugc_locked);
  197. UGCall ug;
  198. ug.call = p_function;
  199. ug.group = p_group;
  200. if (unique_group_calls.has(ug)) {
  201. return;
  202. }
  203. Vector<Variant> args;
  204. for (int i = 0; i < p_argcount; i++) {
  205. args.push_back(*p_args[i]);
  206. }
  207. unique_group_calls[ug] = args;
  208. return;
  209. }
  210. _update_group_order(g);
  211. Vector<Node *> nodes_copy = g.nodes;
  212. Node **gr_nodes = nodes_copy.ptrw();
  213. int gr_node_count = nodes_copy.size();
  214. call_lock++;
  215. if (p_call_flags & GROUP_CALL_REVERSE) {
  216. for (int i = gr_node_count - 1; i >= 0; i--) {
  217. if (call_lock && call_skip.has(gr_nodes[i])) {
  218. continue;
  219. }
  220. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  221. Callable::CallError ce;
  222. gr_nodes[i]->callp(p_function, p_args, p_argcount, ce);
  223. } else {
  224. MessageQueue::get_singleton()->push_callp(gr_nodes[i], p_function, p_args, p_argcount);
  225. }
  226. }
  227. } else {
  228. for (int i = 0; i < gr_node_count; i++) {
  229. if (call_lock && call_skip.has(gr_nodes[i])) {
  230. continue;
  231. }
  232. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  233. Callable::CallError ce;
  234. gr_nodes[i]->callp(p_function, p_args, p_argcount, ce);
  235. } else {
  236. MessageQueue::get_singleton()->push_callp(gr_nodes[i], p_function, p_args, p_argcount);
  237. }
  238. }
  239. }
  240. call_lock--;
  241. if (call_lock == 0) {
  242. call_skip.clear();
  243. }
  244. }
  245. void SceneTree::notify_group_flags(uint32_t p_call_flags, const StringName &p_group, int p_notification) {
  246. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  247. if (!E) {
  248. return;
  249. }
  250. Group &g = E->value;
  251. if (g.nodes.is_empty()) {
  252. return;
  253. }
  254. _update_group_order(g);
  255. Vector<Node *> nodes_copy = g.nodes;
  256. Node **gr_nodes = nodes_copy.ptrw();
  257. int gr_node_count = nodes_copy.size();
  258. call_lock++;
  259. if (p_call_flags & GROUP_CALL_REVERSE) {
  260. for (int i = gr_node_count - 1; i >= 0; i--) {
  261. if (call_lock && call_skip.has(gr_nodes[i])) {
  262. continue;
  263. }
  264. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  265. gr_nodes[i]->notification(p_notification);
  266. } else {
  267. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  268. }
  269. }
  270. } else {
  271. for (int i = 0; i < gr_node_count; i++) {
  272. if (call_lock && call_skip.has(gr_nodes[i])) {
  273. continue;
  274. }
  275. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  276. gr_nodes[i]->notification(p_notification);
  277. } else {
  278. MessageQueue::get_singleton()->push_notification(gr_nodes[i], p_notification);
  279. }
  280. }
  281. }
  282. call_lock--;
  283. if (call_lock == 0) {
  284. call_skip.clear();
  285. }
  286. }
  287. void SceneTree::set_group_flags(uint32_t p_call_flags, const StringName &p_group, const String &p_name, const Variant &p_value) {
  288. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  289. if (!E) {
  290. return;
  291. }
  292. Group &g = E->value;
  293. if (g.nodes.is_empty()) {
  294. return;
  295. }
  296. _update_group_order(g);
  297. Vector<Node *> nodes_copy = g.nodes;
  298. Node **gr_nodes = nodes_copy.ptrw();
  299. int gr_node_count = nodes_copy.size();
  300. call_lock++;
  301. if (p_call_flags & GROUP_CALL_REVERSE) {
  302. for (int i = gr_node_count - 1; i >= 0; i--) {
  303. if (call_lock && call_skip.has(gr_nodes[i])) {
  304. continue;
  305. }
  306. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  307. gr_nodes[i]->set(p_name, p_value);
  308. } else {
  309. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  310. }
  311. }
  312. } else {
  313. for (int i = 0; i < gr_node_count; i++) {
  314. if (call_lock && call_skip.has(gr_nodes[i])) {
  315. continue;
  316. }
  317. if (!(p_call_flags & GROUP_CALL_DEFERRED)) {
  318. gr_nodes[i]->set(p_name, p_value);
  319. } else {
  320. MessageQueue::get_singleton()->push_set(gr_nodes[i], p_name, p_value);
  321. }
  322. }
  323. }
  324. call_lock--;
  325. if (call_lock == 0) {
  326. call_skip.clear();
  327. }
  328. }
  329. void SceneTree::notify_group(const StringName &p_group, int p_notification) {
  330. notify_group_flags(GROUP_CALL_DEFAULT, p_group, p_notification);
  331. }
  332. void SceneTree::set_group(const StringName &p_group, const String &p_name, const Variant &p_value) {
  333. set_group_flags(GROUP_CALL_DEFAULT, p_group, p_name, p_value);
  334. }
  335. void SceneTree::initialize() {
  336. ERR_FAIL_COND(!root);
  337. initialized = true;
  338. root->_set_tree(this);
  339. MainLoop::initialize();
  340. }
  341. bool SceneTree::physics_process(double p_time) {
  342. root_lock++;
  343. current_frame++;
  344. flush_transform_notifications();
  345. MainLoop::physics_process(p_time);
  346. physics_process_time = p_time;
  347. emit_signal(SNAME("physics_frame"));
  348. _notify_group_pause(SNAME("_physics_process_internal"), Node::NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  349. call_group(SNAME("_picking_viewports"), SNAME("_process_picking"));
  350. _notify_group_pause(SNAME("_physics_process"), Node::NOTIFICATION_PHYSICS_PROCESS);
  351. _flush_ugc();
  352. MessageQueue::get_singleton()->flush(); //small little hack
  353. process_timers(p_time, true); //go through timers
  354. process_tweens(p_time, true);
  355. flush_transform_notifications();
  356. root_lock--;
  357. _flush_delete_queue();
  358. _call_idle_callbacks();
  359. return _quit;
  360. }
  361. bool SceneTree::process(double p_time) {
  362. root_lock++;
  363. MainLoop::process(p_time);
  364. process_time = p_time;
  365. if (multiplayer_poll) {
  366. multiplayer->poll();
  367. for (KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  368. E.value->poll();
  369. }
  370. }
  371. emit_signal(SNAME("process_frame"));
  372. MessageQueue::get_singleton()->flush(); //small little hack
  373. flush_transform_notifications();
  374. _notify_group_pause(SNAME("_process_internal"), Node::NOTIFICATION_INTERNAL_PROCESS);
  375. _notify_group_pause(SNAME("_process"), Node::NOTIFICATION_PROCESS);
  376. _flush_ugc();
  377. MessageQueue::get_singleton()->flush(); //small little hack
  378. flush_transform_notifications(); //transforms after world update, to avoid unnecessary enter/exit notifications
  379. root_lock--;
  380. _flush_delete_queue();
  381. process_timers(p_time, false); //go through timers
  382. process_tweens(p_time, false);
  383. flush_transform_notifications(); //additional transforms after timers update
  384. _call_idle_callbacks();
  385. #ifdef TOOLS_ENABLED
  386. #ifndef _3D_DISABLED
  387. if (Engine::get_singleton()->is_editor_hint()) {
  388. //simple hack to reload fallback environment if it changed from editor
  389. String env_path = GLOBAL_GET(SNAME("rendering/environment/defaults/default_environment"));
  390. env_path = env_path.strip_edges(); //user may have added a space or two
  391. String cpath;
  392. Ref<Environment> fallback = get_root()->get_world_3d()->get_fallback_environment();
  393. if (fallback.is_valid()) {
  394. cpath = fallback->get_path();
  395. }
  396. if (cpath != env_path) {
  397. if (!env_path.is_empty()) {
  398. fallback = ResourceLoader::load(env_path);
  399. if (fallback.is_null()) {
  400. //could not load fallback, set as empty
  401. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  402. }
  403. } else {
  404. fallback.unref();
  405. }
  406. get_root()->get_world_3d()->set_fallback_environment(fallback);
  407. }
  408. }
  409. #endif // _3D_DISABLED
  410. #endif // TOOLS_ENABLED
  411. return _quit;
  412. }
  413. void SceneTree::process_timers(double p_delta, bool p_physics_frame) {
  414. List<Ref<SceneTreeTimer>>::Element *L = timers.back(); //last element
  415. for (List<Ref<SceneTreeTimer>>::Element *E = timers.front(); E;) {
  416. List<Ref<SceneTreeTimer>>::Element *N = E->next();
  417. if ((paused && !E->get()->is_process_always()) || (E->get()->is_process_in_physics() != p_physics_frame)) {
  418. if (E == L) {
  419. break; //break on last, so if new timers were added during list traversal, ignore them.
  420. }
  421. E = N;
  422. continue;
  423. }
  424. double time_left = E->get()->get_time_left();
  425. if (E->get()->is_ignore_time_scale()) {
  426. time_left -= Engine::get_singleton()->get_process_step();
  427. } else {
  428. time_left -= p_delta;
  429. }
  430. E->get()->set_time_left(time_left);
  431. if (time_left <= 0) {
  432. E->get()->emit_signal(SNAME("timeout"));
  433. timers.erase(E);
  434. }
  435. if (E == L) {
  436. break; //break on last, so if new timers were added during list traversal, ignore them.
  437. }
  438. E = N;
  439. }
  440. }
  441. void SceneTree::process_tweens(double p_delta, bool p_physics) {
  442. // This methods works similarly to how SceneTreeTimers are handled.
  443. List<Ref<Tween>>::Element *L = tweens.back();
  444. for (List<Ref<Tween>>::Element *E = tweens.front(); E;) {
  445. List<Ref<Tween>>::Element *N = E->next();
  446. // Don't process if paused or process mode doesn't match.
  447. if (!E->get()->can_process(paused) || (p_physics == (E->get()->get_process_mode() == Tween::TWEEN_PROCESS_IDLE))) {
  448. if (E == L) {
  449. break;
  450. }
  451. E = N;
  452. continue;
  453. }
  454. if (!E->get()->step(p_delta)) {
  455. E->get()->clear();
  456. tweens.erase(E);
  457. }
  458. if (E == L) {
  459. break;
  460. }
  461. E = N;
  462. }
  463. }
  464. void SceneTree::finalize() {
  465. _flush_delete_queue();
  466. _flush_ugc();
  467. initialized = false;
  468. MainLoop::finalize();
  469. if (root) {
  470. root->_set_tree(nullptr);
  471. root->_propagate_after_exit_tree();
  472. memdelete(root); //delete root
  473. root = nullptr;
  474. }
  475. // In case deletion of some objects was queued when destructing the `root`.
  476. // E.g. if `queue_free()` was called for some node outside the tree when handling NOTIFICATION_PREDELETE for some node in the tree.
  477. _flush_delete_queue();
  478. // Cleanup timers.
  479. for (Ref<SceneTreeTimer> &timer : timers) {
  480. timer->release_connections();
  481. }
  482. timers.clear();
  483. // Cleanup tweens.
  484. for (Ref<Tween> &tween : tweens) {
  485. tween->clear();
  486. }
  487. tweens.clear();
  488. }
  489. void SceneTree::quit(int p_exit_code) {
  490. OS::get_singleton()->set_exit_code(p_exit_code);
  491. _quit = true;
  492. }
  493. void SceneTree::_main_window_close() {
  494. if (accept_quit) {
  495. _quit = true;
  496. }
  497. }
  498. void SceneTree::_main_window_go_back() {
  499. if (quit_on_go_back) {
  500. _quit = true;
  501. }
  502. }
  503. void SceneTree::_main_window_focus_in() {
  504. Input *id = Input::get_singleton();
  505. if (id) {
  506. id->ensure_touch_mouse_raised();
  507. }
  508. }
  509. void SceneTree::_notification(int p_notification) {
  510. switch (p_notification) {
  511. case NOTIFICATION_TRANSLATION_CHANGED: {
  512. if (!Engine::get_singleton()->is_editor_hint()) {
  513. get_root()->propagate_notification(p_notification);
  514. }
  515. } break;
  516. case NOTIFICATION_OS_MEMORY_WARNING:
  517. case NOTIFICATION_OS_IME_UPDATE:
  518. case NOTIFICATION_WM_ABOUT:
  519. case NOTIFICATION_CRASH:
  520. case NOTIFICATION_APPLICATION_RESUMED:
  521. case NOTIFICATION_APPLICATION_PAUSED:
  522. case NOTIFICATION_APPLICATION_FOCUS_IN:
  523. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  524. // Pass these to nodes, since they are mirrored.
  525. get_root()->propagate_notification(p_notification);
  526. } break;
  527. }
  528. }
  529. bool SceneTree::is_auto_accept_quit() const {
  530. return accept_quit;
  531. }
  532. void SceneTree::set_auto_accept_quit(bool p_enable) {
  533. accept_quit = p_enable;
  534. }
  535. bool SceneTree::is_quit_on_go_back() const {
  536. return quit_on_go_back;
  537. }
  538. void SceneTree::set_quit_on_go_back(bool p_enable) {
  539. quit_on_go_back = p_enable;
  540. }
  541. #ifdef TOOLS_ENABLED
  542. bool SceneTree::is_node_being_edited(const Node *p_node) const {
  543. return Engine::get_singleton()->is_editor_hint() && edited_scene_root && (edited_scene_root->is_ancestor_of(p_node) || edited_scene_root == p_node);
  544. }
  545. #endif
  546. #ifdef DEBUG_ENABLED
  547. void SceneTree::set_debug_collisions_hint(bool p_enabled) {
  548. debug_collisions_hint = p_enabled;
  549. }
  550. bool SceneTree::is_debugging_collisions_hint() const {
  551. return debug_collisions_hint;
  552. }
  553. void SceneTree::set_debug_paths_hint(bool p_enabled) {
  554. debug_paths_hint = p_enabled;
  555. }
  556. bool SceneTree::is_debugging_paths_hint() const {
  557. return debug_paths_hint;
  558. }
  559. void SceneTree::set_debug_navigation_hint(bool p_enabled) {
  560. debug_navigation_hint = p_enabled;
  561. }
  562. bool SceneTree::is_debugging_navigation_hint() const {
  563. return debug_navigation_hint;
  564. }
  565. #endif
  566. void SceneTree::set_debug_collisions_color(const Color &p_color) {
  567. debug_collisions_color = p_color;
  568. }
  569. Color SceneTree::get_debug_collisions_color() const {
  570. return debug_collisions_color;
  571. }
  572. void SceneTree::set_debug_collision_contact_color(const Color &p_color) {
  573. debug_collision_contact_color = p_color;
  574. }
  575. Color SceneTree::get_debug_collision_contact_color() const {
  576. return debug_collision_contact_color;
  577. }
  578. void SceneTree::set_debug_paths_color(const Color &p_color) {
  579. debug_paths_color = p_color;
  580. }
  581. Color SceneTree::get_debug_paths_color() const {
  582. return debug_paths_color;
  583. }
  584. void SceneTree::set_debug_paths_width(float p_width) {
  585. debug_paths_width = p_width;
  586. }
  587. float SceneTree::get_debug_paths_width() const {
  588. return debug_paths_width;
  589. }
  590. Ref<Material> SceneTree::get_debug_paths_material() {
  591. if (debug_paths_material.is_valid()) {
  592. return debug_paths_material;
  593. }
  594. Ref<StandardMaterial3D> _debug_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  595. _debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  596. _debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  597. _debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  598. _debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  599. _debug_material->set_albedo(get_debug_paths_color());
  600. debug_paths_material = _debug_material;
  601. return debug_paths_material;
  602. }
  603. Ref<Material> SceneTree::get_debug_collision_material() {
  604. if (collision_material.is_valid()) {
  605. return collision_material;
  606. }
  607. Ref<StandardMaterial3D> line_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  608. line_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  609. line_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  610. line_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  611. line_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  612. line_material->set_albedo(get_debug_collisions_color());
  613. collision_material = line_material;
  614. return collision_material;
  615. }
  616. Ref<ArrayMesh> SceneTree::get_debug_contact_mesh() {
  617. if (debug_contact_mesh.is_valid()) {
  618. return debug_contact_mesh;
  619. }
  620. debug_contact_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  621. Ref<StandardMaterial3D> mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  622. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  623. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  624. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  625. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  626. mat->set_albedo(get_debug_collision_contact_color());
  627. Vector3 diamond[6] = {
  628. Vector3(-1, 0, 0),
  629. Vector3(1, 0, 0),
  630. Vector3(0, -1, 0),
  631. Vector3(0, 1, 0),
  632. Vector3(0, 0, -1),
  633. Vector3(0, 0, 1)
  634. };
  635. /* clang-format off */
  636. int diamond_faces[8 * 3] = {
  637. 0, 2, 4,
  638. 0, 3, 4,
  639. 1, 2, 4,
  640. 1, 3, 4,
  641. 0, 2, 5,
  642. 0, 3, 5,
  643. 1, 2, 5,
  644. 1, 3, 5,
  645. };
  646. /* clang-format on */
  647. Vector<int> indices;
  648. for (int i = 0; i < 8 * 3; i++) {
  649. indices.push_back(diamond_faces[i]);
  650. }
  651. Vector<Vector3> vertices;
  652. for (int i = 0; i < 6; i++) {
  653. vertices.push_back(diamond[i] * 0.1);
  654. }
  655. Array arr;
  656. arr.resize(Mesh::ARRAY_MAX);
  657. arr[Mesh::ARRAY_VERTEX] = vertices;
  658. arr[Mesh::ARRAY_INDEX] = indices;
  659. debug_contact_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  660. debug_contact_mesh->surface_set_material(0, mat);
  661. return debug_contact_mesh;
  662. }
  663. void SceneTree::set_pause(bool p_enabled) {
  664. if (p_enabled == paused) {
  665. return;
  666. }
  667. paused = p_enabled;
  668. NavigationServer3D::get_singleton()->set_active(!p_enabled);
  669. PhysicsServer3D::get_singleton()->set_active(!p_enabled);
  670. PhysicsServer2D::get_singleton()->set_active(!p_enabled);
  671. if (get_root()) {
  672. get_root()->_propagate_pause_notification(p_enabled);
  673. }
  674. }
  675. bool SceneTree::is_paused() const {
  676. return paused;
  677. }
  678. void SceneTree::_notify_group_pause(const StringName &p_group, int p_notification) {
  679. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  680. if (!E) {
  681. return;
  682. }
  683. Group &g = E->value;
  684. if (g.nodes.is_empty()) {
  685. return;
  686. }
  687. _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);
  688. //copy, so copy on write happens in case something is removed from process while being called
  689. //performance is not lost because only if something is added/removed the vector is copied.
  690. Vector<Node *> nodes_copy = g.nodes;
  691. int gr_node_count = nodes_copy.size();
  692. Node **gr_nodes = nodes_copy.ptrw();
  693. call_lock++;
  694. for (int i = 0; i < gr_node_count; i++) {
  695. Node *n = gr_nodes[i];
  696. if (call_lock && call_skip.has(n)) {
  697. continue;
  698. }
  699. if (!n->can_process()) {
  700. continue;
  701. }
  702. if (!n->can_process_notification(p_notification)) {
  703. continue;
  704. }
  705. n->notification(p_notification);
  706. //ERR_FAIL_COND(gr_node_count != g.nodes.size());
  707. }
  708. call_lock--;
  709. if (call_lock == 0) {
  710. call_skip.clear();
  711. }
  712. }
  713. void SceneTree::_call_input_pause(const StringName &p_group, CallInputType p_call_type, const Ref<InputEvent> &p_input, Viewport *p_viewport) {
  714. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  715. if (!E) {
  716. return;
  717. }
  718. Group &g = E->value;
  719. if (g.nodes.is_empty()) {
  720. return;
  721. }
  722. _update_group_order(g);
  723. //copy, so copy on write happens in case something is removed from process while being called
  724. //performance is not lost because only if something is added/removed the vector is copied.
  725. Vector<Node *> nodes_copy = g.nodes;
  726. int gr_node_count = nodes_copy.size();
  727. Node **gr_nodes = nodes_copy.ptrw();
  728. call_lock++;
  729. Vector<ObjectID> no_context_node_ids; // Nodes may be deleted due to this shortcut input.
  730. for (int i = gr_node_count - 1; i >= 0; i--) {
  731. if (p_viewport->is_input_handled()) {
  732. break;
  733. }
  734. Node *n = gr_nodes[i];
  735. if (call_lock && call_skip.has(n)) {
  736. continue;
  737. }
  738. if (!n->can_process()) {
  739. continue;
  740. }
  741. switch (p_call_type) {
  742. case CALL_INPUT_TYPE_INPUT:
  743. n->_call_input(p_input);
  744. break;
  745. case CALL_INPUT_TYPE_SHORTCUT_INPUT: {
  746. const Control *c = Object::cast_to<Control>(n);
  747. if (c) {
  748. // If calling shortcut input on a control, ensure it respects the shortcut context.
  749. // Shortcut context (based on focus) only makes sense for controls (UI), so don't need to worry about it for nodes
  750. if (c->get_shortcut_context() == nullptr) {
  751. no_context_node_ids.append(n->get_instance_id());
  752. continue;
  753. }
  754. if (!c->is_focus_owner_in_shortcut_context()) {
  755. continue;
  756. }
  757. }
  758. n->_call_shortcut_input(p_input);
  759. break;
  760. }
  761. case CALL_INPUT_TYPE_UNHANDLED_INPUT:
  762. n->_call_unhandled_input(p_input);
  763. break;
  764. case CALL_INPUT_TYPE_UNHANDLED_KEY_INPUT:
  765. n->_call_unhandled_key_input(p_input);
  766. break;
  767. }
  768. }
  769. for (const ObjectID &id : no_context_node_ids) {
  770. if (p_viewport->is_input_handled()) {
  771. break;
  772. }
  773. Node *n = Object::cast_to<Node>(ObjectDB::get_instance(id));
  774. if (n) {
  775. n->_call_shortcut_input(p_input);
  776. }
  777. }
  778. call_lock--;
  779. if (call_lock == 0) {
  780. call_skip.clear();
  781. }
  782. }
  783. void SceneTree::_call_group_flags(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  784. r_error.error = Callable::CallError::CALL_OK;
  785. ERR_FAIL_COND(p_argcount < 3);
  786. ERR_FAIL_COND(!p_args[0]->is_num());
  787. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  788. ERR_FAIL_COND(p_args[2]->get_type() != Variant::STRING_NAME && p_args[2]->get_type() != Variant::STRING);
  789. int flags = *p_args[0];
  790. StringName group = *p_args[1];
  791. StringName method = *p_args[2];
  792. call_group_flagsp(flags, group, method, p_args + 3, p_argcount - 3);
  793. }
  794. void SceneTree::_call_group(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  795. r_error.error = Callable::CallError::CALL_OK;
  796. ERR_FAIL_COND(p_argcount < 2);
  797. ERR_FAIL_COND(p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING);
  798. ERR_FAIL_COND(p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING);
  799. StringName group = *p_args[0];
  800. StringName method = *p_args[1];
  801. call_group_flagsp(GROUP_CALL_DEFAULT, group, method, p_args + 2, p_argcount - 2);
  802. }
  803. int64_t SceneTree::get_frame() const {
  804. return current_frame;
  805. }
  806. TypedArray<Node> SceneTree::_get_nodes_in_group(const StringName &p_group) {
  807. TypedArray<Node> ret;
  808. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  809. if (!E) {
  810. return ret;
  811. }
  812. _update_group_order(E->value); //update order just in case
  813. int nc = E->value.nodes.size();
  814. if (nc == 0) {
  815. return ret;
  816. }
  817. ret.resize(nc);
  818. Node **ptr = E->value.nodes.ptrw();
  819. for (int i = 0; i < nc; i++) {
  820. ret[i] = ptr[i];
  821. }
  822. return ret;
  823. }
  824. bool SceneTree::has_group(const StringName &p_identifier) const {
  825. return group_map.has(p_identifier);
  826. }
  827. Node *SceneTree::get_first_node_in_group(const StringName &p_group) {
  828. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  829. if (!E) {
  830. return nullptr; // No group.
  831. }
  832. _update_group_order(E->value); // Update order just in case.
  833. if (E->value.nodes.is_empty()) {
  834. return nullptr;
  835. }
  836. return E->value.nodes[0];
  837. }
  838. void SceneTree::get_nodes_in_group(const StringName &p_group, List<Node *> *p_list) {
  839. HashMap<StringName, Group>::Iterator E = group_map.find(p_group);
  840. if (!E) {
  841. return;
  842. }
  843. _update_group_order(E->value); //update order just in case
  844. int nc = E->value.nodes.size();
  845. if (nc == 0) {
  846. return;
  847. }
  848. Node **ptr = E->value.nodes.ptrw();
  849. for (int i = 0; i < nc; i++) {
  850. p_list->push_back(ptr[i]);
  851. }
  852. }
  853. void SceneTree::_flush_delete_queue() {
  854. _THREAD_SAFE_METHOD_
  855. while (delete_queue.size()) {
  856. Object *obj = ObjectDB::get_instance(delete_queue.front()->get());
  857. if (obj) {
  858. memdelete(obj);
  859. }
  860. delete_queue.pop_front();
  861. }
  862. }
  863. void SceneTree::queue_delete(Object *p_object) {
  864. _THREAD_SAFE_METHOD_
  865. ERR_FAIL_NULL(p_object);
  866. p_object->_is_queued_for_deletion = true;
  867. delete_queue.push_back(p_object->get_instance_id());
  868. }
  869. int SceneTree::get_node_count() const {
  870. return node_count;
  871. }
  872. void SceneTree::set_edited_scene_root(Node *p_node) {
  873. #ifdef TOOLS_ENABLED
  874. edited_scene_root = p_node;
  875. #endif
  876. }
  877. Node *SceneTree::get_edited_scene_root() const {
  878. #ifdef TOOLS_ENABLED
  879. return edited_scene_root;
  880. #else
  881. return nullptr;
  882. #endif
  883. }
  884. void SceneTree::set_current_scene(Node *p_scene) {
  885. ERR_FAIL_COND(p_scene && p_scene->get_parent() != root);
  886. current_scene = p_scene;
  887. }
  888. Node *SceneTree::get_current_scene() const {
  889. return current_scene;
  890. }
  891. void SceneTree::_change_scene(Node *p_to) {
  892. if (current_scene) {
  893. memdelete(current_scene);
  894. current_scene = nullptr;
  895. }
  896. // If we're quitting, abort.
  897. if (unlikely(_quit)) {
  898. if (p_to) { // Prevent memory leak.
  899. memdelete(p_to);
  900. }
  901. return;
  902. }
  903. if (p_to) {
  904. current_scene = p_to;
  905. root->add_child(p_to);
  906. root->update_mouse_cursor_shape();
  907. }
  908. }
  909. Error SceneTree::change_scene_to_file(const String &p_path) {
  910. Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
  911. if (new_scene.is_null()) {
  912. return ERR_CANT_OPEN;
  913. }
  914. return change_scene_to_packed(new_scene);
  915. }
  916. Error SceneTree::change_scene_to_packed(const Ref<PackedScene> &p_scene) {
  917. Node *new_scene = nullptr;
  918. if (p_scene.is_valid()) {
  919. new_scene = p_scene->instantiate();
  920. ERR_FAIL_COND_V(!new_scene, ERR_CANT_CREATE);
  921. }
  922. call_deferred(SNAME("_change_scene"), new_scene);
  923. return OK;
  924. }
  925. Error SceneTree::reload_current_scene() {
  926. ERR_FAIL_COND_V(!current_scene, ERR_UNCONFIGURED);
  927. String fname = current_scene->get_scene_file_path();
  928. return change_scene_to_file(fname);
  929. }
  930. void SceneTree::add_current_scene(Node *p_current) {
  931. current_scene = p_current;
  932. root->add_child(p_current);
  933. }
  934. Ref<SceneTreeTimer> SceneTree::create_timer(double p_delay_sec, bool p_process_always, bool p_process_in_physics, bool p_ignore_time_scale) {
  935. Ref<SceneTreeTimer> stt;
  936. stt.instantiate();
  937. stt->set_process_always(p_process_always);
  938. stt->set_time_left(p_delay_sec);
  939. stt->set_process_in_physics(p_process_in_physics);
  940. stt->set_ignore_time_scale(p_ignore_time_scale);
  941. timers.push_back(stt);
  942. return stt;
  943. }
  944. Ref<Tween> SceneTree::create_tween() {
  945. Ref<Tween> tween = memnew(Tween(true));
  946. tweens.push_back(tween);
  947. return tween;
  948. }
  949. TypedArray<Tween> SceneTree::get_processed_tweens() {
  950. TypedArray<Tween> ret;
  951. ret.resize(tweens.size());
  952. int i = 0;
  953. for (const Ref<Tween> &tween : tweens) {
  954. ret[i] = tween;
  955. i++;
  956. }
  957. return ret;
  958. }
  959. Ref<MultiplayerAPI> SceneTree::get_multiplayer(const NodePath &p_for_path) const {
  960. Ref<MultiplayerAPI> out = multiplayer;
  961. for (const KeyValue<NodePath, Ref<MultiplayerAPI>> &E : custom_multiplayers) {
  962. const Vector<StringName> snames = E.key.get_names();
  963. const Vector<StringName> tnames = p_for_path.get_names();
  964. if (tnames.size() < snames.size()) {
  965. continue;
  966. }
  967. const StringName *sptr = snames.ptr();
  968. const StringName *nptr = tnames.ptr();
  969. bool valid = true;
  970. for (int i = 0; i < snames.size(); i++) {
  971. if (sptr[i] != nptr[i]) {
  972. valid = false;
  973. break;
  974. }
  975. }
  976. if (valid) {
  977. out = E.value;
  978. break;
  979. }
  980. }
  981. return out;
  982. }
  983. void SceneTree::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer, const NodePath &p_root_path) {
  984. if (p_root_path.is_empty()) {
  985. ERR_FAIL_COND(!p_multiplayer.is_valid());
  986. if (multiplayer.is_valid()) {
  987. multiplayer->object_configuration_remove(nullptr, NodePath("/" + root->get_name()));
  988. }
  989. multiplayer = p_multiplayer;
  990. multiplayer->object_configuration_add(nullptr, NodePath("/" + root->get_name()));
  991. } else {
  992. if (custom_multiplayers.has(p_root_path)) {
  993. custom_multiplayers[p_root_path]->object_configuration_remove(nullptr, p_root_path);
  994. }
  995. if (p_multiplayer.is_valid()) {
  996. custom_multiplayers[p_root_path] = p_multiplayer;
  997. p_multiplayer->object_configuration_add(nullptr, p_root_path);
  998. }
  999. }
  1000. }
  1001. void SceneTree::set_multiplayer_poll_enabled(bool p_enabled) {
  1002. multiplayer_poll = p_enabled;
  1003. }
  1004. bool SceneTree::is_multiplayer_poll_enabled() const {
  1005. return multiplayer_poll;
  1006. }
  1007. void SceneTree::_bind_methods() {
  1008. ClassDB::bind_method(D_METHOD("get_root"), &SceneTree::get_root);
  1009. ClassDB::bind_method(D_METHOD("has_group", "name"), &SceneTree::has_group);
  1010. ClassDB::bind_method(D_METHOD("is_auto_accept_quit"), &SceneTree::is_auto_accept_quit);
  1011. ClassDB::bind_method(D_METHOD("set_auto_accept_quit", "enabled"), &SceneTree::set_auto_accept_quit);
  1012. ClassDB::bind_method(D_METHOD("is_quit_on_go_back"), &SceneTree::is_quit_on_go_back);
  1013. ClassDB::bind_method(D_METHOD("set_quit_on_go_back", "enabled"), &SceneTree::set_quit_on_go_back);
  1014. ClassDB::bind_method(D_METHOD("set_debug_collisions_hint", "enable"), &SceneTree::set_debug_collisions_hint);
  1015. ClassDB::bind_method(D_METHOD("is_debugging_collisions_hint"), &SceneTree::is_debugging_collisions_hint);
  1016. ClassDB::bind_method(D_METHOD("set_debug_paths_hint", "enable"), &SceneTree::set_debug_paths_hint);
  1017. ClassDB::bind_method(D_METHOD("is_debugging_paths_hint"), &SceneTree::is_debugging_paths_hint);
  1018. ClassDB::bind_method(D_METHOD("set_debug_navigation_hint", "enable"), &SceneTree::set_debug_navigation_hint);
  1019. ClassDB::bind_method(D_METHOD("is_debugging_navigation_hint"), &SceneTree::is_debugging_navigation_hint);
  1020. ClassDB::bind_method(D_METHOD("set_edited_scene_root", "scene"), &SceneTree::set_edited_scene_root);
  1021. ClassDB::bind_method(D_METHOD("get_edited_scene_root"), &SceneTree::get_edited_scene_root);
  1022. ClassDB::bind_method(D_METHOD("set_pause", "enable"), &SceneTree::set_pause);
  1023. ClassDB::bind_method(D_METHOD("is_paused"), &SceneTree::is_paused);
  1024. ClassDB::bind_method(D_METHOD("create_timer", "time_sec", "process_always", "process_in_physics", "ignore_time_scale"), &SceneTree::create_timer, DEFVAL(true), DEFVAL(false), DEFVAL(false));
  1025. ClassDB::bind_method(D_METHOD("create_tween"), &SceneTree::create_tween);
  1026. ClassDB::bind_method(D_METHOD("get_processed_tweens"), &SceneTree::get_processed_tweens);
  1027. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneTree::get_node_count);
  1028. ClassDB::bind_method(D_METHOD("get_frame"), &SceneTree::get_frame);
  1029. ClassDB::bind_method(D_METHOD("quit", "exit_code"), &SceneTree::quit, DEFVAL(EXIT_SUCCESS));
  1030. ClassDB::bind_method(D_METHOD("queue_delete", "obj"), &SceneTree::queue_delete);
  1031. MethodInfo mi;
  1032. mi.name = "call_group_flags";
  1033. mi.arguments.push_back(PropertyInfo(Variant::INT, "flags"));
  1034. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1035. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1036. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group_flags", &SceneTree::_call_group_flags, mi);
  1037. ClassDB::bind_method(D_METHOD("notify_group_flags", "call_flags", "group", "notification"), &SceneTree::notify_group_flags);
  1038. ClassDB::bind_method(D_METHOD("set_group_flags", "call_flags", "group", "property", "value"), &SceneTree::set_group_flags);
  1039. MethodInfo mi2;
  1040. mi2.name = "call_group";
  1041. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "group"));
  1042. mi2.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1043. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_group", &SceneTree::_call_group, mi2);
  1044. ClassDB::bind_method(D_METHOD("notify_group", "group", "notification"), &SceneTree::notify_group);
  1045. ClassDB::bind_method(D_METHOD("set_group", "group", "property", "value"), &SceneTree::set_group);
  1046. ClassDB::bind_method(D_METHOD("get_nodes_in_group", "group"), &SceneTree::_get_nodes_in_group);
  1047. ClassDB::bind_method(D_METHOD("get_first_node_in_group", "group"), &SceneTree::get_first_node_in_group);
  1048. ClassDB::bind_method(D_METHOD("set_current_scene", "child_node"), &SceneTree::set_current_scene);
  1049. ClassDB::bind_method(D_METHOD("get_current_scene"), &SceneTree::get_current_scene);
  1050. ClassDB::bind_method(D_METHOD("change_scene_to_file", "path"), &SceneTree::change_scene_to_file);
  1051. ClassDB::bind_method(D_METHOD("change_scene_to_packed", "packed_scene"), &SceneTree::change_scene_to_packed);
  1052. ClassDB::bind_method(D_METHOD("reload_current_scene"), &SceneTree::reload_current_scene);
  1053. ClassDB::bind_method(D_METHOD("_change_scene"), &SceneTree::_change_scene);
  1054. ClassDB::bind_method(D_METHOD("set_multiplayer", "multiplayer", "root_path"), &SceneTree::set_multiplayer, DEFVAL(NodePath()));
  1055. ClassDB::bind_method(D_METHOD("get_multiplayer", "for_path"), &SceneTree::get_multiplayer, DEFVAL(NodePath()));
  1056. ClassDB::bind_method(D_METHOD("set_multiplayer_poll_enabled", "enabled"), &SceneTree::set_multiplayer_poll_enabled);
  1057. ClassDB::bind_method(D_METHOD("is_multiplayer_poll_enabled"), &SceneTree::is_multiplayer_poll_enabled);
  1058. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_accept_quit"), "set_auto_accept_quit", "is_auto_accept_quit");
  1059. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "quit_on_go_back"), "set_quit_on_go_back", "is_quit_on_go_back");
  1060. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_collisions_hint"), "set_debug_collisions_hint", "is_debugging_collisions_hint");
  1061. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_paths_hint"), "set_debug_paths_hint", "is_debugging_paths_hint");
  1062. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_navigation_hint"), "set_debug_navigation_hint", "is_debugging_navigation_hint");
  1063. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused"), "set_pause", "is_paused");
  1064. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "edited_scene_root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_edited_scene_root", "get_edited_scene_root");
  1065. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "current_scene", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_current_scene", "get_current_scene");
  1066. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "", "get_root");
  1067. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multiplayer_poll"), "set_multiplayer_poll_enabled", "is_multiplayer_poll_enabled");
  1068. ADD_SIGNAL(MethodInfo("tree_changed"));
  1069. ADD_SIGNAL(MethodInfo("tree_process_mode_changed")); //editor only signal, but due to API hash it can't be removed in run-time
  1070. ADD_SIGNAL(MethodInfo("node_added", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1071. ADD_SIGNAL(MethodInfo("node_removed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1072. ADD_SIGNAL(MethodInfo("node_renamed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1073. ADD_SIGNAL(MethodInfo("node_configuration_warning_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1074. ADD_SIGNAL(MethodInfo("process_frame"));
  1075. ADD_SIGNAL(MethodInfo("physics_frame"));
  1076. BIND_ENUM_CONSTANT(GROUP_CALL_DEFAULT);
  1077. BIND_ENUM_CONSTANT(GROUP_CALL_REVERSE);
  1078. BIND_ENUM_CONSTANT(GROUP_CALL_DEFERRED);
  1079. BIND_ENUM_CONSTANT(GROUP_CALL_UNIQUE);
  1080. }
  1081. SceneTree *SceneTree::singleton = nullptr;
  1082. SceneTree::IdleCallback SceneTree::idle_callbacks[SceneTree::MAX_IDLE_CALLBACKS];
  1083. int SceneTree::idle_callback_count = 0;
  1084. void SceneTree::_call_idle_callbacks() {
  1085. for (int i = 0; i < idle_callback_count; i++) {
  1086. idle_callbacks[i]();
  1087. }
  1088. }
  1089. void SceneTree::add_idle_callback(IdleCallback p_callback) {
  1090. ERR_FAIL_COND(idle_callback_count >= MAX_IDLE_CALLBACKS);
  1091. idle_callbacks[idle_callback_count++] = p_callback;
  1092. }
  1093. void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1094. if (p_function == "change_scene_to_file") {
  1095. Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1096. List<String> directories;
  1097. directories.push_back(dir_access->get_current_dir());
  1098. while (!directories.is_empty()) {
  1099. dir_access->change_dir(directories.back()->get());
  1100. directories.pop_back();
  1101. dir_access->list_dir_begin();
  1102. String filename = dir_access->get_next();
  1103. while (!filename.is_empty()) {
  1104. if (filename == "." || filename == "..") {
  1105. filename = dir_access->get_next();
  1106. continue;
  1107. }
  1108. if (dir_access->dir_exists(filename)) {
  1109. directories.push_back(dir_access->get_current_dir().path_join(filename));
  1110. } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) {
  1111. r_options->push_back("\"" + dir_access->get_current_dir().path_join(filename) + "\"");
  1112. }
  1113. filename = dir_access->get_next();
  1114. }
  1115. }
  1116. }
  1117. }
  1118. SceneTree::SceneTree() {
  1119. if (singleton == nullptr) {
  1120. singleton = this;
  1121. }
  1122. debug_collisions_color = GLOBAL_DEF("debug/shapes/collision/shape_color", Color(0.0, 0.6, 0.7, 0.42));
  1123. debug_collision_contact_color = GLOBAL_DEF("debug/shapes/collision/contact_color", Color(1.0, 0.2, 0.1, 0.8));
  1124. debug_paths_color = GLOBAL_DEF("debug/shapes/paths/geometry_color", Color(0.1, 1.0, 0.7, 0.4));
  1125. debug_paths_width = GLOBAL_DEF("debug/shapes/paths/geometry_width", 2.0);
  1126. collision_debug_contacts = GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1"), 10000);
  1127. GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
  1128. Math::randomize();
  1129. // Create with mainloop.
  1130. root = memnew(Window);
  1131. root->set_process_mode(Node::PROCESS_MODE_PAUSABLE);
  1132. root->set_name("root");
  1133. root->set_title(GLOBAL_GET("application/config/name"));
  1134. #ifndef _3D_DISABLED
  1135. if (!root->get_world_3d().is_valid()) {
  1136. root->set_world_3d(Ref<World3D>(memnew(World3D)));
  1137. }
  1138. root->set_as_audio_listener_3d(true);
  1139. #endif // _3D_DISABLED
  1140. // Initialize network state.
  1141. set_multiplayer(MultiplayerAPI::create_default_interface());
  1142. root->set_as_audio_listener_2d(true);
  1143. current_scene = nullptr;
  1144. const int msaa_mode_2d = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_2d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), 0);
  1145. root->set_msaa_2d(Viewport::MSAA(msaa_mode_2d));
  1146. const int msaa_mode_3d = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_3d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), 0);
  1147. root->set_msaa_3d(Viewport::MSAA(msaa_mode_3d));
  1148. const bool transparent_background = GLOBAL_DEF("rendering/viewport/transparent_background", false);
  1149. root->set_transparent_background(transparent_background);
  1150. const int ssaa_mode = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/screen_space_aa", PROPERTY_HINT_ENUM, "Disabled (Fastest),FXAA (Fast)"), 0);
  1151. root->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  1152. const bool use_taa = GLOBAL_DEF_BASIC("rendering/anti_aliasing/quality/use_taa", false);
  1153. root->set_use_taa(use_taa);
  1154. const bool use_debanding = GLOBAL_DEF("rendering/anti_aliasing/quality/use_debanding", false);
  1155. root->set_use_debanding(use_debanding);
  1156. const bool use_occlusion_culling = GLOBAL_DEF("rendering/occlusion_culling/use_occlusion_culling", false);
  1157. root->set_use_occlusion_culling(use_occlusion_culling);
  1158. float mesh_lod_threshold = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "rendering/mesh_lod/lod_change/threshold_pixels", PROPERTY_HINT_RANGE, "0,1024,0.1"), 1.0);
  1159. root->set_mesh_lod_threshold(mesh_lod_threshold);
  1160. bool snap_2d_transforms = GLOBAL_DEF("rendering/2d/snap/snap_2d_transforms_to_pixel", false);
  1161. root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
  1162. bool snap_2d_vertices = GLOBAL_DEF("rendering/2d/snap/snap_2d_vertices_to_pixel", false);
  1163. root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
  1164. // We setup VRS for the main viewport here, in the editor this will have little effect.
  1165. const int vrs_mode = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/vrs/mode", PROPERTY_HINT_ENUM, String::utf8("Disabled,Texture,XR")), 0);
  1166. root->set_vrs_mode(Viewport::VRSMode(vrs_mode));
  1167. const String vrs_texture_path = String(GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/vrs/texture", PROPERTY_HINT_FILE, "*.bmp,*.png,*.tga,*.webp"), String())).strip_edges();
  1168. if (vrs_mode == 1 && !vrs_texture_path.is_empty()) {
  1169. Ref<Image> vrs_image;
  1170. vrs_image.instantiate();
  1171. Error load_err = ImageLoader::load_image(vrs_texture_path, vrs_image);
  1172. if (load_err) {
  1173. ERR_PRINT("Non-existing or invalid VRS texture at '" + vrs_texture_path + "'.");
  1174. } else {
  1175. Ref<ImageTexture> vrs_texture;
  1176. vrs_texture.instantiate();
  1177. vrs_texture->create_from_image(vrs_image);
  1178. root->set_vrs_texture(vrs_texture);
  1179. }
  1180. }
  1181. int shadowmap_size = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_size", PROPERTY_HINT_RANGE, "256,16384"), 4096);
  1182. GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_size.mobile", 2048);
  1183. bool shadowmap_16_bits = GLOBAL_DEF("rendering/lights_and_shadows/positional_shadow/atlas_16_bits", true);
  1184. int atlas_q0 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1185. int atlas_q1 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 2);
  1186. int atlas_q2 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 3);
  1187. int atlas_q3 = GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv", PROPERTY_HINT_ENUM, "Disabled,1 Shadow,4 Shadows,16 Shadows,64 Shadows,256 Shadows,1024 Shadows"), 4);
  1188. root->set_positional_shadow_atlas_size(shadowmap_size);
  1189. root->set_positional_shadow_atlas_16_bits(shadowmap_16_bits);
  1190. root->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0));
  1191. root->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1));
  1192. root->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2));
  1193. root->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3));
  1194. Viewport::SDFOversize sdf_oversize = Viewport::SDFOversize(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/oversize", PROPERTY_HINT_ENUM, "100%,120%,150%,200%"), 1)));
  1195. root->set_sdf_oversize(sdf_oversize);
  1196. Viewport::SDFScale sdf_scale = Viewport::SDFScale(int(GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/2d/sdf/scale", PROPERTY_HINT_ENUM, "100%,50%,25%"), 1)));
  1197. root->set_sdf_scale(sdf_scale);
  1198. #ifndef _3D_DISABLED
  1199. { // Load default fallback environment.
  1200. // Get possible extensions.
  1201. List<String> exts;
  1202. ResourceLoader::get_recognized_extensions_for_type("Environment", &exts);
  1203. String ext_hint;
  1204. for (const String &E : exts) {
  1205. if (!ext_hint.is_empty()) {
  1206. ext_hint += ",";
  1207. }
  1208. ext_hint += "*." + E;
  1209. }
  1210. // Get path.
  1211. String env_path = GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/environment/defaults/default_environment", PROPERTY_HINT_FILE, ext_hint), "");
  1212. // Setup property.
  1213. env_path = env_path.strip_edges();
  1214. if (!env_path.is_empty()) {
  1215. Ref<Environment> env = ResourceLoader::load(env_path);
  1216. if (env.is_valid()) {
  1217. root->get_world_3d()->set_fallback_environment(env);
  1218. } else {
  1219. if (Engine::get_singleton()->is_editor_hint()) {
  1220. // File was erased, clear the field.
  1221. ProjectSettings::get_singleton()->set("rendering/environment/defaults/default_environment", "");
  1222. } else {
  1223. // File was erased, notify user.
  1224. ERR_PRINT(RTR("Default Environment as specified in Project Settings (Rendering -> Environment -> Default Environment) could not be loaded."));
  1225. }
  1226. }
  1227. }
  1228. }
  1229. #endif // _3D_DISABLED
  1230. root->set_physics_object_picking(GLOBAL_DEF("physics/common/enable_object_picking", true));
  1231. root->connect("close_requested", callable_mp(this, &SceneTree::_main_window_close));
  1232. root->connect("go_back_requested", callable_mp(this, &SceneTree::_main_window_go_back));
  1233. root->connect("focus_entered", callable_mp(this, &SceneTree::_main_window_focus_in));
  1234. #ifdef TOOLS_ENABLED
  1235. edited_scene_root = nullptr;
  1236. #endif
  1237. }
  1238. SceneTree::~SceneTree() {
  1239. if (root) {
  1240. root->_set_tree(nullptr);
  1241. root->_propagate_after_exit_tree();
  1242. memdelete(root);
  1243. }
  1244. if (singleton == this) {
  1245. singleton = nullptr;
  1246. }
  1247. }