node.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. /*************************************************************************/
  2. /* node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "node.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/multiplayer/multiplayer_api.h"
  35. #include "core/object/message_queue.h"
  36. #include "core/string/print_string.h"
  37. #include "instance_placeholder.h"
  38. #include "scene/animation/tween.h"
  39. #include "scene/debugger/scene_debugger.h"
  40. #include "scene/resources/packed_scene.h"
  41. #include "scene/scene_string_names.h"
  42. #include "viewport.h"
  43. #include <stdint.h>
  44. VARIANT_ENUM_CAST(Node::ProcessMode);
  45. VARIANT_ENUM_CAST(Node::InternalMode);
  46. int Node::orphan_node_count = 0;
  47. void Node::_notification(int p_notification) {
  48. switch (p_notification) {
  49. case NOTIFICATION_PROCESS: {
  50. GDVIRTUAL_CALL(_process, get_process_delta_time());
  51. } break;
  52. case NOTIFICATION_PHYSICS_PROCESS: {
  53. GDVIRTUAL_CALL(_physics_process, get_physics_process_delta_time());
  54. } break;
  55. case NOTIFICATION_ENTER_TREE: {
  56. ERR_FAIL_COND(!get_viewport());
  57. ERR_FAIL_COND(!get_tree());
  58. if (data.process_mode == PROCESS_MODE_INHERIT) {
  59. if (data.parent) {
  60. data.process_owner = data.parent->data.process_owner;
  61. } else {
  62. ERR_PRINT("The root node can't be set to Inherit process mode, reverting to Pausable instead.");
  63. data.process_mode = PROCESS_MODE_PAUSABLE;
  64. data.process_owner = this;
  65. }
  66. } else {
  67. data.process_owner = this;
  68. }
  69. if (data.input) {
  70. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  71. }
  72. if (data.shortcut_input) {
  73. add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  74. }
  75. if (data.unhandled_input) {
  76. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  77. }
  78. if (data.unhandled_key_input) {
  79. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  80. }
  81. get_tree()->node_count++;
  82. orphan_node_count--;
  83. } break;
  84. case NOTIFICATION_EXIT_TREE: {
  85. ERR_FAIL_COND(!get_viewport());
  86. ERR_FAIL_COND(!get_tree());
  87. get_tree()->node_count--;
  88. orphan_node_count++;
  89. if (data.input) {
  90. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  91. }
  92. if (data.shortcut_input) {
  93. remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  94. }
  95. if (data.unhandled_input) {
  96. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  97. }
  98. if (data.unhandled_key_input) {
  99. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  100. }
  101. data.process_owner = nullptr;
  102. if (data.path_cache) {
  103. memdelete(data.path_cache);
  104. data.path_cache = nullptr;
  105. }
  106. } break;
  107. case NOTIFICATION_PATH_RENAMED: {
  108. if (data.path_cache) {
  109. memdelete(data.path_cache);
  110. data.path_cache = nullptr;
  111. }
  112. } break;
  113. case NOTIFICATION_READY: {
  114. if (GDVIRTUAL_IS_OVERRIDDEN(_input)) {
  115. set_process_input(true);
  116. }
  117. if (GDVIRTUAL_IS_OVERRIDDEN(_shortcut_input)) {
  118. set_process_shortcut_input(true);
  119. }
  120. if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_input)) {
  121. set_process_unhandled_input(true);
  122. }
  123. if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_key_input)) {
  124. set_process_unhandled_key_input(true);
  125. }
  126. if (GDVIRTUAL_IS_OVERRIDDEN(_process)) {
  127. set_process(true);
  128. }
  129. if (GDVIRTUAL_IS_OVERRIDDEN(_physics_process)) {
  130. set_physics_process(true);
  131. }
  132. GDVIRTUAL_CALL(_ready);
  133. } break;
  134. case NOTIFICATION_POSTINITIALIZE: {
  135. data.in_constructor = false;
  136. } break;
  137. case NOTIFICATION_PREDELETE: {
  138. if (data.parent) {
  139. data.parent->remove_child(this);
  140. }
  141. // kill children as cleanly as possible
  142. while (data.children.size()) {
  143. Node *child = data.children[data.children.size() - 1]; //begin from the end because its faster and more consistent with creation
  144. memdelete(child);
  145. }
  146. } break;
  147. }
  148. }
  149. void Node::_propagate_ready() {
  150. data.ready_notified = true;
  151. data.blocked++;
  152. for (int i = 0; i < data.children.size(); i++) {
  153. data.children[i]->_propagate_ready();
  154. }
  155. data.blocked--;
  156. notification(NOTIFICATION_POST_ENTER_TREE);
  157. if (data.ready_first) {
  158. data.ready_first = false;
  159. notification(NOTIFICATION_READY);
  160. emit_signal(SceneStringNames::get_singleton()->ready);
  161. }
  162. }
  163. void Node::_propagate_enter_tree() {
  164. // this needs to happen to all children before any enter_tree
  165. if (data.parent) {
  166. data.tree = data.parent->data.tree;
  167. data.depth = data.parent->data.depth + 1;
  168. } else {
  169. data.depth = 1;
  170. }
  171. data.viewport = Object::cast_to<Viewport>(this);
  172. if (!data.viewport && data.parent) {
  173. data.viewport = data.parent->data.viewport;
  174. }
  175. data.inside_tree = true;
  176. for (KeyValue<StringName, GroupData> &E : data.grouped) {
  177. E.value.group = data.tree->add_to_group(E.key, this);
  178. }
  179. notification(NOTIFICATION_ENTER_TREE);
  180. GDVIRTUAL_CALL(_enter_tree);
  181. emit_signal(SceneStringNames::get_singleton()->tree_entered);
  182. data.tree->node_added(this);
  183. if (data.parent) {
  184. Variant c = this;
  185. const Variant *cptr = &c;
  186. data.parent->emit_signalp(SNAME("child_entered_tree"), &cptr, 1);
  187. }
  188. data.blocked++;
  189. //block while adding children
  190. for (int i = 0; i < data.children.size(); i++) {
  191. if (!data.children[i]->is_inside_tree()) { // could have been added in enter_tree
  192. data.children[i]->_propagate_enter_tree();
  193. }
  194. }
  195. data.blocked--;
  196. #ifdef DEBUG_ENABLED
  197. SceneDebugger::add_to_cache(data.scene_file_path, this);
  198. #endif
  199. // enter groups
  200. }
  201. void Node::_propagate_after_exit_tree() {
  202. // Clear owner if it was not part of the pruned branch
  203. if (data.owner) {
  204. bool found = false;
  205. Node *parent = data.parent;
  206. while (parent) {
  207. if (parent == data.owner) {
  208. found = true;
  209. break;
  210. }
  211. parent = parent->data.parent;
  212. }
  213. if (!found) {
  214. if (data.unique_name_in_owner) {
  215. _release_unique_name_in_owner();
  216. }
  217. data.owner->data.owned.erase(data.OW);
  218. data.owner = nullptr;
  219. }
  220. }
  221. data.blocked++;
  222. for (int i = data.children.size() - 1; i >= 0; i--) {
  223. data.children[i]->_propagate_after_exit_tree();
  224. }
  225. data.blocked--;
  226. emit_signal(SceneStringNames::get_singleton()->tree_exited);
  227. }
  228. void Node::_propagate_exit_tree() {
  229. //block while removing children
  230. #ifdef DEBUG_ENABLED
  231. SceneDebugger::remove_from_cache(data.scene_file_path, this);
  232. #endif
  233. data.blocked++;
  234. for (int i = data.children.size() - 1; i >= 0; i--) {
  235. data.children[i]->_propagate_exit_tree();
  236. }
  237. data.blocked--;
  238. GDVIRTUAL_CALL(_exit_tree);
  239. emit_signal(SceneStringNames::get_singleton()->tree_exiting);
  240. notification(NOTIFICATION_EXIT_TREE, true);
  241. if (data.tree) {
  242. data.tree->node_removed(this);
  243. }
  244. if (data.parent) {
  245. Variant c = this;
  246. const Variant *cptr = &c;
  247. data.parent->emit_signalp(SNAME("child_exited_tree"), &cptr, 1);
  248. }
  249. // exit groups
  250. for (KeyValue<StringName, GroupData> &E : data.grouped) {
  251. data.tree->remove_from_group(E.key, this);
  252. E.value.group = nullptr;
  253. }
  254. data.viewport = nullptr;
  255. if (data.tree) {
  256. data.tree->tree_changed();
  257. }
  258. data.inside_tree = false;
  259. data.ready_notified = false;
  260. data.tree = nullptr;
  261. data.depth = -1;
  262. }
  263. void Node::move_child(Node *p_child, int p_pos) {
  264. ERR_FAIL_NULL(p_child);
  265. ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
  266. // We need to check whether node is internal and move it only in the relevant node range.
  267. if (p_child->_is_internal_front()) {
  268. ERR_FAIL_INDEX_MSG(p_pos, data.internal_children_front, vformat("Invalid new child position: %d. Child is internal.", p_pos));
  269. _move_child(p_child, p_pos);
  270. } else if (p_child->_is_internal_back()) {
  271. ERR_FAIL_INDEX_MSG(p_pos, data.internal_children_back, vformat("Invalid new child position: %d. Child is internal.", p_pos));
  272. _move_child(p_child, data.children.size() - data.internal_children_back + p_pos);
  273. } else {
  274. ERR_FAIL_INDEX_MSG(p_pos, data.children.size() + 1 - data.internal_children_front - data.internal_children_back, vformat("Invalid new child position: %d.", p_pos));
  275. _move_child(p_child, p_pos + data.internal_children_front);
  276. }
  277. }
  278. void Node::_move_child(Node *p_child, int p_pos, bool p_ignore_end) {
  279. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, move_child() failed. Consider using call_deferred(\"move_child\") instead (or \"popup\" if this is from a popup).");
  280. // Specifying one place beyond the end
  281. // means the same as moving to the last position
  282. if (!p_ignore_end) { // p_ignore_end is a little hack to make back internal children work properly.
  283. if (p_child->_is_internal_front()) {
  284. if (p_pos == data.internal_children_front) {
  285. p_pos--;
  286. }
  287. } else if (p_child->_is_internal_back()) {
  288. if (p_pos == data.children.size()) {
  289. p_pos--;
  290. }
  291. } else {
  292. if (p_pos == data.children.size() - data.internal_children_back) {
  293. p_pos--;
  294. }
  295. }
  296. }
  297. if (p_child->data.pos == p_pos) {
  298. return; //do nothing
  299. }
  300. int motion_from = MIN(p_pos, p_child->data.pos);
  301. int motion_to = MAX(p_pos, p_child->data.pos);
  302. data.children.remove_at(p_child->data.pos);
  303. data.children.insert(p_pos, p_child);
  304. if (data.tree) {
  305. data.tree->tree_changed();
  306. }
  307. data.blocked++;
  308. //new pos first
  309. for (int i = motion_from; i <= motion_to; i++) {
  310. data.children[i]->data.pos = i;
  311. }
  312. // notification second
  313. move_child_notify(p_child);
  314. for (int i = motion_from; i <= motion_to; i++) {
  315. data.children[i]->notification(NOTIFICATION_MOVED_IN_PARENT);
  316. }
  317. for (const KeyValue<StringName, GroupData> &E : p_child->data.grouped) {
  318. if (E.value.group) {
  319. E.value.group->changed = true;
  320. }
  321. }
  322. data.blocked--;
  323. }
  324. void Node::raise() {
  325. if (!data.parent) {
  326. return;
  327. }
  328. // Internal children move within a different index range.
  329. if (_is_internal_front()) {
  330. data.parent->move_child(this, data.parent->data.internal_children_front - 1);
  331. } else if (_is_internal_back()) {
  332. data.parent->move_child(this, data.parent->data.internal_children_back - 1);
  333. } else {
  334. data.parent->move_child(this, data.parent->get_child_count(false) - 1);
  335. }
  336. }
  337. void Node::add_child_notify(Node *p_child) {
  338. // to be used when not wanted
  339. }
  340. void Node::remove_child_notify(Node *p_child) {
  341. // to be used when not wanted
  342. }
  343. void Node::move_child_notify(Node *p_child) {
  344. // to be used when not wanted
  345. }
  346. void Node::set_physics_process(bool p_process) {
  347. if (data.physics_process == p_process) {
  348. return;
  349. }
  350. data.physics_process = p_process;
  351. if (data.physics_process) {
  352. add_to_group("physics_process", false);
  353. } else {
  354. remove_from_group("physics_process");
  355. }
  356. }
  357. bool Node::is_physics_processing() const {
  358. return data.physics_process;
  359. }
  360. void Node::set_physics_process_internal(bool p_process_internal) {
  361. if (data.physics_process_internal == p_process_internal) {
  362. return;
  363. }
  364. data.physics_process_internal = p_process_internal;
  365. if (data.physics_process_internal) {
  366. add_to_group("physics_process_internal", false);
  367. } else {
  368. remove_from_group("physics_process_internal");
  369. }
  370. }
  371. bool Node::is_physics_processing_internal() const {
  372. return data.physics_process_internal;
  373. }
  374. void Node::set_process_mode(ProcessMode p_mode) {
  375. if (data.process_mode == p_mode) {
  376. return;
  377. }
  378. if (!is_inside_tree()) {
  379. data.process_mode = p_mode;
  380. return;
  381. }
  382. bool prev_can_process = can_process();
  383. bool prev_enabled = _is_enabled();
  384. if (p_mode == PROCESS_MODE_INHERIT) {
  385. if (data.parent) {
  386. data.process_owner = data.parent->data.process_owner;
  387. } else {
  388. ERR_FAIL_MSG("The root node can't be set to Inherit process mode.");
  389. }
  390. } else {
  391. data.process_owner = this;
  392. }
  393. data.process_mode = p_mode;
  394. bool next_can_process = can_process();
  395. bool next_enabled = _is_enabled();
  396. int pause_notification = 0;
  397. if (prev_can_process && !next_can_process) {
  398. pause_notification = NOTIFICATION_PAUSED;
  399. } else if (!prev_can_process && next_can_process) {
  400. pause_notification = NOTIFICATION_UNPAUSED;
  401. }
  402. int enabled_notification = 0;
  403. if (prev_enabled && !next_enabled) {
  404. enabled_notification = NOTIFICATION_DISABLED;
  405. } else if (!prev_enabled && next_enabled) {
  406. enabled_notification = NOTIFICATION_ENABLED;
  407. }
  408. _propagate_process_owner(data.process_owner, pause_notification, enabled_notification);
  409. #ifdef TOOLS_ENABLED
  410. // This is required for the editor to update the visibility of disabled nodes
  411. // It's very expensive during runtime to change, so editor-only
  412. if (Engine::get_singleton()->is_editor_hint()) {
  413. get_tree()->emit_signal(SNAME("tree_process_mode_changed"));
  414. }
  415. #endif
  416. }
  417. void Node::_propagate_pause_notification(bool p_enable) {
  418. bool prev_can_process = _can_process(!p_enable);
  419. bool next_can_process = _can_process(p_enable);
  420. if (prev_can_process && !next_can_process) {
  421. notification(NOTIFICATION_PAUSED);
  422. } else if (!prev_can_process && next_can_process) {
  423. notification(NOTIFICATION_UNPAUSED);
  424. }
  425. for (int i = 0; i < data.children.size(); i++) {
  426. data.children[i]->_propagate_pause_notification(p_enable);
  427. }
  428. }
  429. Node::ProcessMode Node::get_process_mode() const {
  430. return data.process_mode;
  431. }
  432. void Node::_propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification) {
  433. data.process_owner = p_owner;
  434. if (p_pause_notification != 0) {
  435. notification(p_pause_notification);
  436. }
  437. if (p_enabled_notification != 0) {
  438. notification(p_enabled_notification);
  439. }
  440. for (int i = 0; i < data.children.size(); i++) {
  441. Node *c = data.children[i];
  442. if (c->data.process_mode == PROCESS_MODE_INHERIT) {
  443. c->_propagate_process_owner(p_owner, p_pause_notification, p_enabled_notification);
  444. }
  445. }
  446. }
  447. void Node::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
  448. data.multiplayer_authority = p_peer_id;
  449. if (p_recursive) {
  450. for (int i = 0; i < data.children.size(); i++) {
  451. data.children[i]->set_multiplayer_authority(p_peer_id, true);
  452. }
  453. }
  454. }
  455. int Node::get_multiplayer_authority() const {
  456. return data.multiplayer_authority;
  457. }
  458. bool Node::is_multiplayer_authority() const {
  459. ERR_FAIL_COND_V(!is_inside_tree(), false);
  460. return get_multiplayer()->get_unique_id() == data.multiplayer_authority;
  461. }
  462. /***** RPC CONFIG ********/
  463. uint16_t Node::rpc_config(const StringName &p_method, Multiplayer::RPCMode p_rpc_mode, bool p_call_local, Multiplayer::TransferMode p_transfer_mode, int p_channel) {
  464. for (int i = 0; i < data.rpc_methods.size(); i++) {
  465. if (data.rpc_methods[i].name == p_method) {
  466. Multiplayer::RPCConfig &nd = data.rpc_methods.write[i];
  467. nd.rpc_mode = p_rpc_mode;
  468. nd.transfer_mode = p_transfer_mode;
  469. nd.call_local = p_call_local;
  470. nd.channel = p_channel;
  471. return i | (1 << 15);
  472. }
  473. }
  474. // New method
  475. Multiplayer::RPCConfig nd;
  476. nd.name = p_method;
  477. nd.rpc_mode = p_rpc_mode;
  478. nd.transfer_mode = p_transfer_mode;
  479. nd.channel = p_channel;
  480. nd.call_local = p_call_local;
  481. data.rpc_methods.push_back(nd);
  482. return ((uint16_t)data.rpc_methods.size() - 1) | (1 << 15);
  483. }
  484. /***** RPC FUNCTIONS ********/
  485. void Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  486. if (p_argcount < 1) {
  487. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  488. r_error.argument = 1;
  489. return;
  490. }
  491. Variant::Type type = p_args[0]->get_type();
  492. if (type != Variant::STRING_NAME && type != Variant::STRING) {
  493. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  494. r_error.argument = 0;
  495. r_error.expected = Variant::STRING_NAME;
  496. return;
  497. }
  498. StringName method = (*p_args[0]).operator StringName();
  499. rpcp(0, method, &p_args[1], p_argcount - 1);
  500. r_error.error = Callable::CallError::CALL_OK;
  501. }
  502. void Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  503. if (p_argcount < 2) {
  504. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  505. r_error.argument = 2;
  506. return;
  507. }
  508. if (p_args[0]->get_type() != Variant::INT) {
  509. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  510. r_error.argument = 0;
  511. r_error.expected = Variant::INT;
  512. return;
  513. }
  514. Variant::Type type = p_args[1]->get_type();
  515. if (type != Variant::STRING_NAME && type != Variant::STRING) {
  516. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  517. r_error.argument = 1;
  518. r_error.expected = Variant::STRING_NAME;
  519. return;
  520. }
  521. int peer_id = *p_args[0];
  522. StringName method = (*p_args[1]).operator StringName();
  523. rpcp(peer_id, method, &p_args[2], p_argcount - 2);
  524. r_error.error = Callable::CallError::CALL_OK;
  525. }
  526. void Node::rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  527. ERR_FAIL_COND(!is_inside_tree());
  528. get_multiplayer()->rpcp(this, p_peer_id, p_method, p_arg, p_argcount);
  529. }
  530. Ref<MultiplayerAPI> Node::get_multiplayer() const {
  531. if (multiplayer.is_valid()) {
  532. return multiplayer;
  533. }
  534. if (!is_inside_tree()) {
  535. return Ref<MultiplayerAPI>();
  536. }
  537. return get_tree()->get_multiplayer();
  538. }
  539. Ref<MultiplayerAPI> Node::get_custom_multiplayer() const {
  540. return multiplayer;
  541. }
  542. void Node::set_custom_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  543. multiplayer = p_multiplayer;
  544. }
  545. Vector<Multiplayer::RPCConfig> Node::get_node_rpc_methods() const {
  546. return data.rpc_methods;
  547. }
  548. //////////// end of rpc
  549. bool Node::can_process_notification(int p_what) const {
  550. switch (p_what) {
  551. case NOTIFICATION_PHYSICS_PROCESS:
  552. return data.physics_process;
  553. case NOTIFICATION_PROCESS:
  554. return data.process;
  555. case NOTIFICATION_INTERNAL_PROCESS:
  556. return data.process_internal;
  557. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
  558. return data.physics_process_internal;
  559. }
  560. return true;
  561. }
  562. bool Node::can_process() const {
  563. ERR_FAIL_COND_V(!is_inside_tree(), false);
  564. return _can_process(get_tree()->is_paused());
  565. }
  566. bool Node::_can_process(bool p_paused) const {
  567. ProcessMode process_mode;
  568. if (data.process_mode == PROCESS_MODE_INHERIT) {
  569. if (!data.process_owner) {
  570. process_mode = PROCESS_MODE_PAUSABLE;
  571. } else {
  572. process_mode = data.process_owner->data.process_mode;
  573. }
  574. } else {
  575. process_mode = data.process_mode;
  576. }
  577. // The owner can't be set to inherit, must be a bug.
  578. ERR_FAIL_COND_V(process_mode == PROCESS_MODE_INHERIT, false);
  579. if (process_mode == PROCESS_MODE_DISABLED) {
  580. return false;
  581. } else if (process_mode == PROCESS_MODE_ALWAYS) {
  582. return true;
  583. }
  584. if (p_paused) {
  585. return process_mode == PROCESS_MODE_WHEN_PAUSED;
  586. } else {
  587. return process_mode == PROCESS_MODE_PAUSABLE;
  588. }
  589. }
  590. bool Node::_is_enabled() const {
  591. ProcessMode process_mode;
  592. if (data.process_mode == PROCESS_MODE_INHERIT) {
  593. if (!data.process_owner) {
  594. process_mode = PROCESS_MODE_PAUSABLE;
  595. } else {
  596. process_mode = data.process_owner->data.process_mode;
  597. }
  598. } else {
  599. process_mode = data.process_mode;
  600. }
  601. return (process_mode != PROCESS_MODE_DISABLED);
  602. }
  603. bool Node::is_enabled() const {
  604. ERR_FAIL_COND_V(!is_inside_tree(), false);
  605. return _is_enabled();
  606. }
  607. double Node::get_physics_process_delta_time() const {
  608. if (data.tree) {
  609. return data.tree->get_physics_process_time();
  610. } else {
  611. return 0;
  612. }
  613. }
  614. double Node::get_process_delta_time() const {
  615. if (data.tree) {
  616. return data.tree->get_process_time();
  617. } else {
  618. return 0;
  619. }
  620. }
  621. void Node::set_process(bool p_process) {
  622. if (data.process == p_process) {
  623. return;
  624. }
  625. data.process = p_process;
  626. if (data.process) {
  627. add_to_group("process", false);
  628. } else {
  629. remove_from_group("process");
  630. }
  631. }
  632. bool Node::is_processing() const {
  633. return data.process;
  634. }
  635. void Node::set_process_internal(bool p_process_internal) {
  636. if (data.process_internal == p_process_internal) {
  637. return;
  638. }
  639. data.process_internal = p_process_internal;
  640. if (data.process_internal) {
  641. add_to_group("process_internal", false);
  642. } else {
  643. remove_from_group("process_internal");
  644. }
  645. }
  646. bool Node::is_processing_internal() const {
  647. return data.process_internal;
  648. }
  649. void Node::set_process_priority(int p_priority) {
  650. data.process_priority = p_priority;
  651. // Make sure we are in SceneTree.
  652. if (data.tree == nullptr) {
  653. return;
  654. }
  655. if (is_processing()) {
  656. data.tree->make_group_changed("process");
  657. }
  658. if (is_processing_internal()) {
  659. data.tree->make_group_changed("process_internal");
  660. }
  661. if (is_physics_processing()) {
  662. data.tree->make_group_changed("physics_process");
  663. }
  664. if (is_physics_processing_internal()) {
  665. data.tree->make_group_changed("physics_process_internal");
  666. }
  667. }
  668. int Node::get_process_priority() const {
  669. return data.process_priority;
  670. }
  671. void Node::set_process_input(bool p_enable) {
  672. if (p_enable == data.input) {
  673. return;
  674. }
  675. data.input = p_enable;
  676. if (!is_inside_tree()) {
  677. return;
  678. }
  679. if (p_enable) {
  680. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  681. } else {
  682. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  683. }
  684. }
  685. bool Node::is_processing_input() const {
  686. return data.input;
  687. }
  688. void Node::set_process_shortcut_input(bool p_enable) {
  689. if (p_enable == data.shortcut_input) {
  690. return;
  691. }
  692. data.shortcut_input = p_enable;
  693. if (!is_inside_tree()) {
  694. return;
  695. }
  696. if (p_enable) {
  697. add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  698. } else {
  699. remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  700. }
  701. }
  702. bool Node::is_processing_shortcut_input() const {
  703. return data.shortcut_input;
  704. }
  705. void Node::set_process_unhandled_input(bool p_enable) {
  706. if (p_enable == data.unhandled_input) {
  707. return;
  708. }
  709. data.unhandled_input = p_enable;
  710. if (!is_inside_tree()) {
  711. return;
  712. }
  713. if (p_enable) {
  714. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  715. } else {
  716. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  717. }
  718. }
  719. bool Node::is_processing_unhandled_input() const {
  720. return data.unhandled_input;
  721. }
  722. void Node::set_process_unhandled_key_input(bool p_enable) {
  723. if (p_enable == data.unhandled_key_input) {
  724. return;
  725. }
  726. data.unhandled_key_input = p_enable;
  727. if (!is_inside_tree()) {
  728. return;
  729. }
  730. if (p_enable) {
  731. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  732. } else {
  733. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  734. }
  735. }
  736. bool Node::is_processing_unhandled_key_input() const {
  737. return data.unhandled_key_input;
  738. }
  739. StringName Node::get_name() const {
  740. return data.name;
  741. }
  742. void Node::_set_name_nocheck(const StringName &p_name) {
  743. data.name = p_name;
  744. }
  745. void Node::set_name(const String &p_name) {
  746. String name = p_name.validate_node_name();
  747. ERR_FAIL_COND(name.is_empty());
  748. if (data.unique_name_in_owner && data.owner) {
  749. _release_unique_name_in_owner();
  750. }
  751. data.name = name;
  752. if (data.parent) {
  753. data.parent->_validate_child_name(this, true);
  754. }
  755. if (data.unique_name_in_owner && data.owner) {
  756. _acquire_unique_name_in_owner();
  757. }
  758. propagate_notification(NOTIFICATION_PATH_RENAMED);
  759. if (is_inside_tree()) {
  760. emit_signal(SNAME("renamed"));
  761. get_tree()->node_renamed(this);
  762. get_tree()->tree_changed();
  763. }
  764. }
  765. static SafeRefCount node_hrcr_count;
  766. void Node::init_node_hrcr() {
  767. node_hrcr_count.init(1);
  768. }
  769. #ifdef TOOLS_ENABLED
  770. String Node::validate_child_name(Node *p_child) {
  771. StringName name = p_child->data.name;
  772. _generate_serial_child_name(p_child, name);
  773. return name;
  774. }
  775. #endif
  776. void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
  777. /* Make sure the name is unique */
  778. if (p_force_human_readable) {
  779. //this approach to autoset node names is human readable but very slow
  780. StringName name = p_child->data.name;
  781. _generate_serial_child_name(p_child, name);
  782. p_child->data.name = name;
  783. } else {
  784. //this approach to autoset node names is fast but not as readable
  785. //it's the default and reserves the '@' character for unique names.
  786. bool unique = true;
  787. if (p_child->data.name == StringName()) {
  788. //new unique name must be assigned
  789. unique = false;
  790. } else {
  791. //check if exists
  792. Node **children = data.children.ptrw();
  793. int cc = data.children.size();
  794. for (int i = 0; i < cc; i++) {
  795. if (children[i] == p_child) {
  796. continue;
  797. }
  798. if (children[i]->data.name == p_child->data.name) {
  799. unique = false;
  800. break;
  801. }
  802. }
  803. }
  804. if (!unique) {
  805. ERR_FAIL_COND(!node_hrcr_count.ref());
  806. String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
  807. p_child->data.name = name;
  808. }
  809. }
  810. }
  811. // Return s + 1 as if it were an integer
  812. String increase_numeric_string(const String &s) {
  813. String res = s;
  814. bool carry = res.length() > 0;
  815. for (int i = res.length() - 1; i >= 0; i--) {
  816. if (!carry) {
  817. break;
  818. }
  819. char32_t n = s[i];
  820. if (n == '9') { // keep carry as true: 9 + 1
  821. res[i] = '0';
  822. } else {
  823. res[i] = s[i] + 1;
  824. carry = false;
  825. }
  826. }
  827. if (carry) {
  828. res = "1" + res;
  829. }
  830. return res;
  831. }
  832. void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const {
  833. if (name == StringName()) {
  834. //no name and a new name is needed, create one.
  835. name = p_child->get_class();
  836. // Adjust casing according to project setting. The current type name is expected to be in PascalCase.
  837. switch (ProjectSettings::get_singleton()->get("editor/node_naming/name_casing").operator int()) {
  838. case NAME_CASING_PASCAL_CASE:
  839. break;
  840. case NAME_CASING_CAMEL_CASE: {
  841. String n = name;
  842. n[0] = n.to_lower()[0];
  843. name = n;
  844. } break;
  845. case NAME_CASING_SNAKE_CASE:
  846. name = String(name).camelcase_to_underscore(true);
  847. break;
  848. }
  849. }
  850. //quickly test if proposed name exists
  851. int cc = data.children.size(); //children count
  852. const Node *const *children_ptr = data.children.ptr();
  853. {
  854. bool exists = false;
  855. for (int i = 0; i < cc; i++) {
  856. if (children_ptr[i] == p_child) { //exclude self in renaming if it's already a child
  857. continue;
  858. }
  859. if (children_ptr[i]->data.name == name) {
  860. exists = true;
  861. }
  862. }
  863. if (!exists) {
  864. return; //if it does not exist, it does not need validation
  865. }
  866. }
  867. // Extract trailing number
  868. String name_string = name;
  869. String nums;
  870. for (int i = name_string.length() - 1; i >= 0; i--) {
  871. char32_t n = name_string[i];
  872. if (is_digit(n)) {
  873. nums = String::chr(name_string[i]) + nums;
  874. } else {
  875. break;
  876. }
  877. }
  878. String nnsep = _get_name_num_separator();
  879. int name_last_index = name_string.length() - nnsep.length() - nums.length();
  880. // Assign the base name + separator to name if we have numbers preceded by a separator
  881. if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) {
  882. name_string = name_string.substr(0, name_last_index + nnsep.length());
  883. } else {
  884. nums = "";
  885. }
  886. for (;;) {
  887. StringName attempt = name_string + nums;
  888. bool exists = false;
  889. for (int i = 0; i < cc; i++) {
  890. if (children_ptr[i] == p_child) {
  891. continue;
  892. }
  893. if (children_ptr[i]->data.name == attempt) {
  894. exists = true;
  895. }
  896. }
  897. if (!exists) {
  898. name = attempt;
  899. return;
  900. } else {
  901. if (nums.length() == 0) {
  902. // Name was undecorated so skip to 2 for a more natural result
  903. nums = "2";
  904. name_string += nnsep; // Add separator because nums.length() > 0 was false
  905. } else {
  906. nums = increase_numeric_string(nums);
  907. }
  908. }
  909. }
  910. }
  911. void Node::_add_child_nocheck(Node *p_child, const StringName &p_name) {
  912. //add a child node quickly, without name validation
  913. p_child->data.name = p_name;
  914. p_child->data.pos = data.children.size();
  915. data.children.push_back(p_child);
  916. p_child->data.parent = this;
  917. if (data.internal_children_back > 0) {
  918. _move_child(p_child, data.children.size() - data.internal_children_back - 1);
  919. }
  920. p_child->notification(NOTIFICATION_PARENTED);
  921. if (data.tree) {
  922. p_child->_set_tree(data.tree);
  923. }
  924. /* Notify */
  925. //recognize children created in this node constructor
  926. p_child->data.parent_owned = data.in_constructor;
  927. add_child_notify(p_child);
  928. }
  929. void Node::add_child(Node *p_child, bool p_legible_unique_name, InternalMode p_internal) {
  930. ERR_FAIL_NULL(p_child);
  931. ERR_FAIL_COND_MSG(p_child == this, vformat("Can't add child '%s' to itself.", p_child->get_name())); // adding to itself!
  932. ERR_FAIL_COND_MSG(p_child->data.parent, vformat("Can't add child '%s' to '%s', already has a parent '%s'.", p_child->get_name(), get_name(), p_child->data.parent->get_name())); //Fail if node has a parent
  933. #ifdef DEBUG_ENABLED
  934. ERR_FAIL_COND_MSG(p_child->is_ancestor_of(this), vformat("Can't add child '%s' to '%s' as it would result in a cyclic dependency since '%s' is already a parent of '%s'.", p_child->get_name(), get_name(), p_child->get_name(), get_name()));
  935. #endif
  936. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_node() failed. Consider using call_deferred(\"add_child\", child) instead.");
  937. _validate_child_name(p_child, p_legible_unique_name);
  938. _add_child_nocheck(p_child, p_child->data.name);
  939. if (p_internal == INTERNAL_MODE_FRONT) {
  940. _move_child(p_child, data.internal_children_front);
  941. data.internal_children_front++;
  942. } else if (p_internal == INTERNAL_MODE_BACK) {
  943. if (data.internal_children_back > 0) {
  944. _move_child(p_child, data.children.size() - 1, true);
  945. }
  946. data.internal_children_back++;
  947. }
  948. }
  949. void Node::add_sibling(Node *p_sibling, bool p_legible_unique_name) {
  950. ERR_FAIL_NULL(p_sibling);
  951. ERR_FAIL_NULL(data.parent);
  952. ERR_FAIL_COND_MSG(p_sibling == this, vformat("Can't add sibling '%s' to itself.", p_sibling->get_name())); // adding to itself!
  953. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_sibling() failed. Consider using call_deferred(\"add_sibling\", sibling) instead.");
  954. InternalMode internal = INTERNAL_MODE_DISABLED;
  955. if (_is_internal_front()) { // The sibling will have the same internal status.
  956. internal = INTERNAL_MODE_FRONT;
  957. } else if (_is_internal_back()) {
  958. internal = INTERNAL_MODE_BACK;
  959. }
  960. data.parent->add_child(p_sibling, p_legible_unique_name, internal);
  961. data.parent->_move_child(p_sibling, get_index() + 1);
  962. }
  963. void Node::remove_child(Node *p_child) {
  964. ERR_FAIL_NULL(p_child);
  965. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, remove_node() failed. Consider using call_deferred(\"remove_child\", child) instead.");
  966. int child_count = data.children.size();
  967. Node **children = data.children.ptrw();
  968. int idx = -1;
  969. if (p_child->data.pos >= 0 && p_child->data.pos < child_count) {
  970. if (children[p_child->data.pos] == p_child) {
  971. idx = p_child->data.pos;
  972. }
  973. }
  974. if (idx == -1) { //maybe removed while unparenting or something and index was not updated, so just in case the above fails, try this.
  975. for (int i = 0; i < child_count; i++) {
  976. if (children[i] == p_child) {
  977. idx = i;
  978. break;
  979. }
  980. }
  981. }
  982. ERR_FAIL_COND_MSG(idx == -1, vformat("Cannot remove child node '%s' as it is not a child of this node.", p_child->get_name()));
  983. //ERR_FAIL_COND( p_child->data.blocked > 0 );
  984. // If internal child, update the counter.
  985. if (p_child->_is_internal_front()) {
  986. data.internal_children_front--;
  987. } else if (p_child->_is_internal_back()) {
  988. data.internal_children_back--;
  989. }
  990. p_child->_set_tree(nullptr);
  991. //}
  992. remove_child_notify(p_child);
  993. p_child->notification(NOTIFICATION_UNPARENTED);
  994. data.children.remove_at(idx);
  995. //update pointer and size
  996. child_count = data.children.size();
  997. children = data.children.ptrw();
  998. for (int i = idx; i < child_count; i++) {
  999. children[i]->data.pos = i;
  1000. children[i]->notification(NOTIFICATION_MOVED_IN_PARENT);
  1001. }
  1002. p_child->data.parent = nullptr;
  1003. p_child->data.pos = -1;
  1004. if (data.inside_tree) {
  1005. p_child->_propagate_after_exit_tree();
  1006. }
  1007. }
  1008. int Node::get_child_count(bool p_include_internal) const {
  1009. if (p_include_internal) {
  1010. return data.children.size();
  1011. } else {
  1012. return data.children.size() - data.internal_children_front - data.internal_children_back;
  1013. }
  1014. }
  1015. Node *Node::get_child(int p_index, bool p_include_internal) const {
  1016. if (p_include_internal) {
  1017. if (p_index < 0) {
  1018. p_index += data.children.size();
  1019. }
  1020. ERR_FAIL_INDEX_V(p_index, data.children.size(), nullptr);
  1021. return data.children[p_index];
  1022. } else {
  1023. if (p_index < 0) {
  1024. p_index += data.children.size() - data.internal_children_front - data.internal_children_back;
  1025. }
  1026. ERR_FAIL_INDEX_V(p_index, data.children.size() - data.internal_children_front - data.internal_children_back, nullptr);
  1027. p_index += data.internal_children_front;
  1028. return data.children[p_index];
  1029. }
  1030. }
  1031. Node *Node::_get_child_by_name(const StringName &p_name) const {
  1032. int cc = data.children.size();
  1033. Node *const *cd = data.children.ptr();
  1034. for (int i = 0; i < cc; i++) {
  1035. if (cd[i]->data.name == p_name) {
  1036. return cd[i];
  1037. }
  1038. }
  1039. return nullptr;
  1040. }
  1041. Node *Node::get_node_or_null(const NodePath &p_path) const {
  1042. if (p_path.is_empty()) {
  1043. return nullptr;
  1044. }
  1045. ERR_FAIL_COND_V_MSG(!data.inside_tree && p_path.is_absolute(), nullptr, "Can't use get_node() with absolute paths from outside the active scene tree.");
  1046. Node *current = nullptr;
  1047. Node *root = nullptr;
  1048. if (!p_path.is_absolute()) {
  1049. current = const_cast<Node *>(this); //start from this
  1050. } else {
  1051. root = const_cast<Node *>(this);
  1052. while (root->data.parent) {
  1053. root = root->data.parent; //start from root
  1054. }
  1055. }
  1056. for (int i = 0; i < p_path.get_name_count(); i++) {
  1057. StringName name = p_path.get_name(i);
  1058. Node *next = nullptr;
  1059. if (name == SceneStringNames::get_singleton()->dot) { // .
  1060. next = current;
  1061. } else if (name == SceneStringNames::get_singleton()->doubledot) { // ..
  1062. if (current == nullptr || !current->data.parent) {
  1063. return nullptr;
  1064. }
  1065. next = current->data.parent;
  1066. } else if (current == nullptr) {
  1067. if (name == root->get_name()) {
  1068. next = root;
  1069. }
  1070. } else if (name.is_node_unique_name()) {
  1071. if (current->data.owned_unique_nodes.size()) {
  1072. // Has unique nodes in ownership
  1073. Node **unique = current->data.owned_unique_nodes.getptr(name);
  1074. if (!unique) {
  1075. return nullptr;
  1076. }
  1077. next = *unique;
  1078. } else if (current->data.owner) {
  1079. Node **unique = current->data.owner->data.owned_unique_nodes.getptr(name);
  1080. if (!unique) {
  1081. return nullptr;
  1082. }
  1083. next = *unique;
  1084. } else {
  1085. return nullptr;
  1086. }
  1087. } else {
  1088. next = nullptr;
  1089. for (int j = 0; j < current->data.children.size(); j++) {
  1090. Node *child = current->data.children[j];
  1091. if (child->data.name == name) {
  1092. next = child;
  1093. break;
  1094. }
  1095. }
  1096. if (next == nullptr) {
  1097. return nullptr;
  1098. };
  1099. }
  1100. current = next;
  1101. }
  1102. return current;
  1103. }
  1104. Node *Node::get_node(const NodePath &p_path) const {
  1105. Node *node = get_node_or_null(p_path);
  1106. if (unlikely(!node)) {
  1107. if (p_path.is_absolute()) {
  1108. ERR_FAIL_V_MSG(nullptr,
  1109. vformat(R"(Node not found: "%s" (absolute path attempted from "%s").)", p_path, get_path()));
  1110. } else {
  1111. ERR_FAIL_V_MSG(nullptr,
  1112. vformat(R"(Node not found: "%s" (relative to "%s").)", p_path, get_path()));
  1113. }
  1114. }
  1115. return node;
  1116. }
  1117. bool Node::has_node(const NodePath &p_path) const {
  1118. return get_node_or_null(p_path) != nullptr;
  1119. }
  1120. TypedArray<Node> Node::find_nodes(const String &p_mask, const String &p_type, bool p_recursive, bool p_owned) const {
  1121. TypedArray<Node> ret;
  1122. ERR_FAIL_COND_V(p_mask.is_empty() && p_type.is_empty(), ret);
  1123. Node *const *cptr = data.children.ptr();
  1124. int ccount = data.children.size();
  1125. for (int i = 0; i < ccount; i++) {
  1126. if (p_owned && !cptr[i]->data.owner) {
  1127. continue;
  1128. }
  1129. if (!p_mask.is_empty()) {
  1130. if (!cptr[i]->data.name.operator String().match(p_mask)) {
  1131. continue;
  1132. } else if (p_type.is_empty()) {
  1133. ret.append(cptr[i]);
  1134. }
  1135. }
  1136. if (cptr[i]->is_class(p_type)) {
  1137. ret.append(cptr[i]);
  1138. } else if (cptr[i]->get_script_instance()) {
  1139. Ref<Script> script = cptr[i]->get_script_instance()->get_script();
  1140. while (script.is_valid()) {
  1141. if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == script->get_path()) || p_type == script->get_path()) {
  1142. ret.append(cptr[i]);
  1143. break;
  1144. }
  1145. script = script->get_base_script();
  1146. }
  1147. }
  1148. if (p_recursive) {
  1149. ret.append_array(cptr[i]->find_nodes(p_mask, p_type, true, p_owned));
  1150. }
  1151. }
  1152. return ret;
  1153. }
  1154. Node *Node::get_parent() const {
  1155. return data.parent;
  1156. }
  1157. Node *Node::find_parent(const String &p_mask) const {
  1158. Node *p = data.parent;
  1159. while (p) {
  1160. if (p->data.name.operator String().match(p_mask)) {
  1161. return p;
  1162. }
  1163. p = p->data.parent;
  1164. }
  1165. return nullptr;
  1166. }
  1167. bool Node::is_ancestor_of(const Node *p_node) const {
  1168. ERR_FAIL_NULL_V(p_node, false);
  1169. Node *p = p_node->data.parent;
  1170. while (p) {
  1171. if (p == this) {
  1172. return true;
  1173. }
  1174. p = p->data.parent;
  1175. }
  1176. return false;
  1177. }
  1178. bool Node::is_greater_than(const Node *p_node) const {
  1179. ERR_FAIL_NULL_V(p_node, false);
  1180. ERR_FAIL_COND_V(!data.inside_tree, false);
  1181. ERR_FAIL_COND_V(!p_node->data.inside_tree, false);
  1182. ERR_FAIL_COND_V(data.depth < 0, false);
  1183. ERR_FAIL_COND_V(p_node->data.depth < 0, false);
  1184. #ifdef NO_ALLOCA
  1185. Vector<int> this_stack;
  1186. Vector<int> that_stack;
  1187. this_stack.resize(data.depth);
  1188. that_stack.resize(p_node->data.depth);
  1189. #else
  1190. int *this_stack = (int *)alloca(sizeof(int) * data.depth);
  1191. int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
  1192. #endif
  1193. const Node *n = this;
  1194. int idx = data.depth - 1;
  1195. while (n) {
  1196. ERR_FAIL_INDEX_V(idx, data.depth, false);
  1197. this_stack[idx--] = n->data.pos;
  1198. n = n->data.parent;
  1199. }
  1200. ERR_FAIL_COND_V(idx != -1, false);
  1201. n = p_node;
  1202. idx = p_node->data.depth - 1;
  1203. while (n) {
  1204. ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
  1205. that_stack[idx--] = n->data.pos;
  1206. n = n->data.parent;
  1207. }
  1208. ERR_FAIL_COND_V(idx != -1, false);
  1209. idx = 0;
  1210. bool res;
  1211. while (true) {
  1212. // using -2 since out-of-tree or nonroot nodes have -1
  1213. int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
  1214. int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
  1215. if (this_idx > that_idx) {
  1216. res = true;
  1217. break;
  1218. } else if (this_idx < that_idx) {
  1219. res = false;
  1220. break;
  1221. } else if (this_idx == -2) {
  1222. res = false; // equal
  1223. break;
  1224. }
  1225. idx++;
  1226. }
  1227. return res;
  1228. }
  1229. void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
  1230. if (data.owner == p_by) {
  1231. p_owned->push_back(this);
  1232. }
  1233. for (int i = 0; i < get_child_count(); i++) {
  1234. get_child(i)->get_owned_by(p_by, p_owned);
  1235. }
  1236. }
  1237. void Node::_set_owner_nocheck(Node *p_owner) {
  1238. if (data.owner == p_owner) {
  1239. return;
  1240. }
  1241. ERR_FAIL_COND(data.owner);
  1242. data.owner = p_owner;
  1243. data.owner->data.owned.push_back(this);
  1244. data.OW = data.owner->data.owned.back();
  1245. }
  1246. void Node::_release_unique_name_in_owner() {
  1247. ERR_FAIL_NULL(data.owner); // Sanity check.
  1248. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1249. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1250. if (which == nullptr || *which != this) {
  1251. return; // Ignore.
  1252. }
  1253. data.owner->data.owned_unique_nodes.erase(key);
  1254. }
  1255. void Node::_acquire_unique_name_in_owner() {
  1256. ERR_FAIL_NULL(data.owner); // Sanity check.
  1257. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1258. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1259. if (which != nullptr && *which != this) {
  1260. WARN_PRINT(vformat(RTR("Setting node name '%s' to be unique within scene for '%s', but it's already claimed by '%s'. This node is no longer set unique."), get_name(), is_inside_tree() ? get_path() : data.owner->get_path_to(this), is_inside_tree() ? (*which)->get_path() : data.owner->get_path_to(*which)));
  1261. data.unique_name_in_owner = false;
  1262. return;
  1263. }
  1264. data.owner->data.owned_unique_nodes[key] = this;
  1265. }
  1266. void Node::set_unique_name_in_owner(bool p_enabled) {
  1267. if (data.unique_name_in_owner == p_enabled) {
  1268. return;
  1269. }
  1270. if (data.unique_name_in_owner && data.owner != nullptr) {
  1271. _release_unique_name_in_owner();
  1272. }
  1273. data.unique_name_in_owner = p_enabled;
  1274. if (data.unique_name_in_owner && data.owner != nullptr) {
  1275. _acquire_unique_name_in_owner();
  1276. }
  1277. update_configuration_warnings();
  1278. }
  1279. bool Node::is_unique_name_in_owner() const {
  1280. return data.unique_name_in_owner;
  1281. }
  1282. void Node::set_owner(Node *p_owner) {
  1283. if (data.owner) {
  1284. if (data.unique_name_in_owner) {
  1285. _release_unique_name_in_owner();
  1286. }
  1287. data.owner->data.owned.erase(data.OW);
  1288. data.OW = nullptr;
  1289. data.owner = nullptr;
  1290. }
  1291. ERR_FAIL_COND(p_owner == this);
  1292. if (!p_owner) {
  1293. return;
  1294. }
  1295. Node *check = this->get_parent();
  1296. bool owner_valid = false;
  1297. while (check) {
  1298. if (check == p_owner) {
  1299. owner_valid = true;
  1300. break;
  1301. }
  1302. check = check->data.parent;
  1303. }
  1304. ERR_FAIL_COND(!owner_valid);
  1305. _set_owner_nocheck(p_owner);
  1306. if (data.unique_name_in_owner) {
  1307. _acquire_unique_name_in_owner();
  1308. }
  1309. }
  1310. Node *Node::get_owner() const {
  1311. return data.owner;
  1312. }
  1313. Node *Node::find_common_parent_with(const Node *p_node) const {
  1314. if (this == p_node) {
  1315. return const_cast<Node *>(p_node);
  1316. }
  1317. Set<const Node *> visited;
  1318. const Node *n = this;
  1319. while (n) {
  1320. visited.insert(n);
  1321. n = n->data.parent;
  1322. }
  1323. const Node *common_parent = p_node;
  1324. while (common_parent) {
  1325. if (visited.has(common_parent)) {
  1326. break;
  1327. }
  1328. common_parent = common_parent->data.parent;
  1329. }
  1330. if (!common_parent) {
  1331. return nullptr;
  1332. }
  1333. return const_cast<Node *>(common_parent);
  1334. }
  1335. NodePath Node::get_path_to(const Node *p_node) const {
  1336. ERR_FAIL_NULL_V(p_node, NodePath());
  1337. if (this == p_node) {
  1338. return NodePath(".");
  1339. }
  1340. Set<const Node *> visited;
  1341. const Node *n = this;
  1342. while (n) {
  1343. visited.insert(n);
  1344. n = n->data.parent;
  1345. }
  1346. const Node *common_parent = p_node;
  1347. while (common_parent) {
  1348. if (visited.has(common_parent)) {
  1349. break;
  1350. }
  1351. common_parent = common_parent->data.parent;
  1352. }
  1353. ERR_FAIL_COND_V(!common_parent, NodePath()); //nodes not in the same tree
  1354. visited.clear();
  1355. Vector<StringName> path;
  1356. n = p_node;
  1357. while (n != common_parent) {
  1358. path.push_back(n->get_name());
  1359. n = n->data.parent;
  1360. }
  1361. n = this;
  1362. StringName up = String("..");
  1363. while (n != common_parent) {
  1364. path.push_back(up);
  1365. n = n->data.parent;
  1366. }
  1367. path.reverse();
  1368. return NodePath(path, false);
  1369. }
  1370. NodePath Node::get_path() const {
  1371. ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
  1372. if (data.path_cache) {
  1373. return *data.path_cache;
  1374. }
  1375. const Node *n = this;
  1376. Vector<StringName> path;
  1377. while (n) {
  1378. path.push_back(n->get_name());
  1379. n = n->data.parent;
  1380. }
  1381. path.reverse();
  1382. data.path_cache = memnew(NodePath(path, true));
  1383. return *data.path_cache;
  1384. }
  1385. bool Node::is_in_group(const StringName &p_identifier) const {
  1386. return data.grouped.has(p_identifier);
  1387. }
  1388. void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
  1389. ERR_FAIL_COND(!p_identifier.operator String().length());
  1390. if (data.grouped.has(p_identifier)) {
  1391. return;
  1392. }
  1393. GroupData gd;
  1394. if (data.tree) {
  1395. gd.group = data.tree->add_to_group(p_identifier, this);
  1396. } else {
  1397. gd.group = nullptr;
  1398. }
  1399. gd.persistent = p_persistent;
  1400. data.grouped[p_identifier] = gd;
  1401. }
  1402. void Node::remove_from_group(const StringName &p_identifier) {
  1403. ERR_FAIL_COND(!data.grouped.has(p_identifier));
  1404. Map<StringName, GroupData>::Element *E = data.grouped.find(p_identifier);
  1405. ERR_FAIL_COND(!E);
  1406. if (data.tree) {
  1407. data.tree->remove_from_group(E->key(), this);
  1408. }
  1409. data.grouped.erase(E);
  1410. }
  1411. Array Node::_get_groups() const {
  1412. Array groups;
  1413. List<GroupInfo> gi;
  1414. get_groups(&gi);
  1415. for (const GroupInfo &E : gi) {
  1416. groups.push_back(E.name);
  1417. }
  1418. return groups;
  1419. }
  1420. void Node::get_groups(List<GroupInfo> *p_groups) const {
  1421. for (const KeyValue<StringName, GroupData> &E : data.grouped) {
  1422. GroupInfo gi;
  1423. gi.name = E.key;
  1424. gi.persistent = E.value.persistent;
  1425. p_groups->push_back(gi);
  1426. }
  1427. }
  1428. int Node::get_persistent_group_count() const {
  1429. int count = 0;
  1430. for (const KeyValue<StringName, GroupData> &E : data.grouped) {
  1431. if (E.value.persistent) {
  1432. count += 1;
  1433. }
  1434. }
  1435. return count;
  1436. }
  1437. void Node::_print_tree_pretty(const String &prefix, const bool last) {
  1438. String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴");
  1439. print_line(prefix + new_prefix + String(get_name()));
  1440. for (int i = 0; i < data.children.size(); i++) {
  1441. new_prefix = last ? String::utf8(" ") : String::utf8(" ┃ ");
  1442. data.children[i]->_print_tree_pretty(prefix + new_prefix, i == data.children.size() - 1);
  1443. }
  1444. }
  1445. void Node::print_tree_pretty() {
  1446. _print_tree_pretty("", true);
  1447. }
  1448. void Node::print_tree() {
  1449. _print_tree(this);
  1450. }
  1451. void Node::_print_tree(const Node *p_node) {
  1452. print_line(String(p_node->get_path_to(this)));
  1453. for (int i = 0; i < data.children.size(); i++) {
  1454. data.children[i]->_print_tree(p_node);
  1455. }
  1456. }
  1457. void Node::_propagate_reverse_notification(int p_notification) {
  1458. data.blocked++;
  1459. for (int i = data.children.size() - 1; i >= 0; i--) {
  1460. data.children[i]->_propagate_reverse_notification(p_notification);
  1461. }
  1462. notification(p_notification, true);
  1463. data.blocked--;
  1464. }
  1465. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  1466. ERR_FAIL_COND(!is_inside_tree());
  1467. data.blocked++;
  1468. if (!p_reverse) {
  1469. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1470. }
  1471. for (int i = 0; i < data.children.size(); i++) {
  1472. data.children[i]->_propagate_deferred_notification(p_notification, p_reverse);
  1473. }
  1474. if (p_reverse) {
  1475. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1476. }
  1477. data.blocked--;
  1478. }
  1479. void Node::propagate_notification(int p_notification) {
  1480. data.blocked++;
  1481. notification(p_notification);
  1482. for (int i = 0; i < data.children.size(); i++) {
  1483. data.children[i]->propagate_notification(p_notification);
  1484. }
  1485. data.blocked--;
  1486. }
  1487. void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
  1488. data.blocked++;
  1489. if (p_parent_first && has_method(p_method)) {
  1490. callv(p_method, p_args);
  1491. }
  1492. for (int i = 0; i < data.children.size(); i++) {
  1493. data.children[i]->propagate_call(p_method, p_args, p_parent_first);
  1494. }
  1495. if (!p_parent_first && has_method(p_method)) {
  1496. callv(p_method, p_args);
  1497. }
  1498. data.blocked--;
  1499. }
  1500. void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
  1501. if (get_owner() == p_owner) {
  1502. set_owner(p_by_owner);
  1503. }
  1504. data.blocked++;
  1505. for (int i = 0; i < data.children.size(); i++) {
  1506. data.children[i]->_propagate_replace_owner(p_owner, p_by_owner);
  1507. }
  1508. data.blocked--;
  1509. }
  1510. int Node::get_index(bool p_include_internal) const {
  1511. // p_include_internal = false doesn't make sense if the node is internal.
  1512. ERR_FAIL_COND_V_MSG(!p_include_internal && (_is_internal_front() || _is_internal_back()), -1, "Node is internal. Can't get index with 'include_internal' being false.");
  1513. if (data.parent && !p_include_internal) {
  1514. return data.pos - data.parent->data.internal_children_front;
  1515. }
  1516. return data.pos;
  1517. }
  1518. Ref<Tween> Node::create_tween() {
  1519. ERR_FAIL_COND_V_MSG(!data.tree, nullptr, "Can't create Tween when not inside scene tree.");
  1520. Ref<Tween> tween = get_tree()->create_tween();
  1521. tween->bind_node(this);
  1522. return tween;
  1523. }
  1524. void Node::remove_and_skip() {
  1525. ERR_FAIL_COND(!data.parent);
  1526. Node *new_owner = get_owner();
  1527. List<Node *> children;
  1528. while (true) {
  1529. bool clear = true;
  1530. for (int i = 0; i < data.children.size(); i++) {
  1531. Node *c_node = data.children[i];
  1532. if (!c_node->get_owner()) {
  1533. continue;
  1534. }
  1535. remove_child(c_node);
  1536. c_node->_propagate_replace_owner(this, nullptr);
  1537. children.push_back(c_node);
  1538. clear = false;
  1539. break;
  1540. }
  1541. if (clear) {
  1542. break;
  1543. }
  1544. }
  1545. while (!children.is_empty()) {
  1546. Node *c_node = children.front()->get();
  1547. data.parent->add_child(c_node);
  1548. c_node->_propagate_replace_owner(nullptr, new_owner);
  1549. children.pop_front();
  1550. }
  1551. data.parent->remove_child(this);
  1552. }
  1553. void Node::set_scene_file_path(const String &p_scene_file_path) {
  1554. data.scene_file_path = p_scene_file_path;
  1555. }
  1556. String Node::get_scene_file_path() const {
  1557. return data.scene_file_path;
  1558. }
  1559. void Node::set_editor_description(const String &p_editor_description) {
  1560. data.editor_description = p_editor_description;
  1561. }
  1562. String Node::get_editor_description() const {
  1563. return data.editor_description;
  1564. }
  1565. void Node::set_editable_instance(Node *p_node, bool p_editable) {
  1566. ERR_FAIL_NULL(p_node);
  1567. ERR_FAIL_COND(!is_ancestor_of(p_node));
  1568. if (!p_editable) {
  1569. p_node->data.editable_instance = false;
  1570. // Avoid this flag being needlessly saved;
  1571. // also give more visual feedback if editable children are re-enabled
  1572. set_display_folded(false);
  1573. } else {
  1574. p_node->data.editable_instance = true;
  1575. }
  1576. }
  1577. bool Node::is_editable_instance(const Node *p_node) const {
  1578. if (!p_node) {
  1579. return false; // Easier, null is never editable. :)
  1580. }
  1581. ERR_FAIL_COND_V(!is_ancestor_of(p_node), false);
  1582. return p_node->data.editable_instance;
  1583. }
  1584. Node *Node::get_deepest_editable_node(Node *p_start_node) const {
  1585. ERR_FAIL_NULL_V(p_start_node, nullptr);
  1586. ERR_FAIL_COND_V(!is_ancestor_of(p_start_node), p_start_node);
  1587. Node const *iterated_item = p_start_node;
  1588. Node *node = p_start_node;
  1589. while (iterated_item->get_owner() && iterated_item->get_owner() != this) {
  1590. if (!is_editable_instance(iterated_item->get_owner())) {
  1591. node = iterated_item->get_owner();
  1592. }
  1593. iterated_item = iterated_item->get_owner();
  1594. }
  1595. return node;
  1596. }
  1597. #ifdef TOOLS_ENABLED
  1598. void Node::set_property_pinned(const String &p_property, bool p_pinned) {
  1599. bool current_pinned = false;
  1600. bool has_pinned = has_meta("_edit_pinned_properties_");
  1601. Array pinned;
  1602. String psa = get_property_store_alias(p_property);
  1603. if (has_pinned) {
  1604. pinned = get_meta("_edit_pinned_properties_");
  1605. current_pinned = pinned.has(psa);
  1606. }
  1607. if (current_pinned != p_pinned) {
  1608. if (p_pinned) {
  1609. pinned.append(psa);
  1610. if (!has_pinned) {
  1611. set_meta("_edit_pinned_properties_", pinned);
  1612. }
  1613. } else {
  1614. pinned.erase(psa);
  1615. if (pinned.is_empty()) {
  1616. remove_meta("_edit_pinned_properties_");
  1617. }
  1618. }
  1619. }
  1620. }
  1621. bool Node::is_property_pinned(const StringName &p_property) const {
  1622. if (!has_meta("_edit_pinned_properties_")) {
  1623. return false;
  1624. }
  1625. Array pinned = get_meta("_edit_pinned_properties_");
  1626. String psa = get_property_store_alias(p_property);
  1627. return pinned.has(psa);
  1628. }
  1629. StringName Node::get_property_store_alias(const StringName &p_property) const {
  1630. return p_property;
  1631. }
  1632. #endif
  1633. void Node::get_storable_properties(Set<StringName> &r_storable_properties) const {
  1634. List<PropertyInfo> pi;
  1635. get_property_list(&pi);
  1636. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  1637. if ((E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1638. r_storable_properties.insert(E->get().name);
  1639. }
  1640. }
  1641. }
  1642. String Node::to_string() {
  1643. if (get_script_instance()) {
  1644. bool valid;
  1645. String ret = get_script_instance()->to_string(&valid);
  1646. if (valid) {
  1647. return ret;
  1648. }
  1649. }
  1650. return (get_name() ? String(get_name()) + ":" : "") + Object::to_string();
  1651. }
  1652. void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
  1653. data.instance_state = p_state;
  1654. }
  1655. Ref<SceneState> Node::get_scene_instance_state() const {
  1656. return data.instance_state;
  1657. }
  1658. void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
  1659. data.inherited_state = p_state;
  1660. }
  1661. Ref<SceneState> Node::get_scene_inherited_state() const {
  1662. return data.inherited_state;
  1663. }
  1664. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  1665. data.use_placeholder = p_enable;
  1666. }
  1667. bool Node::get_scene_instance_load_placeholder() const {
  1668. return data.use_placeholder;
  1669. }
  1670. Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const {
  1671. Node *node = nullptr;
  1672. bool instantiated = false;
  1673. if (Object::cast_to<InstancePlaceholder>(this)) {
  1674. const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
  1675. InstancePlaceholder *nip = memnew(InstancePlaceholder);
  1676. nip->set_instance_path(ip->get_instance_path());
  1677. node = nip;
  1678. } else if ((p_flags & DUPLICATE_USE_INSTANCING) && !get_scene_file_path().is_empty()) {
  1679. Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
  1680. ERR_FAIL_COND_V(res.is_null(), nullptr);
  1681. PackedScene::GenEditState ges = PackedScene::GEN_EDIT_STATE_DISABLED;
  1682. #ifdef TOOLS_ENABLED
  1683. if (p_flags & DUPLICATE_FROM_EDITOR) {
  1684. ges = PackedScene::GEN_EDIT_STATE_INSTANCE;
  1685. }
  1686. #endif
  1687. node = res->instantiate(ges);
  1688. ERR_FAIL_COND_V(!node, nullptr);
  1689. node->set_scene_instance_load_placeholder(get_scene_instance_load_placeholder());
  1690. instantiated = true;
  1691. } else {
  1692. Object *obj = ClassDB::instantiate(get_class());
  1693. ERR_FAIL_COND_V(!obj, nullptr);
  1694. node = Object::cast_to<Node>(obj);
  1695. if (!node) {
  1696. memdelete(obj);
  1697. }
  1698. ERR_FAIL_COND_V(!node, nullptr);
  1699. }
  1700. if (!get_scene_file_path().is_empty()) { //an instance
  1701. node->set_scene_file_path(get_scene_file_path());
  1702. node->data.editable_instance = data.editable_instance;
  1703. }
  1704. StringName script_property_name = CoreStringNames::get_singleton()->_script;
  1705. List<const Node *> hidden_roots;
  1706. List<const Node *> node_tree;
  1707. node_tree.push_front(this);
  1708. if (instantiated) {
  1709. // Since nodes in the instantiated hierarchy won't be duplicated explicitly, we need to make an inventory
  1710. // of all the nodes in the tree of the instantiated scene in order to transfer the values of the properties
  1711. Vector<const Node *> instance_roots;
  1712. instance_roots.push_back(this);
  1713. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1714. for (int i = 0; i < N->get()->get_child_count(); ++i) {
  1715. Node *descendant = N->get()->get_child(i);
  1716. // Skip nodes not really belonging to the instantiated hierarchy; they'll be processed normally later
  1717. // but remember non-instantiated nodes that are hidden below instantiated ones
  1718. if (!instance_roots.has(descendant->get_owner())) {
  1719. if (descendant->get_parent() && descendant->get_parent() != this && descendant->data.owner != descendant->get_parent()) {
  1720. hidden_roots.push_back(descendant);
  1721. }
  1722. continue;
  1723. }
  1724. node_tree.push_back(descendant);
  1725. if (!descendant->get_scene_file_path().is_empty() && instance_roots.has(descendant->get_owner())) {
  1726. instance_roots.push_back(descendant);
  1727. }
  1728. }
  1729. }
  1730. }
  1731. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1732. Node *current_node = node->get_node(get_path_to(N->get()));
  1733. ERR_CONTINUE(!current_node);
  1734. if (p_flags & DUPLICATE_SCRIPTS) {
  1735. bool is_valid = false;
  1736. Variant script = N->get()->get(script_property_name, &is_valid);
  1737. if (is_valid) {
  1738. current_node->set(script_property_name, script);
  1739. }
  1740. }
  1741. List<PropertyInfo> plist;
  1742. N->get()->get_property_list(&plist);
  1743. for (const PropertyInfo &E : plist) {
  1744. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1745. continue;
  1746. }
  1747. String name = E.name;
  1748. if (name == script_property_name) {
  1749. continue;
  1750. }
  1751. Variant value = N->get()->get(name).duplicate(true);
  1752. if (E.usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE) {
  1753. Resource *res = Object::cast_to<Resource>(value);
  1754. if (res) { // Duplicate only if it's a resource
  1755. current_node->set(name, res->duplicate());
  1756. }
  1757. } else {
  1758. current_node->set(name, value);
  1759. }
  1760. }
  1761. }
  1762. if (get_name() != String()) {
  1763. node->set_name(get_name());
  1764. }
  1765. #ifdef TOOLS_ENABLED
  1766. if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
  1767. r_duplimap->insert(this, node);
  1768. }
  1769. #endif
  1770. if (p_flags & DUPLICATE_GROUPS) {
  1771. List<GroupInfo> gi;
  1772. get_groups(&gi);
  1773. for (const GroupInfo &E : gi) {
  1774. #ifdef TOOLS_ENABLED
  1775. if ((p_flags & DUPLICATE_FROM_EDITOR) && !E.persistent) {
  1776. continue;
  1777. }
  1778. #endif
  1779. node->add_to_group(E.name, E.persistent);
  1780. }
  1781. }
  1782. for (int i = 0; i < get_child_count(); i++) {
  1783. if (get_child(i)->data.parent_owned) {
  1784. continue;
  1785. }
  1786. if (instantiated && get_child(i)->data.owner == this) {
  1787. continue; //part of instance
  1788. }
  1789. Node *dup = get_child(i)->_duplicate(p_flags, r_duplimap);
  1790. if (!dup) {
  1791. memdelete(node);
  1792. return nullptr;
  1793. }
  1794. node->add_child(dup);
  1795. if (i < node->get_child_count() - 1) {
  1796. node->move_child(dup, i);
  1797. }
  1798. }
  1799. for (const Node *&E : hidden_roots) {
  1800. Node *parent = node->get_node(get_path_to(E->data.parent));
  1801. if (!parent) {
  1802. memdelete(node);
  1803. return nullptr;
  1804. }
  1805. Node *dup = E->_duplicate(p_flags, r_duplimap);
  1806. if (!dup) {
  1807. memdelete(node);
  1808. return nullptr;
  1809. }
  1810. parent->add_child(dup);
  1811. int pos = E->get_index();
  1812. if (pos < parent->get_child_count() - 1) {
  1813. parent->move_child(dup, pos);
  1814. }
  1815. }
  1816. return node;
  1817. }
  1818. Node *Node::duplicate(int p_flags) const {
  1819. Node *dupe = _duplicate(p_flags);
  1820. if (dupe && (p_flags & DUPLICATE_SIGNALS)) {
  1821. _duplicate_signals(this, dupe);
  1822. }
  1823. return dupe;
  1824. }
  1825. #ifdef TOOLS_ENABLED
  1826. Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const {
  1827. return duplicate_from_editor(r_duplimap, Map<RES, RES>());
  1828. }
  1829. Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap, const Map<RES, RES> &p_resource_remap) const {
  1830. Node *dupe = _duplicate(DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANCING | DUPLICATE_FROM_EDITOR, &r_duplimap);
  1831. // This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
  1832. if (!p_resource_remap.is_empty()) {
  1833. remap_node_resources(dupe, p_resource_remap);
  1834. }
  1835. // Duplication of signals must happen after all the node descendants have been copied,
  1836. // because re-targeting of connections from some descendant to another is not possible
  1837. // if the emitter node comes later in tree order than the receiver
  1838. _duplicate_signals(this, dupe);
  1839. return dupe;
  1840. }
  1841. void Node::remap_node_resources(Node *p_node, const Map<RES, RES> &p_resource_remap) const {
  1842. List<PropertyInfo> props;
  1843. p_node->get_property_list(&props);
  1844. for (const PropertyInfo &E : props) {
  1845. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1846. continue;
  1847. }
  1848. Variant v = p_node->get(E.name);
  1849. if (v.is_ref_counted()) {
  1850. RES res = v;
  1851. if (res.is_valid()) {
  1852. if (p_resource_remap.has(res)) {
  1853. p_node->set(E.name, p_resource_remap[res]);
  1854. remap_nested_resources(res, p_resource_remap);
  1855. }
  1856. }
  1857. }
  1858. }
  1859. for (int i = 0; i < p_node->get_child_count(); i++) {
  1860. remap_node_resources(p_node->get_child(i), p_resource_remap);
  1861. }
  1862. }
  1863. void Node::remap_nested_resources(RES p_resource, const Map<RES, RES> &p_resource_remap) const {
  1864. List<PropertyInfo> props;
  1865. p_resource->get_property_list(&props);
  1866. for (const PropertyInfo &E : props) {
  1867. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1868. continue;
  1869. }
  1870. Variant v = p_resource->get(E.name);
  1871. if (v.is_ref_counted()) {
  1872. RES res = v;
  1873. if (res.is_valid()) {
  1874. if (p_resource_remap.has(res)) {
  1875. p_resource->set(E.name, p_resource_remap[res]);
  1876. remap_nested_resources(res, p_resource_remap);
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. #endif
  1883. // Duplication of signals must happen after all the node descendants have been copied,
  1884. // because re-targeting of connections from some descendant to another is not possible
  1885. // if the emitter node comes later in tree order than the receiver
  1886. void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
  1887. if ((this != p_original) && !(p_original->is_ancestor_of(this))) {
  1888. return;
  1889. }
  1890. List<const Node *> process_list;
  1891. process_list.push_back(this);
  1892. while (!process_list.is_empty()) {
  1893. const Node *n = process_list.front()->get();
  1894. process_list.pop_front();
  1895. List<Connection> conns;
  1896. n->get_all_signal_connections(&conns);
  1897. for (const Connection &E : conns) {
  1898. if (E.flags & CONNECT_PERSIST) {
  1899. //user connected
  1900. NodePath p = p_original->get_path_to(n);
  1901. Node *copy = p_copy->get_node(p);
  1902. Node *target = Object::cast_to<Node>(E.callable.get_object());
  1903. if (!target) {
  1904. continue;
  1905. }
  1906. NodePath ptarget = p_original->get_path_to(target);
  1907. Node *copytarget = target;
  1908. // Attempt to find a path to the duplicate target, if it seems it's not part
  1909. // of the duplicated and not yet parented hierarchy then at least try to connect
  1910. // to the same target as the original
  1911. if (p_copy->has_node(ptarget)) {
  1912. copytarget = p_copy->get_node(ptarget);
  1913. }
  1914. if (copy && copytarget) {
  1915. const Callable copy_callable = Callable(copytarget, E.callable.get_method());
  1916. if (!copy->is_connected(E.signal.get_name(), copy_callable)) {
  1917. copy->connect(E.signal.get_name(), copy_callable, E.binds, E.flags);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. for (int i = 0; i < n->get_child_count(); i++) {
  1923. process_list.push_back(n->get_child(i));
  1924. }
  1925. }
  1926. }
  1927. static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
  1928. if (p_node->get_owner() == p_by) {
  1929. p_owned->push_back(p_node);
  1930. }
  1931. for (int i = 0; i < p_node->get_child_count(); i++) {
  1932. find_owned_by(p_by, p_node->get_child(i), p_owned);
  1933. }
  1934. }
  1935. void Node::replace_by(Node *p_node, bool p_keep_groups) {
  1936. ERR_FAIL_NULL(p_node);
  1937. ERR_FAIL_COND(p_node->data.parent);
  1938. List<Node *> owned = data.owned;
  1939. List<Node *> owned_by_owner;
  1940. Node *owner = (data.owner == this) ? p_node : data.owner;
  1941. if (p_keep_groups) {
  1942. List<GroupInfo> groups;
  1943. get_groups(&groups);
  1944. for (const GroupInfo &E : groups) {
  1945. p_node->add_to_group(E.name, E.persistent);
  1946. }
  1947. }
  1948. _replace_connections_target(p_node);
  1949. if (data.owner) {
  1950. for (int i = 0; i < get_child_count(); i++) {
  1951. find_owned_by(data.owner, get_child(i), &owned_by_owner);
  1952. }
  1953. }
  1954. Node *parent = data.parent;
  1955. int pos_in_parent = data.pos;
  1956. if (data.parent) {
  1957. parent->remove_child(this);
  1958. parent->add_child(p_node);
  1959. parent->move_child(p_node, pos_in_parent);
  1960. }
  1961. while (get_child_count()) {
  1962. Node *child = get_child(0);
  1963. remove_child(child);
  1964. if (!child->is_owned_by_parent()) {
  1965. // add the custom children to the p_node
  1966. p_node->add_child(child);
  1967. }
  1968. }
  1969. p_node->set_owner(owner);
  1970. for (int i = 0; i < owned.size(); i++) {
  1971. owned[i]->set_owner(p_node);
  1972. }
  1973. for (int i = 0; i < owned_by_owner.size(); i++) {
  1974. owned_by_owner[i]->set_owner(owner);
  1975. }
  1976. p_node->set_scene_file_path(get_scene_file_path());
  1977. }
  1978. void Node::_replace_connections_target(Node *p_new_target) {
  1979. List<Connection> cl;
  1980. get_signals_connected_to_this(&cl);
  1981. for (const Connection &c : cl) {
  1982. if (c.flags & CONNECT_PERSIST) {
  1983. c.signal.get_object()->disconnect(c.signal.get_name(), Callable(this, c.callable.get_method()));
  1984. bool valid = p_new_target->has_method(c.callable.get_method()) || Ref<Script>(p_new_target->get_script()).is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.callable.get_method());
  1985. ERR_CONTINUE_MSG(!valid, vformat("Attempt to connect signal '%s.%s' to nonexistent method '%s.%s'.", c.signal.get_object()->get_class(), c.signal.get_name(), c.callable.get_object()->get_class(), c.callable.get_method()));
  1986. c.signal.get_object()->connect(c.signal.get_name(), Callable(p_new_target, c.callable.get_method()), c.binds, c.flags);
  1987. }
  1988. }
  1989. }
  1990. bool Node::has_node_and_resource(const NodePath &p_path) const {
  1991. if (!has_node(p_path)) {
  1992. return false;
  1993. }
  1994. RES res;
  1995. Vector<StringName> leftover_path;
  1996. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  1997. return node;
  1998. }
  1999. Array Node::_get_node_and_resource(const NodePath &p_path) {
  2000. RES res;
  2001. Vector<StringName> leftover_path;
  2002. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  2003. Array result;
  2004. if (node) {
  2005. result.push_back(node);
  2006. } else {
  2007. result.push_back(Variant());
  2008. }
  2009. if (res.is_valid()) {
  2010. result.push_back(res);
  2011. } else {
  2012. result.push_back(Variant());
  2013. }
  2014. result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
  2015. return result;
  2016. }
  2017. Node *Node::get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
  2018. Node *node = get_node(p_path);
  2019. r_res = RES();
  2020. r_leftover_subpath = Vector<StringName>();
  2021. if (!node) {
  2022. return nullptr;
  2023. }
  2024. if (p_path.get_subname_count()) {
  2025. int j = 0;
  2026. // If not p_last_is_property, we shouldn't consider the last one as part of the resource
  2027. for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
  2028. Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j)) : r_res->get(p_path.get_subname(j));
  2029. if (new_res_v.get_type() == Variant::NIL) { // Found nothing on that path
  2030. return nullptr;
  2031. }
  2032. RES new_res = new_res_v;
  2033. if (new_res.is_null()) { // No longer a resource, assume property
  2034. break;
  2035. }
  2036. r_res = new_res;
  2037. }
  2038. for (; j < p_path.get_subname_count(); j++) {
  2039. // Put the rest of the subpath in the leftover path
  2040. r_leftover_subpath.push_back(p_path.get_subname(j));
  2041. }
  2042. }
  2043. return node;
  2044. }
  2045. void Node::_set_tree(SceneTree *p_tree) {
  2046. SceneTree *tree_changed_a = nullptr;
  2047. SceneTree *tree_changed_b = nullptr;
  2048. //ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  2049. if (data.tree) {
  2050. _propagate_exit_tree();
  2051. tree_changed_a = data.tree;
  2052. }
  2053. data.tree = p_tree;
  2054. if (data.tree) {
  2055. _propagate_enter_tree();
  2056. if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
  2057. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  2058. }
  2059. tree_changed_b = data.tree;
  2060. }
  2061. if (tree_changed_a) {
  2062. tree_changed_a->tree_changed();
  2063. }
  2064. if (tree_changed_b) {
  2065. tree_changed_b->tree_changed();
  2066. }
  2067. }
  2068. #ifdef DEBUG_ENABLED
  2069. static void _Node_debug_sn(Object *p_obj) {
  2070. Node *n = Object::cast_to<Node>(p_obj);
  2071. if (!n) {
  2072. return;
  2073. }
  2074. if (n->is_inside_tree()) {
  2075. return;
  2076. }
  2077. Node *p = n;
  2078. while (p->get_parent()) {
  2079. p = p->get_parent();
  2080. }
  2081. String path;
  2082. if (p == n) {
  2083. path = n->get_name();
  2084. } else {
  2085. path = String(p->get_name()) + "/" + p->get_path_to(n);
  2086. }
  2087. print_line(itos(p_obj->get_instance_id()) + " - Stray Node: " + path + " (Type: " + n->get_class() + ")");
  2088. }
  2089. #endif // DEBUG_ENABLED
  2090. void Node::_print_orphan_nodes() {
  2091. print_orphan_nodes();
  2092. }
  2093. void Node::print_orphan_nodes() {
  2094. #ifdef DEBUG_ENABLED
  2095. ObjectDB::debug_objects(_Node_debug_sn);
  2096. #endif
  2097. }
  2098. void Node::queue_delete() {
  2099. if (is_inside_tree()) {
  2100. get_tree()->queue_delete(this);
  2101. } else {
  2102. SceneTree::get_singleton()->queue_delete(this);
  2103. }
  2104. }
  2105. TypedArray<Node> Node::_get_children(bool p_include_internal) const {
  2106. TypedArray<Node> arr;
  2107. int cc = get_child_count(p_include_internal);
  2108. arr.resize(cc);
  2109. for (int i = 0; i < cc; i++) {
  2110. arr[i] = get_child(i, p_include_internal);
  2111. }
  2112. return arr;
  2113. }
  2114. void Node::set_import_path(const NodePath &p_import_path) {
  2115. #ifdef TOOLS_ENABLED
  2116. data.import_path = p_import_path;
  2117. #endif
  2118. }
  2119. NodePath Node::get_import_path() const {
  2120. #ifdef TOOLS_ENABLED
  2121. return data.import_path;
  2122. #else
  2123. return NodePath();
  2124. #endif
  2125. }
  2126. static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
  2127. if (p_node != p_base && !p_node->get_owner()) {
  2128. return;
  2129. }
  2130. String n = p_base->get_path_to(p_node);
  2131. r_options->push_back(n.quote());
  2132. for (int i = 0; i < p_node->get_child_count(); i++) {
  2133. _add_nodes_to_options(p_base, p_node->get_child(i), r_options);
  2134. }
  2135. }
  2136. void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  2137. String pf = p_function;
  2138. if ((pf == "has_node" || pf == "get_node") && p_idx == 0) {
  2139. _add_nodes_to_options(this, this, r_options);
  2140. }
  2141. Object::get_argument_options(p_function, p_idx, r_options);
  2142. }
  2143. void Node::clear_internal_tree_resource_paths() {
  2144. clear_internal_resource_paths();
  2145. for (int i = 0; i < data.children.size(); i++) {
  2146. data.children[i]->clear_internal_tree_resource_paths();
  2147. }
  2148. }
  2149. TypedArray<String> Node::get_configuration_warnings() const {
  2150. TypedArray<String> ret;
  2151. Vector<String> warnings;
  2152. if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
  2153. for (int i = 0; i < warnings.size(); i++) {
  2154. ret.push_back(warnings[i]);
  2155. }
  2156. }
  2157. return ret;
  2158. }
  2159. String Node::get_configuration_warnings_as_string() const {
  2160. TypedArray<String> warnings = get_configuration_warnings();
  2161. String all_warnings = String();
  2162. for (int i = 0; i < warnings.size(); i++) {
  2163. if (i > 0) {
  2164. all_warnings += "\n\n";
  2165. }
  2166. // Format as a bullet point list to make multiple warnings easier to distinguish
  2167. // from each other.
  2168. all_warnings += String::utf8("• ") + String(warnings[i]);
  2169. }
  2170. return all_warnings;
  2171. }
  2172. void Node::update_configuration_warnings() {
  2173. #ifdef TOOLS_ENABLED
  2174. if (!is_inside_tree()) {
  2175. return;
  2176. }
  2177. if (get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_ancestor_of(this))) {
  2178. get_tree()->emit_signal(SceneStringNames::get_singleton()->node_configuration_warning_changed, this);
  2179. }
  2180. #endif
  2181. }
  2182. bool Node::is_owned_by_parent() const {
  2183. return data.parent_owned;
  2184. }
  2185. void Node::set_display_folded(bool p_folded) {
  2186. data.display_folded = p_folded;
  2187. }
  2188. bool Node::is_displayed_folded() const {
  2189. return data.display_folded;
  2190. }
  2191. void Node::request_ready() {
  2192. data.ready_first = true;
  2193. }
  2194. void Node::_call_input(const Ref<InputEvent> &p_event) {
  2195. GDVIRTUAL_CALL(_input, p_event);
  2196. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2197. return;
  2198. }
  2199. input(p_event);
  2200. }
  2201. void Node::_call_shortcut_input(const Ref<InputEvent> &p_event) {
  2202. GDVIRTUAL_CALL(_shortcut_input, p_event);
  2203. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2204. return;
  2205. }
  2206. shortcut_input(p_event);
  2207. }
  2208. void Node::_call_unhandled_input(const Ref<InputEvent> &p_event) {
  2209. GDVIRTUAL_CALL(_unhandled_input, p_event);
  2210. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2211. return;
  2212. }
  2213. unhandled_input(p_event);
  2214. }
  2215. void Node::_call_unhandled_key_input(const Ref<InputEvent> &p_event) {
  2216. GDVIRTUAL_CALL(_unhandled_key_input, p_event);
  2217. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2218. return;
  2219. }
  2220. unhandled_key_input(p_event);
  2221. }
  2222. void Node::input(const Ref<InputEvent> &p_event) {
  2223. }
  2224. void Node::shortcut_input(const Ref<InputEvent> &p_key_event) {
  2225. }
  2226. void Node::unhandled_input(const Ref<InputEvent> &p_event) {
  2227. }
  2228. void Node::unhandled_key_input(const Ref<InputEvent> &p_key_event) {
  2229. }
  2230. void Node::_bind_methods() {
  2231. GLOBAL_DEF("editor/node_naming/name_num_separator", 0);
  2232. ProjectSettings::get_singleton()->set_custom_property_info("editor/node_naming/name_num_separator", PropertyInfo(Variant::INT, "editor/node_naming/name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"));
  2233. GLOBAL_DEF("editor/node_naming/name_casing", NAME_CASING_PASCAL_CASE);
  2234. ProjectSettings::get_singleton()->set_custom_property_info("editor/node_naming/name_casing", PropertyInfo(Variant::INT, "editor/node_naming/name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case"));
  2235. ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "legible_unique_name"), &Node::add_sibling, DEFVAL(false));
  2236. ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
  2237. ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
  2238. ClassDB::bind_method(D_METHOD("add_child", "node", "legible_unique_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
  2239. ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
  2240. ClassDB::bind_method(D_METHOD("get_child_count", "include_internal"), &Node::get_child_count, DEFVAL(false)); // Note that the default value bound for include_internal is false, while the method is declared with true. This is because internal nodes are irrelevant for GDSCript.
  2241. ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::_get_children, DEFVAL(false));
  2242. ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));
  2243. ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
  2244. ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
  2245. ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
  2246. ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
  2247. ClassDB::bind_method(D_METHOD("find_nodes", "mask", "type", "recursive", "owned"), &Node::find_nodes, DEFVAL(""), DEFVAL(true), DEFVAL(true));
  2248. ClassDB::bind_method(D_METHOD("find_parent", "mask"), &Node::find_parent);
  2249. ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
  2250. ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
  2251. ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
  2252. ClassDB::bind_method(D_METHOD("is_ancestor_of", "node"), &Node::is_ancestor_of);
  2253. ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
  2254. ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
  2255. ClassDB::bind_method(D_METHOD("get_path_to", "node"), &Node::get_path_to);
  2256. ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
  2257. ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
  2258. ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
  2259. ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_position"), &Node::move_child);
  2260. ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
  2261. ClassDB::bind_method(D_METHOD("raise"), &Node::raise);
  2262. ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
  2263. ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
  2264. ClassDB::bind_method(D_METHOD("remove_and_skip"), &Node::remove_and_skip);
  2265. ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false));
  2266. ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
  2267. ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
  2268. ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path);
  2269. ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path);
  2270. ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
  2271. ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
  2272. ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
  2273. ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
  2274. ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
  2275. ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
  2276. ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
  2277. ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
  2278. ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
  2279. ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
  2280. ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
  2281. ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
  2282. ClassDB::bind_method(D_METHOD("set_process_shortcut_input", "enable"), &Node::set_process_shortcut_input);
  2283. ClassDB::bind_method(D_METHOD("is_processing_shortcut_input"), &Node::is_processing_shortcut_input);
  2284. ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
  2285. ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
  2286. ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
  2287. ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
  2288. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
  2289. ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
  2290. ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
  2291. ClassDB::bind_method(D_METHOD("print_orphan_nodes"), &Node::_print_orphan_nodes);
  2292. ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
  2293. ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
  2294. ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
  2295. ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
  2296. ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
  2297. ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
  2298. ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
  2299. ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
  2300. ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANCING | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
  2301. ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
  2302. ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
  2303. ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
  2304. ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
  2305. ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
  2306. ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
  2307. ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_delete);
  2308. ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
  2309. ClassDB::bind_method(D_METHOD("set_multiplayer_authority", "id", "recursive"), &Node::set_multiplayer_authority, DEFVAL(true));
  2310. ClassDB::bind_method(D_METHOD("get_multiplayer_authority"), &Node::get_multiplayer_authority);
  2311. ClassDB::bind_method(D_METHOD("is_multiplayer_authority"), &Node::is_multiplayer_authority);
  2312. ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
  2313. ClassDB::bind_method(D_METHOD("get_custom_multiplayer"), &Node::get_custom_multiplayer);
  2314. ClassDB::bind_method(D_METHOD("set_custom_multiplayer", "api"), &Node::set_custom_multiplayer);
  2315. ClassDB::bind_method(D_METHOD("rpc_config", "method", "rpc_mode", "call_local", "transfer_mode", "channel"), &Node::rpc_config, DEFVAL(false), DEFVAL(Multiplayer::TRANSFER_MODE_RELIABLE), DEFVAL(0));
  2316. ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
  2317. ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);
  2318. ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
  2319. ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);
  2320. ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
  2321. ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
  2322. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_unique_name_in_owner", "is_unique_name_in_owner");
  2323. #ifdef TOOLS_ENABLED
  2324. ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
  2325. #endif
  2326. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "_import_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_import_path", "_get_import_path");
  2327. {
  2328. MethodInfo mi;
  2329. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  2330. mi.name = "rpc";
  2331. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
  2332. mi.arguments.push_front(PropertyInfo(Variant::INT, "peer_id"));
  2333. mi.name = "rpc_id";
  2334. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
  2335. }
  2336. ClassDB::bind_method(D_METHOD("update_configuration_warnings"), &Node::update_configuration_warnings);
  2337. BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
  2338. BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
  2339. BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
  2340. BIND_CONSTANT(NOTIFICATION_READY);
  2341. BIND_CONSTANT(NOTIFICATION_PAUSED);
  2342. BIND_CONSTANT(NOTIFICATION_UNPAUSED);
  2343. BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
  2344. BIND_CONSTANT(NOTIFICATION_PROCESS);
  2345. BIND_CONSTANT(NOTIFICATION_PARENTED);
  2346. BIND_CONSTANT(NOTIFICATION_UNPARENTED);
  2347. BIND_CONSTANT(NOTIFICATION_INSTANCED);
  2348. BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
  2349. BIND_CONSTANT(NOTIFICATION_DRAG_END);
  2350. BIND_CONSTANT(NOTIFICATION_PATH_RENAMED);
  2351. BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
  2352. BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  2353. BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
  2354. BIND_CONSTANT(NOTIFICATION_DISABLED);
  2355. BIND_CONSTANT(NOTIFICATION_ENABLED);
  2356. BIND_CONSTANT(NOTIFICATION_EDITOR_PRE_SAVE);
  2357. BIND_CONSTANT(NOTIFICATION_EDITOR_POST_SAVE);
  2358. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
  2359. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
  2360. BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_IN);
  2361. BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  2362. BIND_CONSTANT(NOTIFICATION_WM_CLOSE_REQUEST);
  2363. BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
  2364. BIND_CONSTANT(NOTIFICATION_WM_SIZE_CHANGED);
  2365. BIND_CONSTANT(NOTIFICATION_WM_DPI_CHANGE);
  2366. BIND_CONSTANT(NOTIFICATION_VP_MOUSE_ENTER);
  2367. BIND_CONSTANT(NOTIFICATION_VP_MOUSE_EXIT);
  2368. BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
  2369. BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
  2370. BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
  2371. BIND_CONSTANT(NOTIFICATION_CRASH);
  2372. BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
  2373. BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
  2374. BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
  2375. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
  2376. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
  2377. BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
  2378. BIND_ENUM_CONSTANT(PROCESS_MODE_INHERIT);
  2379. BIND_ENUM_CONSTANT(PROCESS_MODE_PAUSABLE);
  2380. BIND_ENUM_CONSTANT(PROCESS_MODE_WHEN_PAUSED);
  2381. BIND_ENUM_CONSTANT(PROCESS_MODE_ALWAYS);
  2382. BIND_ENUM_CONSTANT(PROCESS_MODE_DISABLED);
  2383. BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
  2384. BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
  2385. BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
  2386. BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANCING);
  2387. BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
  2388. BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
  2389. BIND_ENUM_CONSTANT(INTERNAL_MODE_BACK);
  2390. ADD_SIGNAL(MethodInfo("ready"));
  2391. ADD_SIGNAL(MethodInfo("renamed"));
  2392. ADD_SIGNAL(MethodInfo("tree_entered"));
  2393. ADD_SIGNAL(MethodInfo("tree_exiting"));
  2394. ADD_SIGNAL(MethodInfo("tree_exited"));
  2395. ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  2396. ADD_SIGNAL(MethodInfo("child_exited_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  2397. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
  2398. ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
  2399. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_owner", "get_owner");
  2400. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "", "get_multiplayer");
  2401. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "set_custom_multiplayer", "get_custom_multiplayer");
  2402. ADD_GROUP("Process", "process_");
  2403. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
  2404. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
  2405. ADD_GROUP("Editor Description", "editor_");
  2406. ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
  2407. GDVIRTUAL_BIND(_process, "delta");
  2408. GDVIRTUAL_BIND(_physics_process, "delta");
  2409. GDVIRTUAL_BIND(_enter_tree);
  2410. GDVIRTUAL_BIND(_exit_tree);
  2411. GDVIRTUAL_BIND(_ready);
  2412. GDVIRTUAL_BIND(_get_configuration_warnings);
  2413. GDVIRTUAL_BIND(_input, "event");
  2414. GDVIRTUAL_BIND(_shortcut_input, "event");
  2415. GDVIRTUAL_BIND(_unhandled_input, "event");
  2416. GDVIRTUAL_BIND(_unhandled_key_input, "event");
  2417. }
  2418. String Node::_get_name_num_separator() {
  2419. switch (ProjectSettings::get_singleton()->get("editor/node_naming/name_num_separator").operator int()) {
  2420. case 0:
  2421. return "";
  2422. case 1:
  2423. return " ";
  2424. case 2:
  2425. return "_";
  2426. case 3:
  2427. return "-";
  2428. }
  2429. return " ";
  2430. }
  2431. Node::Node() {
  2432. orphan_node_count++;
  2433. }
  2434. Node::~Node() {
  2435. data.grouped.clear();
  2436. data.owned.clear();
  2437. data.children.clear();
  2438. ERR_FAIL_COND(data.parent);
  2439. ERR_FAIL_COND(data.children.size());
  2440. orphan_node_count--;
  2441. }
  2442. ////////////////////////////////