node.cpp 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932
  1. /**************************************************************************/
  2. /* node.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "node.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/object/message_queue.h"
  34. #include "core/object/script_language.h"
  35. #include "core/string/print_string.h"
  36. #include "instance_placeholder.h"
  37. #include "scene/animation/tween.h"
  38. #include "scene/debugger/scene_debugger.h"
  39. #include "scene/main/multiplayer_api.h"
  40. #include "scene/main/window.h"
  41. #include "scene/resources/packed_scene.h"
  42. #include "viewport.h"
  43. #include <stdint.h>
  44. int Node::orphan_node_count = 0;
  45. thread_local Node *Node::current_process_thread_group = nullptr;
  46. void Node::_notification(int p_notification) {
  47. switch (p_notification) {
  48. case NOTIFICATION_PROCESS: {
  49. GDVIRTUAL_CALL(_process, get_process_delta_time());
  50. } break;
  51. case NOTIFICATION_PHYSICS_PROCESS: {
  52. GDVIRTUAL_CALL(_physics_process, get_physics_process_delta_time());
  53. } break;
  54. case NOTIFICATION_ENTER_TREE: {
  55. ERR_FAIL_NULL(get_viewport());
  56. ERR_FAIL_NULL(get_tree());
  57. // Update process mode.
  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. { // Update threaded process mode.
  70. if (data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
  71. if (data.parent) {
  72. data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
  73. }
  74. if (data.process_thread_group_owner) {
  75. data.process_group = data.process_thread_group_owner->data.process_group;
  76. } else {
  77. data.process_group = &data.tree->default_process_group;
  78. }
  79. } else {
  80. data.process_thread_group_owner = this;
  81. _add_process_group();
  82. }
  83. if (_is_any_processing()) {
  84. _add_to_process_thread_group();
  85. }
  86. }
  87. if (data.physics_interpolation_mode == PHYSICS_INTERPOLATION_MODE_INHERIT) {
  88. bool interpolate = true; // Root node default is for interpolation to be on.
  89. if (data.parent) {
  90. interpolate = data.parent->is_physics_interpolated();
  91. }
  92. _propagate_physics_interpolated(interpolate);
  93. }
  94. // Update auto translate mode.
  95. if (data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT && !data.parent) {
  96. ERR_PRINT("The root node can't be set to Inherit auto translate mode, reverting to Always instead.");
  97. data.auto_translate_mode = AUTO_TRANSLATE_MODE_ALWAYS;
  98. }
  99. data.is_auto_translate_dirty = true;
  100. #ifdef TOOLS_ENABLED
  101. // Don't translate UI elements when they're being edited.
  102. if (is_part_of_edited_scene()) {
  103. set_message_translation(false);
  104. }
  105. #endif
  106. if (data.auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {
  107. notification(NOTIFICATION_TRANSLATION_CHANGED);
  108. }
  109. if (data.input) {
  110. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  111. }
  112. if (data.shortcut_input) {
  113. add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  114. }
  115. if (data.unhandled_input) {
  116. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  117. }
  118. if (data.unhandled_key_input) {
  119. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  120. }
  121. get_tree()->nodes_in_tree_count++;
  122. orphan_node_count--;
  123. } break;
  124. case NOTIFICATION_EXIT_TREE: {
  125. ERR_FAIL_NULL(get_viewport());
  126. ERR_FAIL_NULL(get_tree());
  127. get_tree()->nodes_in_tree_count--;
  128. orphan_node_count++;
  129. if (data.input) {
  130. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  131. }
  132. if (data.shortcut_input) {
  133. remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  134. }
  135. if (data.unhandled_input) {
  136. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  137. }
  138. if (data.unhandled_key_input) {
  139. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  140. }
  141. // Remove from processing first.
  142. if (_is_any_processing()) {
  143. _remove_from_process_thread_group();
  144. }
  145. // Remove the process group.
  146. if (data.process_thread_group_owner == this) {
  147. _remove_process_group();
  148. }
  149. data.process_thread_group_owner = nullptr;
  150. data.process_owner = nullptr;
  151. if (data.path_cache) {
  152. memdelete(data.path_cache);
  153. data.path_cache = nullptr;
  154. }
  155. } break;
  156. case NOTIFICATION_PATH_RENAMED: {
  157. if (data.path_cache) {
  158. memdelete(data.path_cache);
  159. data.path_cache = nullptr;
  160. }
  161. } break;
  162. case NOTIFICATION_READY: {
  163. if (GDVIRTUAL_IS_OVERRIDDEN(_input)) {
  164. set_process_input(true);
  165. }
  166. if (GDVIRTUAL_IS_OVERRIDDEN(_shortcut_input)) {
  167. set_process_shortcut_input(true);
  168. }
  169. if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_input)) {
  170. set_process_unhandled_input(true);
  171. }
  172. if (GDVIRTUAL_IS_OVERRIDDEN(_unhandled_key_input)) {
  173. set_process_unhandled_key_input(true);
  174. }
  175. if (GDVIRTUAL_IS_OVERRIDDEN(_process)) {
  176. set_process(true);
  177. }
  178. if (GDVIRTUAL_IS_OVERRIDDEN(_physics_process)) {
  179. set_physics_process(true);
  180. }
  181. GDVIRTUAL_CALL(_ready);
  182. } break;
  183. case NOTIFICATION_POSTINITIALIZE: {
  184. data.in_constructor = false;
  185. } break;
  186. case NOTIFICATION_PREDELETE: {
  187. if (data.inside_tree && !Thread::is_main_thread()) {
  188. cancel_free();
  189. ERR_PRINT("Attempted to free a node that is currently added to the SceneTree from a thread. This is not permitted, use queue_free() instead. Node has not been freed.");
  190. return;
  191. }
  192. if (data.owner) {
  193. _clean_up_owner();
  194. }
  195. while (!data.owned.is_empty()) {
  196. Node *n = data.owned.back()->get();
  197. n->_clean_up_owner(); // This will change data.owned. So it's impossible to loop over the list in the usual manner.
  198. }
  199. if (data.parent) {
  200. data.parent->remove_child(this);
  201. }
  202. // kill children as cleanly as possible
  203. while (data.children.size()) {
  204. Node *child = data.children.last()->value; // begin from the end because its faster and more consistent with creation
  205. memdelete(child);
  206. }
  207. } break;
  208. case NOTIFICATION_TRANSLATION_CHANGED: {
  209. if (data.inside_tree) {
  210. data.is_auto_translate_dirty = true;
  211. }
  212. } break;
  213. }
  214. }
  215. void Node::_propagate_ready() {
  216. data.ready_notified = true;
  217. data.blocked++;
  218. for (KeyValue<StringName, Node *> &K : data.children) {
  219. K.value->_propagate_ready();
  220. }
  221. data.blocked--;
  222. notification(NOTIFICATION_POST_ENTER_TREE);
  223. if (data.ready_first) {
  224. data.ready_first = false;
  225. notification(NOTIFICATION_READY);
  226. emit_signal(SceneStringName(ready));
  227. }
  228. }
  229. void Node::_propagate_enter_tree() {
  230. // this needs to happen to all children before any enter_tree
  231. if (data.parent) {
  232. data.tree = data.parent->data.tree;
  233. data.depth = data.parent->data.depth + 1;
  234. } else {
  235. data.depth = 1;
  236. }
  237. data.viewport = Object::cast_to<Viewport>(this);
  238. if (!data.viewport && data.parent) {
  239. data.viewport = data.parent->data.viewport;
  240. }
  241. data.inside_tree = true;
  242. for (KeyValue<StringName, GroupData> &E : data.grouped) {
  243. E.value.group = data.tree->add_to_group(E.key, this);
  244. }
  245. notification(NOTIFICATION_ENTER_TREE);
  246. GDVIRTUAL_CALL(_enter_tree);
  247. emit_signal(SceneStringName(tree_entered));
  248. data.tree->node_added(this);
  249. if (data.parent) {
  250. Variant c = this;
  251. const Variant *cptr = &c;
  252. data.parent->emit_signalp(SNAME("child_entered_tree"), &cptr, 1);
  253. }
  254. data.blocked++;
  255. //block while adding children
  256. for (KeyValue<StringName, Node *> &K : data.children) {
  257. if (!K.value->is_inside_tree()) { // could have been added in enter_tree
  258. K.value->_propagate_enter_tree();
  259. }
  260. }
  261. data.blocked--;
  262. #ifdef DEBUG_ENABLED
  263. SceneDebugger::add_to_cache(data.scene_file_path, this);
  264. #endif
  265. // enter groups
  266. }
  267. void Node::_propagate_after_exit_tree() {
  268. // Clear owner if it was not part of the pruned branch
  269. if (data.owner) {
  270. bool found = false;
  271. Node *parent = data.parent;
  272. while (parent) {
  273. if (parent == data.owner) {
  274. found = true;
  275. break;
  276. }
  277. parent = parent->data.parent;
  278. }
  279. if (!found) {
  280. _clean_up_owner();
  281. }
  282. }
  283. data.blocked++;
  284. for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
  285. I->value->_propagate_after_exit_tree();
  286. }
  287. data.blocked--;
  288. emit_signal(SceneStringName(tree_exited));
  289. }
  290. void Node::_propagate_exit_tree() {
  291. //block while removing children
  292. #ifdef DEBUG_ENABLED
  293. if (!data.scene_file_path.is_empty()) {
  294. // Only remove if file path is set (optimization).
  295. SceneDebugger::remove_from_cache(data.scene_file_path, this);
  296. }
  297. #endif
  298. data.blocked++;
  299. for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
  300. I->value->_propagate_exit_tree();
  301. }
  302. data.blocked--;
  303. GDVIRTUAL_CALL(_exit_tree);
  304. emit_signal(SceneStringName(tree_exiting));
  305. notification(NOTIFICATION_EXIT_TREE, true);
  306. if (data.tree) {
  307. data.tree->node_removed(this);
  308. }
  309. if (data.parent) {
  310. Variant c = this;
  311. const Variant *cptr = &c;
  312. data.parent->emit_signalp(SNAME("child_exiting_tree"), &cptr, 1);
  313. }
  314. // exit groups
  315. for (KeyValue<StringName, GroupData> &E : data.grouped) {
  316. data.tree->remove_from_group(E.key, this);
  317. E.value.group = nullptr;
  318. }
  319. data.viewport = nullptr;
  320. if (data.tree) {
  321. data.tree->tree_changed();
  322. }
  323. data.inside_tree = false;
  324. data.ready_notified = false;
  325. data.tree = nullptr;
  326. data.depth = -1;
  327. }
  328. void Node::_propagate_physics_interpolated(bool p_interpolated) {
  329. switch (data.physics_interpolation_mode) {
  330. case PHYSICS_INTERPOLATION_MODE_INHERIT:
  331. // Keep the parent p_interpolated.
  332. break;
  333. case PHYSICS_INTERPOLATION_MODE_OFF: {
  334. p_interpolated = false;
  335. } break;
  336. case PHYSICS_INTERPOLATION_MODE_ON: {
  337. p_interpolated = true;
  338. } break;
  339. }
  340. // No change? No need to propagate further.
  341. if (data.physics_interpolated == p_interpolated) {
  342. return;
  343. }
  344. data.physics_interpolated = p_interpolated;
  345. // Allow a call to the RenderingServer etc. in derived classes.
  346. _physics_interpolated_changed();
  347. data.blocked++;
  348. for (KeyValue<StringName, Node *> &K : data.children) {
  349. K.value->_propagate_physics_interpolated(p_interpolated);
  350. }
  351. data.blocked--;
  352. }
  353. void Node::move_child(Node *p_child, int p_index) {
  354. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Moving child node positions inside the SceneTree is only allowed from the main thread. Use call_deferred(\"move_child\",child,index).");
  355. ERR_FAIL_NULL(p_child);
  356. ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
  357. _update_children_cache();
  358. // We need to check whether node is internal and move it only in the relevant node range.
  359. if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
  360. if (p_index < 0) {
  361. p_index += data.internal_children_front_count_cache;
  362. }
  363. ERR_FAIL_INDEX_MSG(p_index, data.internal_children_front_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
  364. _move_child(p_child, p_index);
  365. } else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
  366. if (p_index < 0) {
  367. p_index += data.internal_children_back_count_cache;
  368. }
  369. ERR_FAIL_INDEX_MSG(p_index, data.internal_children_back_count_cache, vformat("Invalid new child index: %d. Child is internal.", p_index));
  370. _move_child(p_child, (int)data.children_cache.size() - data.internal_children_back_count_cache + p_index);
  371. } else {
  372. if (p_index < 0) {
  373. p_index += get_child_count(false);
  374. }
  375. ERR_FAIL_INDEX_MSG(p_index, (int)data.children_cache.size() + 1 - data.internal_children_front_count_cache - data.internal_children_back_count_cache, vformat("Invalid new child index: %d.", p_index));
  376. _move_child(p_child, p_index + data.internal_children_front_count_cache);
  377. }
  378. }
  379. void Node::_move_child(Node *p_child, int p_index, bool p_ignore_end) {
  380. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `move_child()` failed. Consider using `move_child.call_deferred(child, index)` instead (or `popup.call_deferred()` if this is from a popup).");
  381. // Specifying one place beyond the end
  382. // means the same as moving to the last index
  383. if (!p_ignore_end) { // p_ignore_end is a little hack to make back internal children work properly.
  384. if (p_child->data.internal_mode == INTERNAL_MODE_FRONT) {
  385. if (p_index == data.internal_children_front_count_cache) {
  386. p_index--;
  387. }
  388. } else if (p_child->data.internal_mode == INTERNAL_MODE_BACK) {
  389. if (p_index == (int)data.children_cache.size()) {
  390. p_index--;
  391. }
  392. } else {
  393. if (p_index == (int)data.children_cache.size() - data.internal_children_back_count_cache) {
  394. p_index--;
  395. }
  396. }
  397. }
  398. int child_index = p_child->get_index();
  399. if (child_index == p_index) {
  400. return; //do nothing
  401. }
  402. int motion_from = MIN(p_index, child_index);
  403. int motion_to = MAX(p_index, child_index);
  404. data.children_cache.remove_at(child_index);
  405. data.children_cache.insert(p_index, p_child);
  406. if (data.tree) {
  407. data.tree->tree_changed();
  408. }
  409. data.blocked++;
  410. //new pos first
  411. for (int i = motion_from; i <= motion_to; i++) {
  412. if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_DISABLED) {
  413. data.children_cache[i]->data.index = i - data.internal_children_front_count_cache;
  414. } else if (data.children_cache[i]->data.internal_mode == INTERNAL_MODE_BACK) {
  415. data.children_cache[i]->data.index = i - data.internal_children_front_count_cache - data.external_children_count_cache;
  416. } else {
  417. data.children_cache[i]->data.index = i;
  418. }
  419. }
  420. // notification second
  421. move_child_notify(p_child);
  422. notification(NOTIFICATION_CHILD_ORDER_CHANGED);
  423. emit_signal(SNAME("child_order_changed"));
  424. p_child->_propagate_groups_dirty();
  425. data.blocked--;
  426. }
  427. void Node::_propagate_groups_dirty() {
  428. for (const KeyValue<StringName, GroupData> &E : data.grouped) {
  429. if (E.value.group) {
  430. E.value.group->changed = true;
  431. }
  432. }
  433. for (KeyValue<StringName, Node *> &K : data.children) {
  434. K.value->_propagate_groups_dirty();
  435. }
  436. }
  437. void Node::add_child_notify(Node *p_child) {
  438. // to be used when not wanted
  439. }
  440. void Node::remove_child_notify(Node *p_child) {
  441. // to be used when not wanted
  442. }
  443. void Node::move_child_notify(Node *p_child) {
  444. // to be used when not wanted
  445. }
  446. void Node::owner_changed_notify() {
  447. }
  448. void Node::_physics_interpolated_changed() {}
  449. void Node::set_physics_process(bool p_process) {
  450. ERR_THREAD_GUARD
  451. if (data.physics_process == p_process) {
  452. return;
  453. }
  454. if (!is_inside_tree()) {
  455. data.physics_process = p_process;
  456. return;
  457. }
  458. if (_is_any_processing()) {
  459. _remove_from_process_thread_group();
  460. }
  461. data.physics_process = p_process;
  462. if (_is_any_processing()) {
  463. _add_to_process_thread_group();
  464. }
  465. }
  466. bool Node::is_physics_processing() const {
  467. return data.physics_process;
  468. }
  469. void Node::set_physics_process_internal(bool p_process_internal) {
  470. ERR_THREAD_GUARD
  471. if (data.physics_process_internal == p_process_internal) {
  472. return;
  473. }
  474. if (!is_inside_tree()) {
  475. data.physics_process_internal = p_process_internal;
  476. return;
  477. }
  478. if (_is_any_processing()) {
  479. _remove_from_process_thread_group();
  480. }
  481. data.physics_process_internal = p_process_internal;
  482. if (_is_any_processing()) {
  483. _add_to_process_thread_group();
  484. }
  485. }
  486. bool Node::is_physics_processing_internal() const {
  487. return data.physics_process_internal;
  488. }
  489. void Node::set_process_mode(ProcessMode p_mode) {
  490. ERR_THREAD_GUARD
  491. if (data.process_mode == p_mode) {
  492. return;
  493. }
  494. if (!is_inside_tree()) {
  495. data.process_mode = p_mode;
  496. return;
  497. }
  498. bool prev_can_process = can_process();
  499. bool prev_enabled = _is_enabled();
  500. if (p_mode == PROCESS_MODE_INHERIT) {
  501. if (data.parent) {
  502. data.process_owner = data.parent->data.process_owner;
  503. } else {
  504. ERR_FAIL_MSG("The root node can't be set to Inherit process mode.");
  505. }
  506. } else {
  507. data.process_owner = this;
  508. }
  509. data.process_mode = p_mode;
  510. bool next_can_process = can_process();
  511. bool next_enabled = _is_enabled();
  512. int pause_notification = 0;
  513. if (prev_can_process && !next_can_process) {
  514. pause_notification = NOTIFICATION_PAUSED;
  515. } else if (!prev_can_process && next_can_process) {
  516. pause_notification = NOTIFICATION_UNPAUSED;
  517. }
  518. int enabled_notification = 0;
  519. if (prev_enabled && !next_enabled) {
  520. enabled_notification = NOTIFICATION_DISABLED;
  521. } else if (!prev_enabled && next_enabled) {
  522. enabled_notification = NOTIFICATION_ENABLED;
  523. }
  524. _propagate_process_owner(data.process_owner, pause_notification, enabled_notification);
  525. #ifdef TOOLS_ENABLED
  526. // This is required for the editor to update the visibility of disabled nodes
  527. // It's very expensive during runtime to change, so editor-only
  528. if (Engine::get_singleton()->is_editor_hint()) {
  529. get_tree()->emit_signal(SNAME("tree_process_mode_changed"));
  530. }
  531. #endif
  532. }
  533. void Node::_propagate_pause_notification(bool p_enable) {
  534. bool prev_can_process = _can_process(!p_enable);
  535. bool next_can_process = _can_process(p_enable);
  536. if (prev_can_process && !next_can_process) {
  537. notification(NOTIFICATION_PAUSED);
  538. } else if (!prev_can_process && next_can_process) {
  539. notification(NOTIFICATION_UNPAUSED);
  540. }
  541. data.blocked++;
  542. for (KeyValue<StringName, Node *> &K : data.children) {
  543. K.value->_propagate_pause_notification(p_enable);
  544. }
  545. data.blocked--;
  546. }
  547. Node::ProcessMode Node::get_process_mode() const {
  548. return data.process_mode;
  549. }
  550. void Node::_propagate_process_owner(Node *p_owner, int p_pause_notification, int p_enabled_notification) {
  551. data.process_owner = p_owner;
  552. if (p_pause_notification != 0) {
  553. notification(p_pause_notification);
  554. }
  555. if (p_enabled_notification != 0) {
  556. notification(p_enabled_notification);
  557. }
  558. data.blocked++;
  559. for (KeyValue<StringName, Node *> &K : data.children) {
  560. Node *c = K.value;
  561. if (c->data.process_mode == PROCESS_MODE_INHERIT) {
  562. c->_propagate_process_owner(p_owner, p_pause_notification, p_enabled_notification);
  563. }
  564. }
  565. data.blocked--;
  566. }
  567. void Node::set_multiplayer_authority(int p_peer_id, bool p_recursive) {
  568. ERR_THREAD_GUARD
  569. data.multiplayer_authority = p_peer_id;
  570. if (p_recursive) {
  571. for (KeyValue<StringName, Node *> &K : data.children) {
  572. K.value->set_multiplayer_authority(p_peer_id, true);
  573. }
  574. }
  575. }
  576. int Node::get_multiplayer_authority() const {
  577. return data.multiplayer_authority;
  578. }
  579. bool Node::is_multiplayer_authority() const {
  580. ERR_FAIL_COND_V(!is_inside_tree(), false);
  581. Ref<MultiplayerAPI> api = get_multiplayer();
  582. return api.is_valid() && (api->get_unique_id() == data.multiplayer_authority);
  583. }
  584. /***** RPC CONFIG ********/
  585. void Node::rpc_config(const StringName &p_method, const Variant &p_config) {
  586. ERR_THREAD_GUARD
  587. if (data.rpc_config.get_type() != Variant::DICTIONARY) {
  588. data.rpc_config = Dictionary();
  589. }
  590. Dictionary node_config = data.rpc_config;
  591. if (p_config.get_type() == Variant::NIL) {
  592. node_config.erase(p_method);
  593. } else {
  594. ERR_FAIL_COND(p_config.get_type() != Variant::DICTIONARY);
  595. node_config[p_method] = p_config;
  596. }
  597. }
  598. const Variant Node::get_node_rpc_config() const {
  599. return data.rpc_config;
  600. }
  601. /***** RPC FUNCTIONS ********/
  602. Error Node::_rpc_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  603. if (p_argcount < 1) {
  604. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  605. r_error.expected = 1;
  606. return ERR_INVALID_PARAMETER;
  607. }
  608. Variant::Type type = p_args[0]->get_type();
  609. if (type != Variant::STRING_NAME && type != Variant::STRING) {
  610. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  611. r_error.argument = 0;
  612. r_error.expected = Variant::STRING_NAME;
  613. return ERR_INVALID_PARAMETER;
  614. }
  615. StringName method = (*p_args[0]).operator StringName();
  616. Error err = rpcp(0, method, &p_args[1], p_argcount - 1);
  617. r_error.error = Callable::CallError::CALL_OK;
  618. return err;
  619. }
  620. Error Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  621. if (p_argcount < 2) {
  622. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  623. r_error.expected = 2;
  624. return ERR_INVALID_PARAMETER;
  625. }
  626. if (p_args[0]->get_type() != Variant::INT) {
  627. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  628. r_error.argument = 0;
  629. r_error.expected = Variant::INT;
  630. return ERR_INVALID_PARAMETER;
  631. }
  632. Variant::Type type = p_args[1]->get_type();
  633. if (type != Variant::STRING_NAME && type != Variant::STRING) {
  634. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  635. r_error.argument = 1;
  636. r_error.expected = Variant::STRING_NAME;
  637. return ERR_INVALID_PARAMETER;
  638. }
  639. int peer_id = *p_args[0];
  640. StringName method = (*p_args[1]).operator StringName();
  641. Error err = rpcp(peer_id, method, &p_args[2], p_argcount - 2);
  642. r_error.error = Callable::CallError::CALL_OK;
  643. return err;
  644. }
  645. Error Node::rpcp(int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  646. ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
  647. Ref<MultiplayerAPI> api = get_multiplayer();
  648. if (api.is_null()) {
  649. return ERR_UNCONFIGURED;
  650. }
  651. return api->rpcp(this, p_peer_id, p_method, p_arg, p_argcount);
  652. }
  653. Ref<MultiplayerAPI> Node::get_multiplayer() const {
  654. if (!is_inside_tree()) {
  655. return Ref<MultiplayerAPI>();
  656. }
  657. return get_tree()->get_multiplayer(get_path());
  658. }
  659. //////////// end of rpc
  660. bool Node::can_process_notification(int p_what) const {
  661. switch (p_what) {
  662. case NOTIFICATION_PHYSICS_PROCESS:
  663. return data.physics_process;
  664. case NOTIFICATION_PROCESS:
  665. return data.process;
  666. case NOTIFICATION_INTERNAL_PROCESS:
  667. return data.process_internal;
  668. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
  669. return data.physics_process_internal;
  670. }
  671. return true;
  672. }
  673. bool Node::can_process() const {
  674. ERR_FAIL_COND_V(!is_inside_tree(), false);
  675. return _can_process(get_tree()->is_paused());
  676. }
  677. bool Node::_can_process(bool p_paused) const {
  678. ProcessMode process_mode;
  679. if (data.process_mode == PROCESS_MODE_INHERIT) {
  680. if (!data.process_owner) {
  681. process_mode = PROCESS_MODE_PAUSABLE;
  682. } else {
  683. process_mode = data.process_owner->data.process_mode;
  684. }
  685. } else {
  686. process_mode = data.process_mode;
  687. }
  688. // The owner can't be set to inherit, must be a bug.
  689. ERR_FAIL_COND_V(process_mode == PROCESS_MODE_INHERIT, false);
  690. if (process_mode == PROCESS_MODE_DISABLED) {
  691. return false;
  692. } else if (process_mode == PROCESS_MODE_ALWAYS) {
  693. return true;
  694. }
  695. if (p_paused) {
  696. return process_mode == PROCESS_MODE_WHEN_PAUSED;
  697. } else {
  698. return process_mode == PROCESS_MODE_PAUSABLE;
  699. }
  700. }
  701. void Node::set_physics_interpolation_mode(PhysicsInterpolationMode p_mode) {
  702. ERR_THREAD_GUARD
  703. if (data.physics_interpolation_mode == p_mode) {
  704. return;
  705. }
  706. data.physics_interpolation_mode = p_mode;
  707. bool interpolate = true; // Default for root node.
  708. switch (p_mode) {
  709. case PHYSICS_INTERPOLATION_MODE_INHERIT: {
  710. if (is_inside_tree() && data.parent) {
  711. interpolate = data.parent->is_physics_interpolated();
  712. }
  713. } break;
  714. case PHYSICS_INTERPOLATION_MODE_OFF: {
  715. interpolate = false;
  716. } break;
  717. case PHYSICS_INTERPOLATION_MODE_ON: {
  718. interpolate = true;
  719. } break;
  720. }
  721. // If swapping from interpolated to non-interpolated, use this as an extra means to cause a reset.
  722. if (is_physics_interpolated() && !interpolate) {
  723. reset_physics_interpolation();
  724. }
  725. _propagate_physics_interpolated(interpolate);
  726. }
  727. void Node::reset_physics_interpolation() {
  728. propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  729. }
  730. bool Node::_is_enabled() const {
  731. ProcessMode process_mode;
  732. if (data.process_mode == PROCESS_MODE_INHERIT) {
  733. if (!data.process_owner) {
  734. process_mode = PROCESS_MODE_PAUSABLE;
  735. } else {
  736. process_mode = data.process_owner->data.process_mode;
  737. }
  738. } else {
  739. process_mode = data.process_mode;
  740. }
  741. return (process_mode != PROCESS_MODE_DISABLED);
  742. }
  743. bool Node::is_enabled() const {
  744. ERR_FAIL_COND_V(!is_inside_tree(), false);
  745. return _is_enabled();
  746. }
  747. double Node::get_physics_process_delta_time() const {
  748. if (data.tree) {
  749. return data.tree->get_physics_process_time();
  750. } else {
  751. return 0;
  752. }
  753. }
  754. double Node::get_process_delta_time() const {
  755. if (data.tree) {
  756. return data.tree->get_process_time();
  757. } else {
  758. return 0;
  759. }
  760. }
  761. void Node::set_process(bool p_process) {
  762. ERR_THREAD_GUARD
  763. if (data.process == p_process) {
  764. return;
  765. }
  766. if (!is_inside_tree()) {
  767. data.process = p_process;
  768. return;
  769. }
  770. if (_is_any_processing()) {
  771. _remove_from_process_thread_group();
  772. }
  773. data.process = p_process;
  774. if (_is_any_processing()) {
  775. _add_to_process_thread_group();
  776. }
  777. }
  778. bool Node::is_processing() const {
  779. return data.process;
  780. }
  781. void Node::set_process_internal(bool p_process_internal) {
  782. ERR_THREAD_GUARD
  783. if (data.process_internal == p_process_internal) {
  784. return;
  785. }
  786. if (!is_inside_tree()) {
  787. data.process_internal = p_process_internal;
  788. return;
  789. }
  790. if (_is_any_processing()) {
  791. _remove_from_process_thread_group();
  792. }
  793. data.process_internal = p_process_internal;
  794. if (_is_any_processing()) {
  795. _add_to_process_thread_group();
  796. }
  797. }
  798. void Node::_add_process_group() {
  799. get_tree()->_add_process_group(this);
  800. }
  801. void Node::_remove_process_group() {
  802. get_tree()->_remove_process_group(this);
  803. }
  804. void Node::_remove_from_process_thread_group() {
  805. get_tree()->_remove_node_from_process_group(this, data.process_thread_group_owner);
  806. }
  807. void Node::_add_to_process_thread_group() {
  808. get_tree()->_add_node_to_process_group(this, data.process_thread_group_owner);
  809. }
  810. void Node::_remove_tree_from_process_thread_group() {
  811. if (!is_inside_tree()) {
  812. return; // May not be initialized yet.
  813. }
  814. for (KeyValue<StringName, Node *> &K : data.children) {
  815. if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
  816. continue;
  817. }
  818. K.value->_remove_tree_from_process_thread_group();
  819. }
  820. if (_is_any_processing()) {
  821. _remove_from_process_thread_group();
  822. }
  823. }
  824. void Node::_add_tree_to_process_thread_group(Node *p_owner) {
  825. if (_is_any_processing()) {
  826. _add_to_process_thread_group();
  827. }
  828. data.process_thread_group_owner = p_owner;
  829. if (p_owner != nullptr) {
  830. data.process_group = p_owner->data.process_group;
  831. } else {
  832. data.process_group = &data.tree->default_process_group;
  833. }
  834. for (KeyValue<StringName, Node *> &K : data.children) {
  835. if (K.value->data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
  836. continue;
  837. }
  838. K.value->_add_to_process_thread_group();
  839. }
  840. }
  841. bool Node::is_processing_internal() const {
  842. return data.process_internal;
  843. }
  844. void Node::set_process_thread_group_order(int p_order) {
  845. ERR_THREAD_GUARD
  846. if (data.process_thread_group_order == p_order) {
  847. return;
  848. }
  849. data.process_thread_group_order = p_order;
  850. // Not yet in the tree (or not a group owner, in whose case this is pointless but harmless); trivial update.
  851. if (!is_inside_tree() || data.process_thread_group_owner != this) {
  852. return;
  853. }
  854. get_tree()->process_groups_dirty = true;
  855. }
  856. int Node::get_process_thread_group_order() const {
  857. return data.process_thread_group_order;
  858. }
  859. void Node::set_process_priority(int p_priority) {
  860. ERR_THREAD_GUARD
  861. if (data.process_priority == p_priority) {
  862. return;
  863. }
  864. if (!is_inside_tree()) {
  865. // Not yet in the tree; trivial update.
  866. data.process_priority = p_priority;
  867. return;
  868. }
  869. if (_is_any_processing()) {
  870. _remove_from_process_thread_group();
  871. }
  872. data.process_priority = p_priority;
  873. if (_is_any_processing()) {
  874. _add_to_process_thread_group();
  875. }
  876. }
  877. int Node::get_process_priority() const {
  878. return data.process_priority;
  879. }
  880. void Node::set_physics_process_priority(int p_priority) {
  881. ERR_THREAD_GUARD
  882. if (data.physics_process_priority == p_priority) {
  883. return;
  884. }
  885. if (!is_inside_tree()) {
  886. // Not yet in the tree; trivial update.
  887. data.physics_process_priority = p_priority;
  888. return;
  889. }
  890. if (_is_any_processing()) {
  891. _remove_from_process_thread_group();
  892. }
  893. data.physics_process_priority = p_priority;
  894. if (_is_any_processing()) {
  895. _add_to_process_thread_group();
  896. }
  897. }
  898. int Node::get_physics_process_priority() const {
  899. return data.physics_process_priority;
  900. }
  901. void Node::set_process_thread_group(ProcessThreadGroup p_mode) {
  902. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Changing the process thread group can only be done from the main thread. Use call_deferred(\"set_process_thread_group\",mode).");
  903. if (data.process_thread_group == p_mode) {
  904. return;
  905. }
  906. if (!is_inside_tree()) {
  907. // Not yet in the tree; trivial update.
  908. data.process_thread_group = p_mode;
  909. return;
  910. }
  911. _remove_tree_from_process_thread_group();
  912. if (data.process_thread_group != PROCESS_THREAD_GROUP_INHERIT) {
  913. _remove_process_group();
  914. }
  915. data.process_thread_group = p_mode;
  916. if (p_mode == PROCESS_THREAD_GROUP_INHERIT) {
  917. if (data.parent) {
  918. data.process_thread_group_owner = data.parent->data.process_thread_group_owner;
  919. } else {
  920. data.process_thread_group_owner = nullptr;
  921. }
  922. } else {
  923. data.process_thread_group_owner = this;
  924. _add_process_group();
  925. }
  926. _add_tree_to_process_thread_group(data.process_thread_group_owner);
  927. notify_property_list_changed();
  928. }
  929. Node::ProcessThreadGroup Node::get_process_thread_group() const {
  930. return data.process_thread_group;
  931. }
  932. void Node::set_process_thread_messages(BitField<ProcessThreadMessages> p_flags) {
  933. ERR_THREAD_GUARD
  934. if (data.process_thread_messages == p_flags) {
  935. return;
  936. }
  937. data.process_thread_messages = p_flags;
  938. }
  939. BitField<Node::ProcessThreadMessages> Node::get_process_thread_messages() const {
  940. return data.process_thread_messages;
  941. }
  942. void Node::set_process_input(bool p_enable) {
  943. ERR_THREAD_GUARD
  944. if (p_enable == data.input) {
  945. return;
  946. }
  947. data.input = p_enable;
  948. if (!is_inside_tree()) {
  949. return;
  950. }
  951. if (p_enable) {
  952. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  953. } else {
  954. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  955. }
  956. }
  957. bool Node::is_processing_input() const {
  958. return data.input;
  959. }
  960. void Node::set_process_shortcut_input(bool p_enable) {
  961. ERR_THREAD_GUARD
  962. if (p_enable == data.shortcut_input) {
  963. return;
  964. }
  965. data.shortcut_input = p_enable;
  966. if (!is_inside_tree()) {
  967. return;
  968. }
  969. if (p_enable) {
  970. add_to_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  971. } else {
  972. remove_from_group("_vp_shortcut_input" + itos(get_viewport()->get_instance_id()));
  973. }
  974. }
  975. bool Node::is_processing_shortcut_input() const {
  976. return data.shortcut_input;
  977. }
  978. void Node::set_process_unhandled_input(bool p_enable) {
  979. ERR_THREAD_GUARD
  980. if (p_enable == data.unhandled_input) {
  981. return;
  982. }
  983. data.unhandled_input = p_enable;
  984. if (!is_inside_tree()) {
  985. return;
  986. }
  987. if (p_enable) {
  988. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  989. } else {
  990. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  991. }
  992. }
  993. bool Node::is_processing_unhandled_input() const {
  994. return data.unhandled_input;
  995. }
  996. void Node::set_process_unhandled_key_input(bool p_enable) {
  997. ERR_THREAD_GUARD
  998. if (p_enable == data.unhandled_key_input) {
  999. return;
  1000. }
  1001. data.unhandled_key_input = p_enable;
  1002. if (!is_inside_tree()) {
  1003. return;
  1004. }
  1005. if (p_enable) {
  1006. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  1007. } else {
  1008. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  1009. }
  1010. }
  1011. bool Node::is_processing_unhandled_key_input() const {
  1012. return data.unhandled_key_input;
  1013. }
  1014. void Node::set_auto_translate_mode(AutoTranslateMode p_mode) {
  1015. ERR_THREAD_GUARD
  1016. if (data.auto_translate_mode == p_mode) {
  1017. return;
  1018. }
  1019. if (p_mode == AUTO_TRANSLATE_MODE_INHERIT && data.inside_tree && !data.parent) {
  1020. ERR_FAIL_MSG("The root node can't be set to Inherit auto translate mode.");
  1021. }
  1022. data.auto_translate_mode = p_mode;
  1023. data.is_auto_translating = p_mode != AUTO_TRANSLATE_MODE_DISABLED;
  1024. data.is_auto_translate_dirty = true;
  1025. propagate_notification(NOTIFICATION_TRANSLATION_CHANGED);
  1026. }
  1027. Node::AutoTranslateMode Node::get_auto_translate_mode() const {
  1028. return data.auto_translate_mode;
  1029. }
  1030. bool Node::can_auto_translate() const {
  1031. ERR_READ_THREAD_GUARD_V(false);
  1032. if (!data.is_auto_translate_dirty || data.auto_translate_mode != AUTO_TRANSLATE_MODE_INHERIT) {
  1033. return data.is_auto_translating;
  1034. }
  1035. data.is_auto_translate_dirty = false;
  1036. Node *parent = data.parent;
  1037. while (parent) {
  1038. if (parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT) {
  1039. parent = parent->data.parent;
  1040. continue;
  1041. }
  1042. data.is_auto_translating = parent->data.auto_translate_mode == AUTO_TRANSLATE_MODE_ALWAYS;
  1043. break;
  1044. }
  1045. return data.is_auto_translating;
  1046. }
  1047. StringName Node::get_name() const {
  1048. return data.name;
  1049. }
  1050. void Node::_set_name_nocheck(const StringName &p_name) {
  1051. data.name = p_name;
  1052. }
  1053. void Node::set_name(const String &p_name) {
  1054. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Changing the name to nodes inside the SceneTree is only allowed from the main thread. Use `set_name.call_deferred(new_name)`.");
  1055. String name = p_name.validate_node_name();
  1056. ERR_FAIL_COND(name.is_empty());
  1057. if (data.unique_name_in_owner && data.owner) {
  1058. _release_unique_name_in_owner();
  1059. }
  1060. String old_name = data.name;
  1061. data.name = name;
  1062. if (data.parent) {
  1063. data.parent->_validate_child_name(this, true);
  1064. bool success = data.parent->data.children.replace_key(old_name, data.name);
  1065. ERR_FAIL_COND_MSG(!success, "Renaming child in hashtable failed, this is a bug.");
  1066. }
  1067. if (data.unique_name_in_owner && data.owner) {
  1068. _acquire_unique_name_in_owner();
  1069. }
  1070. propagate_notification(NOTIFICATION_PATH_RENAMED);
  1071. if (is_inside_tree()) {
  1072. emit_signal(SNAME("renamed"));
  1073. get_tree()->node_renamed(this);
  1074. get_tree()->tree_changed();
  1075. }
  1076. }
  1077. // Returns a clear description of this node depending on what is available. Useful for error messages.
  1078. String Node::get_description() const {
  1079. String description;
  1080. if (is_inside_tree()) {
  1081. description = get_path();
  1082. } else {
  1083. description = get_name();
  1084. if (description.is_empty()) {
  1085. description = get_class();
  1086. }
  1087. }
  1088. return description;
  1089. }
  1090. static SafeRefCount node_hrcr_count;
  1091. void Node::init_node_hrcr() {
  1092. node_hrcr_count.init(1);
  1093. }
  1094. #ifdef TOOLS_ENABLED
  1095. String Node::validate_child_name(Node *p_child) {
  1096. StringName name = p_child->data.name;
  1097. _generate_serial_child_name(p_child, name);
  1098. return name;
  1099. }
  1100. String Node::prevalidate_child_name(Node *p_child, StringName p_name) {
  1101. _generate_serial_child_name(p_child, p_name);
  1102. return p_name;
  1103. }
  1104. #endif
  1105. String Node::adjust_name_casing(const String &p_name) {
  1106. switch (GLOBAL_GET("editor/naming/node_name_casing").operator int()) {
  1107. case NAME_CASING_PASCAL_CASE:
  1108. return p_name.to_pascal_case();
  1109. case NAME_CASING_CAMEL_CASE:
  1110. return p_name.to_camel_case();
  1111. case NAME_CASING_SNAKE_CASE:
  1112. return p_name.to_snake_case();
  1113. }
  1114. return p_name;
  1115. }
  1116. void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
  1117. /* Make sure the name is unique */
  1118. if (p_force_human_readable) {
  1119. //this approach to autoset node names is human readable but very slow
  1120. StringName name = p_child->data.name;
  1121. _generate_serial_child_name(p_child, name);
  1122. p_child->data.name = name;
  1123. } else {
  1124. //this approach to autoset node names is fast but not as readable
  1125. //it's the default and reserves the '@' character for unique names.
  1126. bool unique = true;
  1127. if (p_child->data.name == StringName()) {
  1128. //new unique name must be assigned
  1129. unique = false;
  1130. } else {
  1131. const Node *const *existing = data.children.getptr(p_child->data.name);
  1132. unique = !existing || *existing == p_child;
  1133. }
  1134. if (!unique) {
  1135. ERR_FAIL_COND(!node_hrcr_count.ref());
  1136. // Optimized version of the code below:
  1137. // String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
  1138. uint32_t c = node_hrcr_count.get();
  1139. String cn = p_child->get_class_name().operator String();
  1140. const char32_t *cn_ptr = cn.ptr();
  1141. uint32_t cn_length = cn.length();
  1142. uint32_t c_chars = String::num_characters(c);
  1143. uint32_t len = 2 + cn_length + c_chars;
  1144. char32_t *str = (char32_t *)alloca(sizeof(char32_t) * (len + 1));
  1145. uint32_t idx = 0;
  1146. str[idx++] = '@';
  1147. for (uint32_t i = 0; i < cn_length; i++) {
  1148. str[idx++] = cn_ptr[i];
  1149. }
  1150. str[idx++] = '@';
  1151. idx += c_chars;
  1152. ERR_FAIL_COND(idx != len);
  1153. str[idx] = 0;
  1154. while (c) {
  1155. str[--idx] = '0' + (c % 10);
  1156. c /= 10;
  1157. }
  1158. p_child->data.name = String(str);
  1159. }
  1160. }
  1161. }
  1162. // Return s + 1 as if it were an integer
  1163. String increase_numeric_string(const String &s) {
  1164. String res = s;
  1165. bool carry = res.length() > 0;
  1166. for (int i = res.length() - 1; i >= 0; i--) {
  1167. if (!carry) {
  1168. break;
  1169. }
  1170. char32_t n = s[i];
  1171. if (n == '9') { // keep carry as true: 9 + 1
  1172. res[i] = '0';
  1173. } else {
  1174. res[i] = s[i] + 1;
  1175. carry = false;
  1176. }
  1177. }
  1178. if (carry) {
  1179. res = "1" + res;
  1180. }
  1181. return res;
  1182. }
  1183. void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const {
  1184. if (name == StringName()) {
  1185. // No name and a new name is needed, create one.
  1186. name = p_child->get_class();
  1187. }
  1188. const Node *const *existing = data.children.getptr(name);
  1189. if (!existing || *existing == p_child) { // Unused, or is current node.
  1190. return;
  1191. }
  1192. // Extract trailing number
  1193. String name_string = name;
  1194. String nums;
  1195. for (int i = name_string.length() - 1; i >= 0; i--) {
  1196. char32_t n = name_string[i];
  1197. if (is_digit(n)) {
  1198. nums = String::chr(name_string[i]) + nums;
  1199. } else {
  1200. break;
  1201. }
  1202. }
  1203. String nnsep = _get_name_num_separator();
  1204. int name_last_index = name_string.length() - nnsep.length() - nums.length();
  1205. // Assign the base name + separator to name if we have numbers preceded by a separator
  1206. if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) {
  1207. name_string = name_string.substr(0, name_last_index + nnsep.length());
  1208. } else {
  1209. nums = "";
  1210. }
  1211. for (;;) {
  1212. StringName attempt = name_string + nums;
  1213. existing = data.children.getptr(attempt);
  1214. bool exists = existing != nullptr && *existing != p_child;
  1215. if (!exists) {
  1216. name = attempt;
  1217. return;
  1218. } else {
  1219. if (nums.length() == 0) {
  1220. // Name was undecorated so skip to 2 for a more natural result
  1221. nums = "2";
  1222. name_string += nnsep; // Add separator because nums.length() > 0 was false
  1223. } else {
  1224. nums = increase_numeric_string(nums);
  1225. }
  1226. }
  1227. }
  1228. }
  1229. Node::InternalMode Node::get_internal_mode() const {
  1230. return data.internal_mode;
  1231. }
  1232. void Node::_add_child_nocheck(Node *p_child, const StringName &p_name, InternalMode p_internal_mode) {
  1233. //add a child node quickly, without name validation
  1234. p_child->data.name = p_name;
  1235. data.children.insert(p_name, p_child);
  1236. p_child->data.internal_mode = p_internal_mode;
  1237. switch (p_internal_mode) {
  1238. case INTERNAL_MODE_FRONT: {
  1239. p_child->data.index = data.internal_children_front_count_cache++;
  1240. } break;
  1241. case INTERNAL_MODE_BACK: {
  1242. p_child->data.index = data.internal_children_back_count_cache++;
  1243. } break;
  1244. case INTERNAL_MODE_DISABLED: {
  1245. p_child->data.index = data.external_children_count_cache++;
  1246. } break;
  1247. }
  1248. p_child->data.parent = this;
  1249. if (!data.children_cache_dirty && p_internal_mode == INTERNAL_MODE_DISABLED && data.internal_children_back_count_cache == 0) {
  1250. // Special case, also add to the cached children array since its cheap.
  1251. data.children_cache.push_back(p_child);
  1252. } else {
  1253. data.children_cache_dirty = true;
  1254. }
  1255. p_child->notification(NOTIFICATION_PARENTED);
  1256. if (data.tree) {
  1257. p_child->_set_tree(data.tree);
  1258. }
  1259. /* Notify */
  1260. //recognize children created in this node constructor
  1261. p_child->data.parent_owned = data.in_constructor;
  1262. add_child_notify(p_child);
  1263. notification(NOTIFICATION_CHILD_ORDER_CHANGED);
  1264. emit_signal(SNAME("child_order_changed"));
  1265. }
  1266. void Node::add_child(Node *p_child, bool p_force_readable_name, InternalMode p_internal) {
  1267. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Adding children to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_child\",node).");
  1268. ERR_THREAD_GUARD
  1269. ERR_FAIL_NULL(p_child);
  1270. ERR_FAIL_COND_MSG(p_child == this, vformat("Can't add child '%s' to itself.", p_child->get_name())); // adding to itself!
  1271. 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
  1272. #ifdef DEBUG_ENABLED
  1273. 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()));
  1274. #endif
  1275. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, `add_child()` failed. Consider using `add_child.call_deferred(child)` instead.");
  1276. _validate_child_name(p_child, p_force_readable_name);
  1277. #ifdef DEBUG_ENABLED
  1278. if (p_child->data.owner && !p_child->data.owner->is_ancestor_of(p_child)) {
  1279. // Owner of p_child should be ancestor of p_child.
  1280. WARN_PRINT(vformat("Adding '%s' as child to '%s' will make owner '%s' inconsistent. Consider unsetting the owner beforehand.", p_child->get_name(), get_name(), p_child->data.owner->get_name()));
  1281. }
  1282. #endif // DEBUG_ENABLED
  1283. _add_child_nocheck(p_child, p_child->data.name, p_internal);
  1284. }
  1285. void Node::add_sibling(Node *p_sibling, bool p_force_readable_name) {
  1286. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Adding a sibling to a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"add_sibling\",node).");
  1287. ERR_FAIL_NULL(p_sibling);
  1288. ERR_FAIL_COND_MSG(p_sibling == this, vformat("Can't add sibling '%s' to itself.", p_sibling->get_name())); // adding to itself!
  1289. ERR_FAIL_NULL(data.parent);
  1290. ERR_FAIL_COND_MSG(data.parent->data.blocked > 0, "Parent node is busy setting up children, `add_sibling()` failed. Consider using `add_sibling.call_deferred(sibling)` instead.");
  1291. data.parent->add_child(p_sibling, p_force_readable_name, data.internal_mode);
  1292. data.parent->_update_children_cache();
  1293. data.parent->_move_child(p_sibling, get_index() + 1);
  1294. }
  1295. void Node::remove_child(Node *p_child) {
  1296. ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Removing children from a node inside the SceneTree is only allowed from the main thread. Use call_deferred(\"remove_child\",node).");
  1297. ERR_FAIL_NULL(p_child);
  1298. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy adding/removing children, `remove_child()` can't be called at this time. Consider using `remove_child.call_deferred(child)` instead.");
  1299. ERR_FAIL_COND(p_child->data.parent != this);
  1300. /**
  1301. * Do not change the data.internal_children*cache counters here.
  1302. * Because if nodes are re-added, the indices can remain
  1303. * greater-than-everything indices and children added remain
  1304. * properly ordered.
  1305. *
  1306. * All children indices and counters will be updated next time the
  1307. * cache is re-generated.
  1308. */
  1309. data.blocked++;
  1310. p_child->_set_tree(nullptr);
  1311. //}
  1312. remove_child_notify(p_child);
  1313. p_child->notification(NOTIFICATION_UNPARENTED);
  1314. data.blocked--;
  1315. data.children_cache_dirty = true;
  1316. bool success = data.children.erase(p_child->data.name);
  1317. ERR_FAIL_COND_MSG(!success, "Children name does not match parent name in hashtable, this is a bug.");
  1318. p_child->data.parent = nullptr;
  1319. p_child->data.index = -1;
  1320. notification(NOTIFICATION_CHILD_ORDER_CHANGED);
  1321. emit_signal(SNAME("child_order_changed"));
  1322. if (data.inside_tree) {
  1323. p_child->_propagate_after_exit_tree();
  1324. }
  1325. }
  1326. void Node::_update_children_cache_impl() const {
  1327. // Assign children
  1328. data.children_cache.resize(data.children.size());
  1329. int idx = 0;
  1330. for (const KeyValue<StringName, Node *> &K : data.children) {
  1331. data.children_cache[idx] = K.value;
  1332. idx++;
  1333. }
  1334. // Sort them
  1335. data.children_cache.sort_custom<ComparatorByIndex>();
  1336. // Update indices
  1337. data.external_children_count_cache = 0;
  1338. data.internal_children_back_count_cache = 0;
  1339. data.internal_children_front_count_cache = 0;
  1340. for (uint32_t i = 0; i < data.children_cache.size(); i++) {
  1341. switch (data.children_cache[i]->data.internal_mode) {
  1342. case INTERNAL_MODE_DISABLED: {
  1343. data.children_cache[i]->data.index = data.external_children_count_cache++;
  1344. } break;
  1345. case INTERNAL_MODE_FRONT: {
  1346. data.children_cache[i]->data.index = data.internal_children_front_count_cache++;
  1347. } break;
  1348. case INTERNAL_MODE_BACK: {
  1349. data.children_cache[i]->data.index = data.internal_children_back_count_cache++;
  1350. } break;
  1351. }
  1352. }
  1353. data.children_cache_dirty = false;
  1354. }
  1355. int Node::get_child_count(bool p_include_internal) const {
  1356. ERR_THREAD_GUARD_V(0);
  1357. _update_children_cache();
  1358. if (p_include_internal) {
  1359. return data.children_cache.size();
  1360. } else {
  1361. return data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
  1362. }
  1363. }
  1364. Node *Node::get_child(int p_index, bool p_include_internal) const {
  1365. ERR_THREAD_GUARD_V(nullptr);
  1366. _update_children_cache();
  1367. if (p_include_internal) {
  1368. if (p_index < 0) {
  1369. p_index += data.children_cache.size();
  1370. }
  1371. ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size(), nullptr);
  1372. return data.children_cache[p_index];
  1373. } else {
  1374. if (p_index < 0) {
  1375. p_index += (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache;
  1376. }
  1377. ERR_FAIL_INDEX_V(p_index, (int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache, nullptr);
  1378. p_index += data.internal_children_front_count_cache;
  1379. return data.children_cache[p_index];
  1380. }
  1381. }
  1382. TypedArray<Node> Node::get_children(bool p_include_internal) const {
  1383. ERR_THREAD_GUARD_V(TypedArray<Node>());
  1384. TypedArray<Node> arr;
  1385. int cc = get_child_count(p_include_internal);
  1386. arr.resize(cc);
  1387. for (int i = 0; i < cc; i++) {
  1388. arr[i] = get_child(i, p_include_internal);
  1389. }
  1390. return arr;
  1391. }
  1392. Node *Node::_get_child_by_name(const StringName &p_name) const {
  1393. const Node *const *node = data.children.getptr(p_name);
  1394. if (node) {
  1395. return const_cast<Node *>(*node);
  1396. } else {
  1397. return nullptr;
  1398. }
  1399. }
  1400. Node *Node::get_node_or_null(const NodePath &p_path) const {
  1401. ERR_THREAD_GUARD_V(nullptr);
  1402. if (p_path.is_empty()) {
  1403. return nullptr;
  1404. }
  1405. 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.");
  1406. Node *current = nullptr;
  1407. Node *root = nullptr;
  1408. if (!p_path.is_absolute()) {
  1409. current = const_cast<Node *>(this); //start from this
  1410. } else {
  1411. root = const_cast<Node *>(this);
  1412. while (root->data.parent) {
  1413. root = root->data.parent; //start from root
  1414. }
  1415. }
  1416. for (int i = 0; i < p_path.get_name_count(); i++) {
  1417. StringName name = p_path.get_name(i);
  1418. Node *next = nullptr;
  1419. if (name == SNAME(".")) {
  1420. next = current;
  1421. } else if (name == SNAME("..")) {
  1422. if (current == nullptr || !current->data.parent) {
  1423. return nullptr;
  1424. }
  1425. next = current->data.parent;
  1426. } else if (current == nullptr) {
  1427. if (name == root->get_name()) {
  1428. next = root;
  1429. }
  1430. } else if (name.is_node_unique_name()) {
  1431. Node **unique = current->data.owned_unique_nodes.getptr(name);
  1432. if (!unique && current->data.owner) {
  1433. unique = current->data.owner->data.owned_unique_nodes.getptr(name);
  1434. }
  1435. if (!unique) {
  1436. return nullptr;
  1437. }
  1438. next = *unique;
  1439. } else {
  1440. next = nullptr;
  1441. const Node *const *node = current->data.children.getptr(name);
  1442. if (node) {
  1443. next = const_cast<Node *>(*node);
  1444. } else {
  1445. return nullptr;
  1446. }
  1447. }
  1448. current = next;
  1449. }
  1450. return current;
  1451. }
  1452. Node *Node::get_node(const NodePath &p_path) const {
  1453. Node *node = get_node_or_null(p_path);
  1454. if (unlikely(!node)) {
  1455. const String desc = get_description();
  1456. if (p_path.is_absolute()) {
  1457. ERR_FAIL_V_MSG(nullptr,
  1458. vformat(R"(Node not found: "%s" (absolute path attempted from "%s").)", p_path, desc));
  1459. } else {
  1460. ERR_FAIL_V_MSG(nullptr,
  1461. vformat(R"(Node not found: "%s" (relative to "%s").)", p_path, desc));
  1462. }
  1463. }
  1464. return node;
  1465. }
  1466. bool Node::has_node(const NodePath &p_path) const {
  1467. return get_node_or_null(p_path) != nullptr;
  1468. }
  1469. // Finds the first child node (in tree order) whose name matches the given pattern.
  1470. // Can be recursive or not, and limited to owned nodes.
  1471. Node *Node::find_child(const String &p_pattern, bool p_recursive, bool p_owned) const {
  1472. ERR_THREAD_GUARD_V(nullptr);
  1473. ERR_FAIL_COND_V(p_pattern.is_empty(), nullptr);
  1474. _update_children_cache();
  1475. Node *const *cptr = data.children_cache.ptr();
  1476. int ccount = data.children_cache.size();
  1477. for (int i = 0; i < ccount; i++) {
  1478. if (p_owned && !cptr[i]->data.owner) {
  1479. continue;
  1480. }
  1481. if (cptr[i]->data.name.operator String().match(p_pattern)) {
  1482. return cptr[i];
  1483. }
  1484. if (!p_recursive) {
  1485. continue;
  1486. }
  1487. Node *ret = cptr[i]->find_child(p_pattern, true, p_owned);
  1488. if (ret) {
  1489. return ret;
  1490. }
  1491. }
  1492. return nullptr;
  1493. }
  1494. // Finds child nodes based on their name using pattern matching, or class name,
  1495. // or both (either pattern or type can be left empty).
  1496. // Can be recursive or not, and limited to owned nodes.
  1497. TypedArray<Node> Node::find_children(const String &p_pattern, const String &p_type, bool p_recursive, bool p_owned) const {
  1498. ERR_THREAD_GUARD_V(TypedArray<Node>());
  1499. TypedArray<Node> ret;
  1500. ERR_FAIL_COND_V(p_pattern.is_empty() && p_type.is_empty(), ret);
  1501. _update_children_cache();
  1502. Node *const *cptr = data.children_cache.ptr();
  1503. int ccount = data.children_cache.size();
  1504. for (int i = 0; i < ccount; i++) {
  1505. if (p_owned && !cptr[i]->data.owner) {
  1506. continue;
  1507. }
  1508. if (p_pattern.is_empty() || cptr[i]->data.name.operator String().match(p_pattern)) {
  1509. if (p_type.is_empty() || cptr[i]->is_class(p_type)) {
  1510. ret.append(cptr[i]);
  1511. } else if (cptr[i]->get_script_instance()) {
  1512. Ref<Script> scr = cptr[i]->get_script_instance()->get_script();
  1513. while (scr.is_valid()) {
  1514. if ((ScriptServer::is_global_class(p_type) && ScriptServer::get_global_class_path(p_type) == scr->get_path()) || p_type == scr->get_path()) {
  1515. ret.append(cptr[i]);
  1516. break;
  1517. }
  1518. scr = scr->get_base_script();
  1519. }
  1520. }
  1521. }
  1522. if (p_recursive) {
  1523. ret.append_array(cptr[i]->find_children(p_pattern, p_type, true, p_owned));
  1524. }
  1525. }
  1526. return ret;
  1527. }
  1528. void Node::reparent(Node *p_parent, bool p_keep_global_transform) {
  1529. ERR_THREAD_GUARD
  1530. ERR_FAIL_NULL(p_parent);
  1531. ERR_FAIL_NULL_MSG(data.parent, "Node needs a parent to be reparented.");
  1532. if (p_parent == data.parent) {
  1533. return;
  1534. }
  1535. bool preserve_owner = data.owner && (data.owner == p_parent || data.owner->is_ancestor_of(p_parent));
  1536. Node *owner_temp = data.owner;
  1537. LocalVector<Node *> common_parents;
  1538. // If the new parent is related to the owner, find all children of the reparented node who have the same owner so that we can reassign them.
  1539. if (preserve_owner) {
  1540. LocalVector<Node *> to_visit;
  1541. to_visit.push_back(this);
  1542. common_parents.push_back(this);
  1543. while (to_visit.size() > 0) {
  1544. Node *check = to_visit[to_visit.size() - 1];
  1545. to_visit.resize(to_visit.size() - 1);
  1546. for (int i = 0; i < check->get_child_count(false); i++) {
  1547. Node *child = check->get_child(i, false);
  1548. to_visit.push_back(child);
  1549. if (child->data.owner == owner_temp) {
  1550. common_parents.push_back(child);
  1551. }
  1552. }
  1553. }
  1554. }
  1555. data.parent->remove_child(this);
  1556. p_parent->add_child(this);
  1557. // Reassign the old owner to those found nodes.
  1558. if (preserve_owner) {
  1559. for (Node *E : common_parents) {
  1560. E->set_owner(owner_temp);
  1561. }
  1562. }
  1563. }
  1564. Node *Node::get_parent() const {
  1565. return data.parent;
  1566. }
  1567. Node *Node::find_parent(const String &p_pattern) const {
  1568. ERR_THREAD_GUARD_V(nullptr);
  1569. Node *p = data.parent;
  1570. while (p) {
  1571. if (p->data.name.operator String().match(p_pattern)) {
  1572. return p;
  1573. }
  1574. p = p->data.parent;
  1575. }
  1576. return nullptr;
  1577. }
  1578. Window *Node::get_window() const {
  1579. ERR_THREAD_GUARD_V(nullptr);
  1580. Viewport *vp = get_viewport();
  1581. if (vp) {
  1582. return vp->get_base_window();
  1583. }
  1584. return nullptr;
  1585. }
  1586. Window *Node::get_last_exclusive_window() const {
  1587. Window *w = get_window();
  1588. while (w && w->get_exclusive_child()) {
  1589. w = w->get_exclusive_child();
  1590. }
  1591. return w;
  1592. }
  1593. bool Node::is_ancestor_of(const Node *p_node) const {
  1594. ERR_FAIL_NULL_V(p_node, false);
  1595. Node *p = p_node->data.parent;
  1596. while (p) {
  1597. if (p == this) {
  1598. return true;
  1599. }
  1600. p = p->data.parent;
  1601. }
  1602. return false;
  1603. }
  1604. bool Node::is_greater_than(const Node *p_node) const {
  1605. ERR_FAIL_NULL_V(p_node, false);
  1606. ERR_FAIL_COND_V(!data.inside_tree, false);
  1607. ERR_FAIL_COND_V(!p_node->data.inside_tree, false);
  1608. ERR_FAIL_COND_V(data.depth < 0, false);
  1609. ERR_FAIL_COND_V(p_node->data.depth < 0, false);
  1610. _update_children_cache();
  1611. int *this_stack = (int *)alloca(sizeof(int) * data.depth);
  1612. int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
  1613. const Node *n = this;
  1614. int idx = data.depth - 1;
  1615. while (n) {
  1616. ERR_FAIL_INDEX_V(idx, data.depth, false);
  1617. this_stack[idx--] = n->get_index();
  1618. n = n->data.parent;
  1619. }
  1620. ERR_FAIL_COND_V(idx != -1, false);
  1621. n = p_node;
  1622. idx = p_node->data.depth - 1;
  1623. while (n) {
  1624. ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
  1625. that_stack[idx--] = n->get_index();
  1626. n = n->data.parent;
  1627. }
  1628. ERR_FAIL_COND_V(idx != -1, false);
  1629. idx = 0;
  1630. bool res;
  1631. while (true) {
  1632. // using -2 since out-of-tree or nonroot nodes have -1
  1633. int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
  1634. int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
  1635. if (this_idx > that_idx) {
  1636. res = true;
  1637. break;
  1638. } else if (this_idx < that_idx) {
  1639. res = false;
  1640. break;
  1641. } else if (this_idx == -2) {
  1642. res = false; // equal
  1643. break;
  1644. }
  1645. idx++;
  1646. }
  1647. return res;
  1648. }
  1649. void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
  1650. if (data.owner == p_by) {
  1651. p_owned->push_back(this);
  1652. }
  1653. for (KeyValue<StringName, Node *> &K : data.children) {
  1654. K.value->get_owned_by(p_by, p_owned);
  1655. }
  1656. }
  1657. void Node::_set_owner_nocheck(Node *p_owner) {
  1658. if (data.owner == p_owner) {
  1659. return;
  1660. }
  1661. ERR_FAIL_COND(data.owner);
  1662. data.owner = p_owner;
  1663. data.owner->data.owned.push_back(this);
  1664. data.OW = data.owner->data.owned.back();
  1665. owner_changed_notify();
  1666. }
  1667. void Node::_release_unique_name_in_owner() {
  1668. ERR_FAIL_NULL(data.owner); // Safety check.
  1669. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1670. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1671. if (which == nullptr || *which != this) {
  1672. return; // Ignore.
  1673. }
  1674. data.owner->data.owned_unique_nodes.erase(key);
  1675. }
  1676. void Node::_acquire_unique_name_in_owner() {
  1677. ERR_FAIL_NULL(data.owner); // Safety check.
  1678. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1679. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1680. if (which != nullptr && *which != this) {
  1681. String which_path = is_inside_tree() ? (*which)->get_path() : data.owner->get_path_to(*which);
  1682. WARN_PRINT(vformat("Setting node name '%s' to be unique within scene for '%s', but it's already claimed by '%s'.\n'%s' is no longer set as having a unique name.",
  1683. get_name(), is_inside_tree() ? get_path() : data.owner->get_path_to(this), which_path, which_path));
  1684. data.unique_name_in_owner = false;
  1685. return;
  1686. }
  1687. data.owner->data.owned_unique_nodes[key] = this;
  1688. }
  1689. void Node::set_unique_name_in_owner(bool p_enabled) {
  1690. ERR_MAIN_THREAD_GUARD
  1691. if (data.unique_name_in_owner == p_enabled) {
  1692. return;
  1693. }
  1694. if (data.unique_name_in_owner && data.owner != nullptr) {
  1695. _release_unique_name_in_owner();
  1696. }
  1697. data.unique_name_in_owner = p_enabled;
  1698. if (data.unique_name_in_owner && data.owner != nullptr) {
  1699. _acquire_unique_name_in_owner();
  1700. }
  1701. update_configuration_warnings();
  1702. }
  1703. bool Node::is_unique_name_in_owner() const {
  1704. return data.unique_name_in_owner;
  1705. }
  1706. void Node::set_owner(Node *p_owner) {
  1707. ERR_MAIN_THREAD_GUARD
  1708. if (data.owner) {
  1709. _clean_up_owner();
  1710. }
  1711. ERR_FAIL_COND(p_owner == this);
  1712. if (!p_owner) {
  1713. return;
  1714. }
  1715. Node *check = get_parent();
  1716. bool owner_valid = false;
  1717. while (check) {
  1718. if (check == p_owner) {
  1719. owner_valid = true;
  1720. break;
  1721. }
  1722. check = check->data.parent;
  1723. }
  1724. ERR_FAIL_COND_MSG(!owner_valid, "Invalid owner. Owner must be an ancestor in the tree.");
  1725. _set_owner_nocheck(p_owner);
  1726. if (data.unique_name_in_owner) {
  1727. _acquire_unique_name_in_owner();
  1728. }
  1729. }
  1730. Node *Node::get_owner() const {
  1731. return data.owner;
  1732. }
  1733. void Node::_clean_up_owner() {
  1734. ERR_FAIL_NULL(data.owner); // Safety check.
  1735. if (data.unique_name_in_owner) {
  1736. _release_unique_name_in_owner();
  1737. }
  1738. data.owner->data.owned.erase(data.OW);
  1739. data.owner = nullptr;
  1740. data.OW = nullptr;
  1741. }
  1742. Node *Node::find_common_parent_with(const Node *p_node) const {
  1743. if (this == p_node) {
  1744. return const_cast<Node *>(p_node);
  1745. }
  1746. HashSet<const Node *> visited;
  1747. const Node *n = this;
  1748. while (n) {
  1749. visited.insert(n);
  1750. n = n->data.parent;
  1751. }
  1752. const Node *common_parent = p_node;
  1753. while (common_parent) {
  1754. if (visited.has(common_parent)) {
  1755. break;
  1756. }
  1757. common_parent = common_parent->data.parent;
  1758. }
  1759. if (!common_parent) {
  1760. return nullptr;
  1761. }
  1762. return const_cast<Node *>(common_parent);
  1763. }
  1764. NodePath Node::get_path_to(const Node *p_node, bool p_use_unique_path) const {
  1765. ERR_FAIL_NULL_V(p_node, NodePath());
  1766. if (this == p_node) {
  1767. return NodePath(".");
  1768. }
  1769. HashSet<const Node *> visited;
  1770. const Node *n = this;
  1771. while (n) {
  1772. visited.insert(n);
  1773. n = n->data.parent;
  1774. }
  1775. const Node *common_parent = p_node;
  1776. while (common_parent) {
  1777. if (visited.has(common_parent)) {
  1778. break;
  1779. }
  1780. common_parent = common_parent->data.parent;
  1781. }
  1782. ERR_FAIL_NULL_V(common_parent, NodePath()); //nodes not in the same tree
  1783. visited.clear();
  1784. Vector<StringName> path;
  1785. StringName up = String("..");
  1786. if (p_use_unique_path) {
  1787. n = p_node;
  1788. bool is_detected = false;
  1789. while (n != common_parent) {
  1790. if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
  1791. path.push_back(UNIQUE_NODE_PREFIX + String(n->get_name()));
  1792. is_detected = true;
  1793. break;
  1794. }
  1795. path.push_back(n->get_name());
  1796. n = n->data.parent;
  1797. }
  1798. if (!is_detected) {
  1799. n = this;
  1800. String detected_name;
  1801. int up_count = 0;
  1802. while (n != common_parent) {
  1803. if (n->is_unique_name_in_owner() && n->get_owner() == get_owner()) {
  1804. detected_name = n->get_name();
  1805. up_count = 0;
  1806. }
  1807. up_count++;
  1808. n = n->data.parent;
  1809. }
  1810. for (int i = 0; i < up_count; i++) {
  1811. path.push_back(up);
  1812. }
  1813. if (!detected_name.is_empty()) {
  1814. path.push_back(UNIQUE_NODE_PREFIX + detected_name);
  1815. }
  1816. }
  1817. } else {
  1818. n = p_node;
  1819. while (n != common_parent) {
  1820. path.push_back(n->get_name());
  1821. n = n->data.parent;
  1822. }
  1823. n = this;
  1824. while (n != common_parent) {
  1825. path.push_back(up);
  1826. n = n->data.parent;
  1827. }
  1828. }
  1829. path.reverse();
  1830. return NodePath(path, false);
  1831. }
  1832. NodePath Node::get_path() const {
  1833. ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
  1834. if (data.path_cache) {
  1835. return *data.path_cache;
  1836. }
  1837. const Node *n = this;
  1838. Vector<StringName> path;
  1839. while (n) {
  1840. path.push_back(n->get_name());
  1841. n = n->data.parent;
  1842. }
  1843. path.reverse();
  1844. data.path_cache = memnew(NodePath(path, true));
  1845. return *data.path_cache;
  1846. }
  1847. bool Node::is_in_group(const StringName &p_identifier) const {
  1848. ERR_THREAD_GUARD_V(false);
  1849. return data.grouped.has(p_identifier);
  1850. }
  1851. void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
  1852. ERR_THREAD_GUARD
  1853. ERR_FAIL_COND(!p_identifier.operator String().length());
  1854. if (data.grouped.has(p_identifier)) {
  1855. return;
  1856. }
  1857. GroupData gd;
  1858. if (data.tree) {
  1859. gd.group = data.tree->add_to_group(p_identifier, this);
  1860. } else {
  1861. gd.group = nullptr;
  1862. }
  1863. gd.persistent = p_persistent;
  1864. data.grouped[p_identifier] = gd;
  1865. }
  1866. void Node::remove_from_group(const StringName &p_identifier) {
  1867. ERR_THREAD_GUARD
  1868. HashMap<StringName, GroupData>::Iterator E = data.grouped.find(p_identifier);
  1869. if (!E) {
  1870. return;
  1871. }
  1872. if (data.tree) {
  1873. data.tree->remove_from_group(E->key, this);
  1874. }
  1875. data.grouped.remove(E);
  1876. }
  1877. TypedArray<StringName> Node::_get_groups() const {
  1878. TypedArray<StringName> groups;
  1879. List<GroupInfo> gi;
  1880. get_groups(&gi);
  1881. for (const GroupInfo &E : gi) {
  1882. groups.push_back(E.name);
  1883. }
  1884. return groups;
  1885. }
  1886. void Node::get_groups(List<GroupInfo> *p_groups) const {
  1887. ERR_THREAD_GUARD
  1888. for (const KeyValue<StringName, GroupData> &E : data.grouped) {
  1889. GroupInfo gi;
  1890. gi.name = E.key;
  1891. gi.persistent = E.value.persistent;
  1892. p_groups->push_back(gi);
  1893. }
  1894. }
  1895. int Node::get_persistent_group_count() const {
  1896. ERR_THREAD_GUARD_V(0);
  1897. int count = 0;
  1898. for (const KeyValue<StringName, GroupData> &E : data.grouped) {
  1899. if (E.value.persistent) {
  1900. count += 1;
  1901. }
  1902. }
  1903. return count;
  1904. }
  1905. void Node::print_tree_pretty() {
  1906. print_line(_get_tree_string_pretty("", true));
  1907. }
  1908. void Node::print_tree() {
  1909. print_line(_get_tree_string(this));
  1910. }
  1911. String Node::_get_tree_string_pretty(const String &p_prefix, bool p_last) {
  1912. String new_prefix = p_last ? String::utf8(" ┖╴") : String::utf8(" ┠╴");
  1913. _update_children_cache();
  1914. String return_tree = p_prefix + new_prefix + String(get_name()) + "\n";
  1915. for (uint32_t i = 0; i < data.children_cache.size(); i++) {
  1916. new_prefix = p_last ? String::utf8(" ") : String::utf8(" ┃ ");
  1917. return_tree += data.children_cache[i]->_get_tree_string_pretty(p_prefix + new_prefix, i == data.children_cache.size() - 1);
  1918. }
  1919. return return_tree;
  1920. }
  1921. String Node::get_tree_string_pretty() {
  1922. return _get_tree_string_pretty("", true);
  1923. }
  1924. String Node::_get_tree_string(const Node *p_node) {
  1925. _update_children_cache();
  1926. String return_tree = String(p_node->get_path_to(this)) + "\n";
  1927. for (uint32_t i = 0; i < data.children_cache.size(); i++) {
  1928. return_tree += data.children_cache[i]->_get_tree_string(p_node);
  1929. }
  1930. return return_tree;
  1931. }
  1932. String Node::get_tree_string() {
  1933. return _get_tree_string(this);
  1934. }
  1935. void Node::_propagate_reverse_notification(int p_notification) {
  1936. data.blocked++;
  1937. for (HashMap<StringName, Node *>::Iterator I = data.children.last(); I; --I) {
  1938. I->value->_propagate_reverse_notification(p_notification);
  1939. }
  1940. notification(p_notification, true);
  1941. data.blocked--;
  1942. }
  1943. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  1944. ERR_FAIL_COND(!is_inside_tree());
  1945. data.blocked++;
  1946. if (!p_reverse) {
  1947. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1948. }
  1949. for (KeyValue<StringName, Node *> &K : data.children) {
  1950. K.value->_propagate_deferred_notification(p_notification, p_reverse);
  1951. }
  1952. if (p_reverse) {
  1953. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1954. }
  1955. data.blocked--;
  1956. }
  1957. void Node::propagate_notification(int p_notification) {
  1958. ERR_THREAD_GUARD
  1959. data.blocked++;
  1960. notification(p_notification);
  1961. for (KeyValue<StringName, Node *> &K : data.children) {
  1962. K.value->propagate_notification(p_notification);
  1963. }
  1964. data.blocked--;
  1965. }
  1966. void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
  1967. ERR_THREAD_GUARD
  1968. data.blocked++;
  1969. if (p_parent_first && has_method(p_method)) {
  1970. callv(p_method, p_args);
  1971. }
  1972. for (KeyValue<StringName, Node *> &K : data.children) {
  1973. K.value->propagate_call(p_method, p_args, p_parent_first);
  1974. }
  1975. if (!p_parent_first && has_method(p_method)) {
  1976. callv(p_method, p_args);
  1977. }
  1978. data.blocked--;
  1979. }
  1980. void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
  1981. if (get_owner() == p_owner) {
  1982. set_owner(p_by_owner);
  1983. }
  1984. data.blocked++;
  1985. for (KeyValue<StringName, Node *> &K : data.children) {
  1986. K.value->_propagate_replace_owner(p_owner, p_by_owner);
  1987. }
  1988. data.blocked--;
  1989. }
  1990. Ref<Tween> Node::create_tween() {
  1991. ERR_THREAD_GUARD_V(Ref<Tween>());
  1992. SceneTree *tree = data.tree;
  1993. if (!tree) {
  1994. tree = SceneTree::get_singleton();
  1995. }
  1996. ERR_FAIL_NULL_V_MSG(tree, Ref<Tween>(), "No available SceneTree to create the Tween.");
  1997. Ref<Tween> tween = tree->create_tween();
  1998. tween->bind_node(this);
  1999. return tween;
  2000. }
  2001. void Node::set_scene_file_path(const String &p_scene_file_path) {
  2002. ERR_THREAD_GUARD
  2003. data.scene_file_path = p_scene_file_path;
  2004. }
  2005. String Node::get_scene_file_path() const {
  2006. return data.scene_file_path;
  2007. }
  2008. void Node::set_editor_description(const String &p_editor_description) {
  2009. ERR_THREAD_GUARD
  2010. if (data.editor_description == p_editor_description) {
  2011. return;
  2012. }
  2013. data.editor_description = p_editor_description;
  2014. emit_signal(SNAME("editor_description_changed"), this);
  2015. }
  2016. String Node::get_editor_description() const {
  2017. return data.editor_description;
  2018. }
  2019. void Node::set_editable_instance(Node *p_node, bool p_editable) {
  2020. ERR_THREAD_GUARD
  2021. ERR_FAIL_NULL(p_node);
  2022. ERR_FAIL_COND(!is_ancestor_of(p_node));
  2023. if (!p_editable) {
  2024. p_node->data.editable_instance = false;
  2025. // Avoid this flag being needlessly saved;
  2026. // also give more visual feedback if editable children are re-enabled
  2027. set_display_folded(false);
  2028. } else {
  2029. p_node->data.editable_instance = true;
  2030. }
  2031. }
  2032. bool Node::is_editable_instance(const Node *p_node) const {
  2033. if (!p_node) {
  2034. return false; // Easier, null is never editable. :)
  2035. }
  2036. ERR_FAIL_COND_V(!is_ancestor_of(p_node), false);
  2037. return p_node->data.editable_instance;
  2038. }
  2039. Node *Node::get_deepest_editable_node(Node *p_start_node) const {
  2040. ERR_THREAD_GUARD_V(nullptr);
  2041. ERR_FAIL_NULL_V(p_start_node, nullptr);
  2042. ERR_FAIL_COND_V(!is_ancestor_of(p_start_node), p_start_node);
  2043. Node const *iterated_item = p_start_node;
  2044. Node *node = p_start_node;
  2045. while (iterated_item->get_owner() && iterated_item->get_owner() != this) {
  2046. if (!is_editable_instance(iterated_item->get_owner())) {
  2047. node = iterated_item->get_owner();
  2048. }
  2049. iterated_item = iterated_item->get_owner();
  2050. }
  2051. return node;
  2052. }
  2053. #ifdef TOOLS_ENABLED
  2054. void Node::set_property_pinned(const String &p_property, bool p_pinned) {
  2055. ERR_THREAD_GUARD
  2056. bool current_pinned = false;
  2057. Array pinned = get_meta("_edit_pinned_properties_", Array());
  2058. StringName psa = get_property_store_alias(p_property);
  2059. current_pinned = pinned.has(psa);
  2060. if (current_pinned != p_pinned) {
  2061. if (p_pinned) {
  2062. pinned.append(psa);
  2063. } else {
  2064. pinned.erase(psa);
  2065. }
  2066. }
  2067. if (pinned.is_empty()) {
  2068. remove_meta("_edit_pinned_properties_");
  2069. } else {
  2070. set_meta("_edit_pinned_properties_", pinned);
  2071. }
  2072. }
  2073. bool Node::is_property_pinned(const StringName &p_property) const {
  2074. Array pinned = get_meta("_edit_pinned_properties_", Array());
  2075. StringName psa = get_property_store_alias(p_property);
  2076. return pinned.has(psa);
  2077. }
  2078. StringName Node::get_property_store_alias(const StringName &p_property) const {
  2079. return p_property;
  2080. }
  2081. bool Node::is_part_of_edited_scene() const {
  2082. return Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->get_edited_scene_root() &&
  2083. get_tree()->get_edited_scene_root()->get_parent()->is_ancestor_of(this);
  2084. }
  2085. #endif
  2086. void Node::get_storable_properties(HashSet<StringName> &r_storable_properties) const {
  2087. ERR_THREAD_GUARD
  2088. List<PropertyInfo> pi;
  2089. get_property_list(&pi);
  2090. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  2091. if ((E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2092. r_storable_properties.insert(E->get().name);
  2093. }
  2094. }
  2095. }
  2096. String Node::to_string() {
  2097. ERR_THREAD_GUARD_V(String());
  2098. if (get_script_instance()) {
  2099. bool valid;
  2100. String ret = get_script_instance()->to_string(&valid);
  2101. if (valid) {
  2102. return ret;
  2103. }
  2104. }
  2105. return (get_name() ? String(get_name()) + ":" : "") + Object::to_string();
  2106. }
  2107. void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
  2108. ERR_THREAD_GUARD
  2109. data.instance_state = p_state;
  2110. }
  2111. Ref<SceneState> Node::get_scene_instance_state() const {
  2112. return data.instance_state;
  2113. }
  2114. void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
  2115. ERR_THREAD_GUARD
  2116. data.inherited_state = p_state;
  2117. }
  2118. Ref<SceneState> Node::get_scene_inherited_state() const {
  2119. return data.inherited_state;
  2120. }
  2121. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  2122. data.use_placeholder = p_enable;
  2123. }
  2124. bool Node::get_scene_instance_load_placeholder() const {
  2125. return data.use_placeholder;
  2126. }
  2127. Node *Node::_duplicate(int p_flags, HashMap<const Node *, Node *> *r_duplimap) const {
  2128. ERR_THREAD_GUARD_V(nullptr);
  2129. Node *node = nullptr;
  2130. bool instantiated = false;
  2131. if (Object::cast_to<InstancePlaceholder>(this)) {
  2132. const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
  2133. InstancePlaceholder *nip = memnew(InstancePlaceholder);
  2134. nip->set_instance_path(ip->get_instance_path());
  2135. node = nip;
  2136. } else if ((p_flags & DUPLICATE_USE_INSTANTIATION) && !get_scene_file_path().is_empty()) {
  2137. Ref<PackedScene> res = ResourceLoader::load(get_scene_file_path());
  2138. ERR_FAIL_COND_V(res.is_null(), nullptr);
  2139. PackedScene::GenEditState edit_state = PackedScene::GEN_EDIT_STATE_DISABLED;
  2140. #ifdef TOOLS_ENABLED
  2141. if (p_flags & DUPLICATE_FROM_EDITOR) {
  2142. edit_state = PackedScene::GEN_EDIT_STATE_INSTANCE;
  2143. }
  2144. #endif
  2145. node = res->instantiate(edit_state);
  2146. ERR_FAIL_NULL_V(node, nullptr);
  2147. node->set_scene_instance_load_placeholder(get_scene_instance_load_placeholder());
  2148. instantiated = true;
  2149. } else {
  2150. Object *obj = ClassDB::instantiate(get_class());
  2151. ERR_FAIL_NULL_V(obj, nullptr);
  2152. node = Object::cast_to<Node>(obj);
  2153. if (!node) {
  2154. memdelete(obj);
  2155. }
  2156. ERR_FAIL_NULL_V(node, nullptr);
  2157. }
  2158. if (!get_scene_file_path().is_empty()) { //an instance
  2159. node->set_scene_file_path(get_scene_file_path());
  2160. node->data.editable_instance = data.editable_instance;
  2161. }
  2162. List<const Node *> hidden_roots;
  2163. List<const Node *> node_tree;
  2164. node_tree.push_front(this);
  2165. if (instantiated) {
  2166. // Since nodes in the instantiated hierarchy won't be duplicated explicitly, we need to make an inventory
  2167. // of all the nodes in the tree of the instantiated scene in order to transfer the values of the properties
  2168. Vector<const Node *> instance_roots;
  2169. instance_roots.push_back(this);
  2170. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  2171. for (int i = 0; i < N->get()->get_child_count(); ++i) {
  2172. Node *descendant = N->get()->get_child(i);
  2173. if (!descendant->get_owner()) {
  2174. continue; // Internal nodes or nodes added by scripts.
  2175. }
  2176. // Skip nodes not really belonging to the instantiated hierarchy; they'll be processed normally later
  2177. // but remember non-instantiated nodes that are hidden below instantiated ones
  2178. if (!instance_roots.has(descendant->get_owner())) {
  2179. if (descendant->get_parent() && descendant->get_parent() != this && descendant->data.owner != descendant->get_parent()) {
  2180. hidden_roots.push_back(descendant);
  2181. }
  2182. continue;
  2183. }
  2184. node_tree.push_back(descendant);
  2185. if (!descendant->get_scene_file_path().is_empty() && instance_roots.has(descendant->get_owner())) {
  2186. instance_roots.push_back(descendant);
  2187. }
  2188. }
  2189. }
  2190. }
  2191. if (get_name() != String()) {
  2192. node->set_name(get_name());
  2193. }
  2194. #ifdef TOOLS_ENABLED
  2195. if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
  2196. r_duplimap->insert(this, node);
  2197. }
  2198. #endif
  2199. if (p_flags & DUPLICATE_GROUPS) {
  2200. List<GroupInfo> gi;
  2201. get_groups(&gi);
  2202. for (const GroupInfo &E : gi) {
  2203. #ifdef TOOLS_ENABLED
  2204. if ((p_flags & DUPLICATE_FROM_EDITOR) && !E.persistent) {
  2205. continue;
  2206. }
  2207. #endif
  2208. node->add_to_group(E.name, E.persistent);
  2209. }
  2210. }
  2211. for (int i = 0; i < get_child_count(); i++) {
  2212. if (get_child(i)->data.parent_owned) {
  2213. continue;
  2214. }
  2215. if (instantiated && get_child(i)->data.owner == this) {
  2216. continue; //part of instance
  2217. }
  2218. Node *dup = get_child(i)->_duplicate(p_flags, r_duplimap);
  2219. if (!dup) {
  2220. memdelete(node);
  2221. return nullptr;
  2222. }
  2223. node->add_child(dup);
  2224. if (i < node->get_child_count() - 1) {
  2225. node->move_child(dup, i);
  2226. }
  2227. }
  2228. for (const Node *&E : hidden_roots) {
  2229. Node *parent = node->get_node(get_path_to(E->data.parent));
  2230. if (!parent) {
  2231. memdelete(node);
  2232. return nullptr;
  2233. }
  2234. Node *dup = E->_duplicate(p_flags, r_duplimap);
  2235. if (!dup) {
  2236. memdelete(node);
  2237. return nullptr;
  2238. }
  2239. parent->add_child(dup);
  2240. int pos = E->get_index();
  2241. if (pos < parent->get_child_count() - 1) {
  2242. parent->move_child(dup, pos);
  2243. }
  2244. }
  2245. return node;
  2246. }
  2247. Node *Node::duplicate(int p_flags) const {
  2248. ERR_THREAD_GUARD_V(nullptr);
  2249. Node *dupe = _duplicate(p_flags);
  2250. if (dupe && (p_flags & DUPLICATE_SIGNALS)) {
  2251. _duplicate_signals(this, dupe);
  2252. }
  2253. _duplicate_properties(this, this, dupe, p_flags);
  2254. return dupe;
  2255. }
  2256. #ifdef TOOLS_ENABLED
  2257. Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap) const {
  2258. return duplicate_from_editor(r_duplimap, HashMap<Ref<Resource>, Ref<Resource>>());
  2259. }
  2260. Node *Node::duplicate_from_editor(HashMap<const Node *, Node *> &r_duplimap, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
  2261. int flags = DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANTIATION | DUPLICATE_FROM_EDITOR;
  2262. Node *dupe = _duplicate(flags, &r_duplimap);
  2263. // This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
  2264. if (!p_resource_remap.is_empty()) {
  2265. remap_node_resources(dupe, p_resource_remap);
  2266. }
  2267. // Duplication of signals must happen after all the node descendants have been copied,
  2268. // because re-targeting of connections from some descendant to another is not possible
  2269. // if the emitter node comes later in tree order than the receiver
  2270. _duplicate_signals(this, dupe);
  2271. _duplicate_properties(this, this, dupe, flags);
  2272. return dupe;
  2273. }
  2274. void Node::remap_node_resources(Node *p_node, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
  2275. List<PropertyInfo> props;
  2276. p_node->get_property_list(&props);
  2277. for (const PropertyInfo &E : props) {
  2278. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2279. continue;
  2280. }
  2281. Variant v = p_node->get(E.name);
  2282. if (v.is_ref_counted()) {
  2283. Ref<Resource> res = v;
  2284. if (res.is_valid()) {
  2285. if (p_resource_remap.has(res)) {
  2286. p_node->set(E.name, p_resource_remap[res]);
  2287. remap_nested_resources(res, p_resource_remap);
  2288. }
  2289. }
  2290. }
  2291. }
  2292. for (int i = 0; i < p_node->get_child_count(); i++) {
  2293. remap_node_resources(p_node->get_child(i), p_resource_remap);
  2294. }
  2295. }
  2296. void Node::remap_nested_resources(Ref<Resource> p_resource, const HashMap<Ref<Resource>, Ref<Resource>> &p_resource_remap) const {
  2297. List<PropertyInfo> props;
  2298. p_resource->get_property_list(&props);
  2299. for (const PropertyInfo &E : props) {
  2300. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2301. continue;
  2302. }
  2303. Variant v = p_resource->get(E.name);
  2304. if (v.is_ref_counted()) {
  2305. Ref<Resource> res = v;
  2306. if (res.is_valid()) {
  2307. if (p_resource_remap.has(res)) {
  2308. p_resource->set(E.name, p_resource_remap[res]);
  2309. remap_nested_resources(res, p_resource_remap);
  2310. }
  2311. }
  2312. }
  2313. }
  2314. }
  2315. #endif
  2316. // Duplicate node's properties.
  2317. // This has to be called after nodes have been duplicated since there might be properties
  2318. // of type Node that can be updated properly only if duplicated node tree is complete.
  2319. void Node::_duplicate_properties(const Node *p_root, const Node *p_original, Node *p_copy, int p_flags) const {
  2320. List<PropertyInfo> props;
  2321. p_original->get_property_list(&props);
  2322. const StringName &script_property_name = CoreStringName(script);
  2323. if (p_flags & DUPLICATE_SCRIPTS) {
  2324. bool is_valid = false;
  2325. Variant scr = p_original->get(script_property_name, &is_valid);
  2326. if (is_valid) {
  2327. p_copy->set(script_property_name, scr);
  2328. }
  2329. }
  2330. for (const PropertyInfo &E : props) {
  2331. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  2332. continue;
  2333. }
  2334. const StringName name = E.name;
  2335. if (name == script_property_name) {
  2336. continue;
  2337. }
  2338. Variant value = p_original->get(name).duplicate(true);
  2339. if (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE) {
  2340. Resource *res = Object::cast_to<Resource>(value);
  2341. if (res) { // Duplicate only if it's a resource
  2342. p_copy->set(name, res->duplicate());
  2343. }
  2344. } else {
  2345. if (value.get_type() == Variant::OBJECT) {
  2346. Node *property_node = Object::cast_to<Node>(value);
  2347. Variant out_value = value;
  2348. if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
  2349. out_value = p_copy->get_node_or_null(p_original->get_path_to(property_node));
  2350. }
  2351. p_copy->set(name, out_value);
  2352. } else if (value.get_type() == Variant::ARRAY) {
  2353. Array arr = value;
  2354. if (arr.get_typed_builtin() == Variant::OBJECT) {
  2355. for (int i = 0; i < arr.size(); i++) {
  2356. Node *property_node = Object::cast_to<Node>(arr[i]);
  2357. if (property_node && (p_root == property_node || p_root->is_ancestor_of(property_node))) {
  2358. arr[i] = p_copy->get_node_or_null(p_original->get_path_to(property_node));
  2359. }
  2360. }
  2361. value = arr;
  2362. p_copy->set(name, value);
  2363. }
  2364. } else {
  2365. p_copy->set(name, value);
  2366. }
  2367. }
  2368. }
  2369. for (int i = 0; i < p_original->get_child_count(); i++) {
  2370. Node *copy_child = p_copy->get_child(i);
  2371. ERR_FAIL_NULL_MSG(copy_child, "Child node disappeared while duplicating.");
  2372. _duplicate_properties(p_root, p_original->get_child(i), copy_child, p_flags);
  2373. }
  2374. }
  2375. // Duplication of signals must happen after all the node descendants have been copied,
  2376. // because re-targeting of connections from some descendant to another is not possible
  2377. // if the emitter node comes later in tree order than the receiver
  2378. void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
  2379. if ((this != p_original) && !(p_original->is_ancestor_of(this))) {
  2380. return;
  2381. }
  2382. List<const Node *> process_list;
  2383. process_list.push_back(this);
  2384. while (!process_list.is_empty()) {
  2385. const Node *n = process_list.front()->get();
  2386. process_list.pop_front();
  2387. List<Connection> conns;
  2388. n->get_all_signal_connections(&conns);
  2389. for (const Connection &E : conns) {
  2390. if (E.flags & CONNECT_PERSIST) {
  2391. //user connected
  2392. NodePath p = p_original->get_path_to(n);
  2393. Node *copy = p_copy->get_node(p);
  2394. Node *target = Object::cast_to<Node>(E.callable.get_object());
  2395. if (!target) {
  2396. continue;
  2397. }
  2398. NodePath ptarget = p_original->get_path_to(target);
  2399. Node *copytarget = target;
  2400. // Attempt to find a path to the duplicate target, if it seems it's not part
  2401. // of the duplicated and not yet parented hierarchy then at least try to connect
  2402. // to the same target as the original
  2403. if (p_copy->has_node(ptarget)) {
  2404. copytarget = p_copy->get_node(ptarget);
  2405. }
  2406. if (copy && copytarget && E.callable.get_method() != StringName()) {
  2407. Callable copy_callable = Callable(copytarget, E.callable.get_method());
  2408. if (!copy->is_connected(E.signal.get_name(), copy_callable)) {
  2409. int arg_count = E.callable.get_bound_arguments_count();
  2410. if (arg_count > 0) {
  2411. copy_callable = copy_callable.bindv(E.callable.get_bound_arguments());
  2412. } else if (arg_count < 0) {
  2413. copy_callable = copy_callable.unbind(-arg_count);
  2414. }
  2415. copy->connect(E.signal.get_name(), copy_callable, E.flags);
  2416. }
  2417. }
  2418. }
  2419. }
  2420. for (int i = 0; i < n->get_child_count(); i++) {
  2421. process_list.push_back(n->get_child(i));
  2422. }
  2423. }
  2424. }
  2425. static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
  2426. if (p_node->get_owner() == p_by) {
  2427. p_owned->push_back(p_node);
  2428. }
  2429. for (int i = 0; i < p_node->get_child_count(); i++) {
  2430. find_owned_by(p_by, p_node->get_child(i), p_owned);
  2431. }
  2432. }
  2433. void Node::replace_by(Node *p_node, bool p_keep_groups) {
  2434. ERR_THREAD_GUARD
  2435. ERR_FAIL_NULL(p_node);
  2436. ERR_FAIL_COND(p_node->data.parent);
  2437. List<Node *> owned = data.owned;
  2438. List<Node *> owned_by_owner;
  2439. Node *owner = (data.owner == this) ? p_node : data.owner;
  2440. if (p_keep_groups) {
  2441. List<GroupInfo> groups;
  2442. get_groups(&groups);
  2443. for (const GroupInfo &E : groups) {
  2444. p_node->add_to_group(E.name, E.persistent);
  2445. }
  2446. }
  2447. _replace_connections_target(p_node);
  2448. if (data.owner) {
  2449. for (int i = 0; i < get_child_count(); i++) {
  2450. find_owned_by(data.owner, get_child(i), &owned_by_owner);
  2451. }
  2452. _clean_up_owner();
  2453. }
  2454. Node *parent = data.parent;
  2455. int index_in_parent = get_index(false);
  2456. if (data.parent) {
  2457. parent->remove_child(this);
  2458. parent->add_child(p_node);
  2459. parent->move_child(p_node, index_in_parent);
  2460. }
  2461. emit_signal(SNAME("replacing_by"), p_node);
  2462. while (get_child_count()) {
  2463. Node *child = get_child(0);
  2464. remove_child(child);
  2465. if (!child->is_owned_by_parent()) {
  2466. // add the custom children to the p_node
  2467. Node *child_owner = child->get_owner() == this ? p_node : child->get_owner();
  2468. child->set_owner(nullptr);
  2469. p_node->add_child(child);
  2470. child->set_owner(child_owner);
  2471. }
  2472. }
  2473. p_node->set_owner(owner);
  2474. for (Node *E : owned) {
  2475. if (E->data.owner != p_node) {
  2476. E->set_owner(p_node);
  2477. }
  2478. }
  2479. for (Node *E : owned_by_owner) {
  2480. if (E->data.owner != owner) {
  2481. E->set_owner(owner);
  2482. }
  2483. }
  2484. p_node->set_scene_file_path(get_scene_file_path());
  2485. }
  2486. void Node::_replace_connections_target(Node *p_new_target) {
  2487. List<Connection> cl;
  2488. get_signals_connected_to_this(&cl);
  2489. for (const Connection &c : cl) {
  2490. if (c.flags & CONNECT_PERSIST) {
  2491. c.signal.get_object()->disconnect(c.signal.get_name(), Callable(this, c.callable.get_method()));
  2492. 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());
  2493. 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()));
  2494. c.signal.get_object()->connect(c.signal.get_name(), Callable(p_new_target, c.callable.get_method()), c.flags);
  2495. }
  2496. }
  2497. }
  2498. bool Node::has_node_and_resource(const NodePath &p_path) const {
  2499. ERR_THREAD_GUARD_V(false);
  2500. if (!has_node(p_path)) {
  2501. return false;
  2502. }
  2503. Ref<Resource> res;
  2504. Vector<StringName> leftover_path;
  2505. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  2506. return node;
  2507. }
  2508. Array Node::_get_node_and_resource(const NodePath &p_path) {
  2509. Ref<Resource> res;
  2510. Vector<StringName> leftover_path;
  2511. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  2512. Array result;
  2513. if (node) {
  2514. result.push_back(node);
  2515. } else {
  2516. result.push_back(Variant());
  2517. }
  2518. if (res.is_valid()) {
  2519. result.push_back(res);
  2520. } else {
  2521. result.push_back(Variant());
  2522. }
  2523. result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
  2524. return result;
  2525. }
  2526. Node *Node::get_node_and_resource(const NodePath &p_path, Ref<Resource> &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
  2527. ERR_THREAD_GUARD_V(nullptr);
  2528. r_res = Ref<Resource>();
  2529. r_leftover_subpath = Vector<StringName>();
  2530. Node *node = get_node_or_null(p_path);
  2531. if (!node) {
  2532. return nullptr;
  2533. }
  2534. if (p_path.get_subname_count()) {
  2535. int j = 0;
  2536. // If not p_last_is_property, we shouldn't consider the last one as part of the resource
  2537. for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
  2538. bool is_valid = false;
  2539. Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j), &is_valid) : r_res->get(p_path.get_subname(j), &is_valid);
  2540. if (!is_valid) { // Found nothing on that path
  2541. return nullptr;
  2542. }
  2543. Ref<Resource> new_res = new_res_v;
  2544. if (new_res.is_null()) { // No longer a resource, assume property
  2545. break;
  2546. }
  2547. r_res = new_res;
  2548. }
  2549. for (; j < p_path.get_subname_count(); j++) {
  2550. // Put the rest of the subpath in the leftover path
  2551. r_leftover_subpath.push_back(p_path.get_subname(j));
  2552. }
  2553. }
  2554. return node;
  2555. }
  2556. void Node::_set_tree(SceneTree *p_tree) {
  2557. SceneTree *tree_changed_a = nullptr;
  2558. SceneTree *tree_changed_b = nullptr;
  2559. //ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  2560. if (data.tree) {
  2561. _propagate_exit_tree();
  2562. tree_changed_a = data.tree;
  2563. }
  2564. data.tree = p_tree;
  2565. if (data.tree) {
  2566. _propagate_enter_tree();
  2567. if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
  2568. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  2569. }
  2570. tree_changed_b = data.tree;
  2571. }
  2572. if (tree_changed_a) {
  2573. tree_changed_a->tree_changed();
  2574. }
  2575. if (tree_changed_b) {
  2576. tree_changed_b->tree_changed();
  2577. }
  2578. }
  2579. #ifdef DEBUG_ENABLED
  2580. static HashMap<ObjectID, List<String>> _print_orphan_nodes_map;
  2581. static void _print_orphan_nodes_routine(Object *p_obj) {
  2582. Node *n = Object::cast_to<Node>(p_obj);
  2583. if (!n) {
  2584. return;
  2585. }
  2586. if (n->is_inside_tree()) {
  2587. return;
  2588. }
  2589. Node *p = n;
  2590. while (p->get_parent()) {
  2591. p = p->get_parent();
  2592. }
  2593. String path;
  2594. if (p == n) {
  2595. path = n->get_name();
  2596. } else {
  2597. path = String(p->get_name()) + "/" + p->get_path_to(n);
  2598. }
  2599. List<String> info_strings;
  2600. info_strings.push_back(path);
  2601. info_strings.push_back(n->get_class());
  2602. _print_orphan_nodes_map[p_obj->get_instance_id()] = info_strings;
  2603. }
  2604. #endif // DEBUG_ENABLED
  2605. void Node::print_orphan_nodes() {
  2606. #ifdef DEBUG_ENABLED
  2607. // Make sure it's empty.
  2608. _print_orphan_nodes_map.clear();
  2609. // Collect and print information about orphan nodes.
  2610. ObjectDB::debug_objects(_print_orphan_nodes_routine);
  2611. for (const KeyValue<ObjectID, List<String>> &E : _print_orphan_nodes_map) {
  2612. print_line(itos(E.key) + " - Stray Node: " + E.value.get(0) + " (Type: " + E.value.get(1) + ")");
  2613. }
  2614. // Flush it after use.
  2615. _print_orphan_nodes_map.clear();
  2616. #endif
  2617. }
  2618. void Node::queue_free() {
  2619. // There are users which instantiate multiple scene trees for their games.
  2620. // Use the node's own tree to handle its deletion when relevant.
  2621. if (is_inside_tree()) {
  2622. get_tree()->queue_delete(this);
  2623. } else {
  2624. SceneTree *tree = SceneTree::get_singleton();
  2625. ERR_FAIL_NULL_MSG(tree, "Can't queue free a node when no SceneTree is available.");
  2626. tree->queue_delete(this);
  2627. }
  2628. }
  2629. void Node::set_import_path(const NodePath &p_import_path) {
  2630. #ifdef TOOLS_ENABLED
  2631. data.import_path = p_import_path;
  2632. #endif
  2633. }
  2634. NodePath Node::get_import_path() const {
  2635. #ifdef TOOLS_ENABLED
  2636. return data.import_path;
  2637. #else
  2638. return NodePath();
  2639. #endif
  2640. }
  2641. #ifdef TOOLS_ENABLED
  2642. static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
  2643. if (p_node != p_base && !p_node->get_owner()) {
  2644. return;
  2645. }
  2646. if (p_node->is_unique_name_in_owner() && p_node->get_owner() == p_base) {
  2647. String n = "%" + p_node->get_name();
  2648. r_options->push_back(n.quote());
  2649. }
  2650. String n = p_base->get_path_to(p_node);
  2651. r_options->push_back(n.quote());
  2652. for (int i = 0; i < p_node->get_child_count(); i++) {
  2653. _add_nodes_to_options(p_base, p_node->get_child(i), r_options);
  2654. }
  2655. }
  2656. void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  2657. const String pf = p_function;
  2658. if (p_idx == 0 && (pf == "has_node" || pf == "get_node" || pf == "get_node_or_null")) {
  2659. _add_nodes_to_options(this, this, r_options);
  2660. } else if (p_idx == 0 && (pf == "add_to_group" || pf == "remove_from_group" || pf == "is_in_group")) {
  2661. HashMap<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
  2662. for (const KeyValue<StringName, String> &E : global_groups) {
  2663. r_options->push_back(E.key.operator String().quote());
  2664. }
  2665. }
  2666. Object::get_argument_options(p_function, p_idx, r_options);
  2667. }
  2668. #endif
  2669. void Node::clear_internal_tree_resource_paths() {
  2670. clear_internal_resource_paths();
  2671. for (KeyValue<StringName, Node *> &K : data.children) {
  2672. K.value->clear_internal_tree_resource_paths();
  2673. }
  2674. }
  2675. PackedStringArray Node::get_configuration_warnings() const {
  2676. ERR_THREAD_GUARD_V(PackedStringArray());
  2677. PackedStringArray ret;
  2678. Vector<String> warnings;
  2679. if (GDVIRTUAL_CALL(_get_configuration_warnings, warnings)) {
  2680. ret.append_array(warnings);
  2681. }
  2682. return ret;
  2683. }
  2684. void Node::update_configuration_warnings() {
  2685. ERR_THREAD_GUARD
  2686. #ifdef TOOLS_ENABLED
  2687. if (!is_inside_tree()) {
  2688. return;
  2689. }
  2690. if (get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_ancestor_of(this))) {
  2691. get_tree()->emit_signal(SceneStringName(node_configuration_warning_changed), this);
  2692. }
  2693. #endif
  2694. }
  2695. bool Node::is_owned_by_parent() const {
  2696. return data.parent_owned;
  2697. }
  2698. void Node::set_display_folded(bool p_folded) {
  2699. ERR_THREAD_GUARD
  2700. data.display_folded = p_folded;
  2701. }
  2702. bool Node::is_displayed_folded() const {
  2703. return data.display_folded;
  2704. }
  2705. bool Node::is_ready() const {
  2706. return !data.ready_first;
  2707. }
  2708. void Node::request_ready() {
  2709. ERR_THREAD_GUARD
  2710. data.ready_first = true;
  2711. }
  2712. void Node::_call_input(const Ref<InputEvent> &p_event) {
  2713. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  2714. GDVIRTUAL_CALL(_input, p_event);
  2715. }
  2716. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2717. return;
  2718. }
  2719. input(p_event);
  2720. }
  2721. void Node::_call_shortcut_input(const Ref<InputEvent> &p_event) {
  2722. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  2723. GDVIRTUAL_CALL(_shortcut_input, p_event);
  2724. }
  2725. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2726. return;
  2727. }
  2728. shortcut_input(p_event);
  2729. }
  2730. void Node::_call_unhandled_input(const Ref<InputEvent> &p_event) {
  2731. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  2732. GDVIRTUAL_CALL(_unhandled_input, p_event);
  2733. }
  2734. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2735. return;
  2736. }
  2737. unhandled_input(p_event);
  2738. }
  2739. void Node::_call_unhandled_key_input(const Ref<InputEvent> &p_event) {
  2740. if (p_event->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  2741. GDVIRTUAL_CALL(_unhandled_key_input, p_event);
  2742. }
  2743. if (!is_inside_tree() || !get_viewport() || get_viewport()->is_input_handled()) {
  2744. return;
  2745. }
  2746. unhandled_key_input(p_event);
  2747. }
  2748. void Node::_validate_property(PropertyInfo &p_property) const {
  2749. if ((p_property.name == "process_thread_group_order" || p_property.name == "process_thread_messages") && data.process_thread_group == PROCESS_THREAD_GROUP_INHERIT) {
  2750. p_property.usage = 0;
  2751. }
  2752. }
  2753. void Node::input(const Ref<InputEvent> &p_event) {
  2754. }
  2755. void Node::shortcut_input(const Ref<InputEvent> &p_key_event) {
  2756. }
  2757. void Node::unhandled_input(const Ref<InputEvent> &p_event) {
  2758. }
  2759. void Node::unhandled_key_input(const Ref<InputEvent> &p_key_event) {
  2760. }
  2761. Variant Node::_call_deferred_thread_group_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  2762. if (p_argcount < 1) {
  2763. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  2764. r_error.expected = 1;
  2765. return Variant();
  2766. }
  2767. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  2768. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  2769. r_error.argument = 0;
  2770. r_error.expected = Variant::STRING_NAME;
  2771. return Variant();
  2772. }
  2773. r_error.error = Callable::CallError::CALL_OK;
  2774. StringName method = *p_args[0];
  2775. call_deferred_thread_groupp(method, &p_args[1], p_argcount - 1, true);
  2776. return Variant();
  2777. }
  2778. Variant Node::_call_thread_safe_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  2779. if (p_argcount < 1) {
  2780. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  2781. r_error.expected = 1;
  2782. return Variant();
  2783. }
  2784. if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) {
  2785. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  2786. r_error.argument = 0;
  2787. r_error.expected = Variant::STRING_NAME;
  2788. return Variant();
  2789. }
  2790. r_error.error = Callable::CallError::CALL_OK;
  2791. StringName method = *p_args[0];
  2792. call_thread_safep(method, &p_args[1], p_argcount - 1, true);
  2793. return Variant();
  2794. }
  2795. void Node::call_deferred_thread_groupp(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  2796. ERR_FAIL_COND(!is_inside_tree());
  2797. SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
  2798. pg->call_queue.push_callp(this, p_method, p_args, p_argcount, p_show_error);
  2799. }
  2800. void Node::set_deferred_thread_group(const StringName &p_property, const Variant &p_value) {
  2801. ERR_FAIL_COND(!is_inside_tree());
  2802. SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
  2803. pg->call_queue.push_set(this, p_property, p_value);
  2804. }
  2805. void Node::notify_deferred_thread_group(int p_notification) {
  2806. ERR_FAIL_COND(!is_inside_tree());
  2807. SceneTree::ProcessGroup *pg = (SceneTree::ProcessGroup *)data.process_group;
  2808. pg->call_queue.push_notification(this, p_notification);
  2809. }
  2810. void Node::call_thread_safep(const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  2811. if (is_accessible_from_caller_thread()) {
  2812. Callable::CallError ce;
  2813. callp(p_method, p_args, p_argcount, ce);
  2814. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  2815. ERR_FAIL_MSG("Error calling method from 'call_threadp': " + Variant::get_call_error_text(this, p_method, p_args, p_argcount, ce) + ".");
  2816. }
  2817. } else {
  2818. call_deferred_thread_groupp(p_method, p_args, p_argcount, p_show_error);
  2819. }
  2820. }
  2821. void Node::set_thread_safe(const StringName &p_property, const Variant &p_value) {
  2822. if (is_accessible_from_caller_thread()) {
  2823. set(p_property, p_value);
  2824. } else {
  2825. set_deferred_thread_group(p_property, p_value);
  2826. }
  2827. }
  2828. void Node::notify_thread_safe(int p_notification) {
  2829. if (is_accessible_from_caller_thread()) {
  2830. notification(p_notification);
  2831. } else {
  2832. notify_deferred_thread_group(p_notification);
  2833. }
  2834. }
  2835. void Node::_bind_methods() {
  2836. GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"), 0);
  2837. GLOBAL_DEF(PropertyInfo(Variant::INT, "editor/naming/node_name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case"), NAME_CASING_PASCAL_CASE);
  2838. ClassDB::bind_static_method("Node", D_METHOD("print_orphan_nodes"), &Node::print_orphan_nodes);
  2839. ClassDB::bind_method(D_METHOD("add_sibling", "sibling", "force_readable_name"), &Node::add_sibling, DEFVAL(false));
  2840. ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
  2841. ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
  2842. ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_name", "internal"), &Node::add_child, DEFVAL(false), DEFVAL(0));
  2843. ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
  2844. ClassDB::bind_method(D_METHOD("reparent", "new_parent", "keep_global_transform"), &Node::reparent, DEFVAL(true));
  2845. 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.
  2846. ClassDB::bind_method(D_METHOD("get_children", "include_internal"), &Node::get_children, DEFVAL(false));
  2847. ClassDB::bind_method(D_METHOD("get_child", "idx", "include_internal"), &Node::get_child, DEFVAL(false));
  2848. ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
  2849. ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
  2850. ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
  2851. ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
  2852. ClassDB::bind_method(D_METHOD("find_child", "pattern", "recursive", "owned"), &Node::find_child, DEFVAL(true), DEFVAL(true));
  2853. ClassDB::bind_method(D_METHOD("find_children", "pattern", "type", "recursive", "owned"), &Node::find_children, DEFVAL(""), DEFVAL(true), DEFVAL(true));
  2854. ClassDB::bind_method(D_METHOD("find_parent", "pattern"), &Node::find_parent);
  2855. ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
  2856. ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
  2857. ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
  2858. ClassDB::bind_method(D_METHOD("is_part_of_edited_scene"), &Node::is_part_of_edited_scene);
  2859. ClassDB::bind_method(D_METHOD("is_ancestor_of", "node"), &Node::is_ancestor_of);
  2860. ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
  2861. ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
  2862. ClassDB::bind_method(D_METHOD("get_path_to", "node", "use_unique_path"), &Node::get_path_to, DEFVAL(false));
  2863. ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
  2864. ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
  2865. ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
  2866. ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_index"), &Node::move_child);
  2867. ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
  2868. ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
  2869. ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
  2870. ClassDB::bind_method(D_METHOD("get_index", "include_internal"), &Node::get_index, DEFVAL(false));
  2871. ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
  2872. ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
  2873. ClassDB::bind_method(D_METHOD("get_tree_string"), &Node::get_tree_string);
  2874. ClassDB::bind_method(D_METHOD("get_tree_string_pretty"), &Node::get_tree_string_pretty);
  2875. ClassDB::bind_method(D_METHOD("set_scene_file_path", "scene_file_path"), &Node::set_scene_file_path);
  2876. ClassDB::bind_method(D_METHOD("get_scene_file_path"), &Node::get_scene_file_path);
  2877. ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
  2878. ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
  2879. ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
  2880. ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
  2881. ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
  2882. ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
  2883. ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
  2884. ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
  2885. ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
  2886. ClassDB::bind_method(D_METHOD("set_physics_process_priority", "priority"), &Node::set_physics_process_priority);
  2887. ClassDB::bind_method(D_METHOD("get_physics_process_priority"), &Node::get_physics_process_priority);
  2888. ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
  2889. ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
  2890. ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
  2891. ClassDB::bind_method(D_METHOD("set_process_shortcut_input", "enable"), &Node::set_process_shortcut_input);
  2892. ClassDB::bind_method(D_METHOD("is_processing_shortcut_input"), &Node::is_processing_shortcut_input);
  2893. ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
  2894. ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
  2895. ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
  2896. ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
  2897. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Node::set_process_mode);
  2898. ClassDB::bind_method(D_METHOD("get_process_mode"), &Node::get_process_mode);
  2899. ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
  2900. ClassDB::bind_method(D_METHOD("set_process_thread_group", "mode"), &Node::set_process_thread_group);
  2901. ClassDB::bind_method(D_METHOD("get_process_thread_group"), &Node::get_process_thread_group);
  2902. ClassDB::bind_method(D_METHOD("set_process_thread_messages", "flags"), &Node::set_process_thread_messages);
  2903. ClassDB::bind_method(D_METHOD("get_process_thread_messages"), &Node::get_process_thread_messages);
  2904. ClassDB::bind_method(D_METHOD("set_process_thread_group_order", "order"), &Node::set_process_thread_group_order);
  2905. ClassDB::bind_method(D_METHOD("get_process_thread_group_order"), &Node::get_process_thread_group_order);
  2906. ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
  2907. ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
  2908. ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
  2909. ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
  2910. ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
  2911. ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
  2912. ClassDB::bind_method(D_METHOD("set_physics_interpolation_mode", "mode"), &Node::set_physics_interpolation_mode);
  2913. ClassDB::bind_method(D_METHOD("get_physics_interpolation_mode"), &Node::get_physics_interpolation_mode);
  2914. ClassDB::bind_method(D_METHOD("is_physics_interpolated"), &Node::is_physics_interpolated);
  2915. ClassDB::bind_method(D_METHOD("is_physics_interpolated_and_enabled"), &Node::is_physics_interpolated_and_enabled);
  2916. ClassDB::bind_method(D_METHOD("reset_physics_interpolation"), &Node::reset_physics_interpolation);
  2917. ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "mode"), &Node::set_auto_translate_mode);
  2918. ClassDB::bind_method(D_METHOD("get_auto_translate_mode"), &Node::get_auto_translate_mode);
  2919. ClassDB::bind_method(D_METHOD("get_window"), &Node::get_window);
  2920. ClassDB::bind_method(D_METHOD("get_last_exclusive_window"), &Node::get_last_exclusive_window);
  2921. ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
  2922. ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
  2923. ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANTIATION | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
  2924. ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_groups"), &Node::replace_by, DEFVAL(false));
  2925. ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
  2926. ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
  2927. ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
  2928. ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
  2929. ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
  2930. ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_free);
  2931. ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
  2932. ClassDB::bind_method(D_METHOD("is_node_ready"), &Node::is_ready);
  2933. ClassDB::bind_method(D_METHOD("set_multiplayer_authority", "id", "recursive"), &Node::set_multiplayer_authority, DEFVAL(true));
  2934. ClassDB::bind_method(D_METHOD("get_multiplayer_authority"), &Node::get_multiplayer_authority);
  2935. ClassDB::bind_method(D_METHOD("is_multiplayer_authority"), &Node::is_multiplayer_authority);
  2936. ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
  2937. ClassDB::bind_method(D_METHOD("rpc_config", "method", "config"), &Node::rpc_config);
  2938. ClassDB::bind_method(D_METHOD("set_editor_description", "editor_description"), &Node::set_editor_description);
  2939. ClassDB::bind_method(D_METHOD("get_editor_description"), &Node::get_editor_description);
  2940. ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
  2941. ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);
  2942. ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
  2943. ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
  2944. ClassDB::bind_method(D_METHOD("atr", "message", "context"), &Node::atr, DEFVAL(""));
  2945. ClassDB::bind_method(D_METHOD("atr_n", "message", "plural_message", "n", "context"), &Node::atr_n, DEFVAL(""));
  2946. #ifdef TOOLS_ENABLED
  2947. ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
  2948. #endif
  2949. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "_import_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_import_path", "_get_import_path");
  2950. {
  2951. MethodInfo mi;
  2952. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  2953. mi.name = "rpc";
  2954. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
  2955. mi.arguments.push_front(PropertyInfo(Variant::INT, "peer_id"));
  2956. mi.name = "rpc_id";
  2957. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
  2958. }
  2959. ClassDB::bind_method(D_METHOD("update_configuration_warnings"), &Node::update_configuration_warnings);
  2960. {
  2961. MethodInfo mi;
  2962. mi.name = "call_deferred_thread_group";
  2963. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  2964. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred_thread_group", &Node::_call_deferred_thread_group_bind, mi, varray(), false);
  2965. }
  2966. ClassDB::bind_method(D_METHOD("set_deferred_thread_group", "property", "value"), &Node::set_deferred_thread_group);
  2967. ClassDB::bind_method(D_METHOD("notify_deferred_thread_group", "what"), &Node::notify_deferred_thread_group);
  2968. {
  2969. MethodInfo mi;
  2970. mi.name = "call_thread_safe";
  2971. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  2972. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_thread_safe", &Node::_call_thread_safe_bind, mi, varray(), false);
  2973. }
  2974. ClassDB::bind_method(D_METHOD("set_thread_safe", "property", "value"), &Node::set_thread_safe);
  2975. ClassDB::bind_method(D_METHOD("notify_thread_safe", "what"), &Node::notify_thread_safe);
  2976. BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
  2977. BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
  2978. BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
  2979. BIND_CONSTANT(NOTIFICATION_READY);
  2980. BIND_CONSTANT(NOTIFICATION_PAUSED);
  2981. BIND_CONSTANT(NOTIFICATION_UNPAUSED);
  2982. BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
  2983. BIND_CONSTANT(NOTIFICATION_PROCESS);
  2984. BIND_CONSTANT(NOTIFICATION_PARENTED);
  2985. BIND_CONSTANT(NOTIFICATION_UNPARENTED);
  2986. BIND_CONSTANT(NOTIFICATION_SCENE_INSTANTIATED);
  2987. BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
  2988. BIND_CONSTANT(NOTIFICATION_DRAG_END);
  2989. BIND_CONSTANT(NOTIFICATION_PATH_RENAMED);
  2990. BIND_CONSTANT(NOTIFICATION_CHILD_ORDER_CHANGED);
  2991. BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
  2992. BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  2993. BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
  2994. BIND_CONSTANT(NOTIFICATION_DISABLED);
  2995. BIND_CONSTANT(NOTIFICATION_ENABLED);
  2996. BIND_CONSTANT(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  2997. BIND_CONSTANT(NOTIFICATION_EDITOR_PRE_SAVE);
  2998. BIND_CONSTANT(NOTIFICATION_EDITOR_POST_SAVE);
  2999. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
  3000. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
  3001. BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_IN);
  3002. BIND_CONSTANT(NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  3003. BIND_CONSTANT(NOTIFICATION_WM_CLOSE_REQUEST);
  3004. BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
  3005. BIND_CONSTANT(NOTIFICATION_WM_SIZE_CHANGED);
  3006. BIND_CONSTANT(NOTIFICATION_WM_DPI_CHANGE);
  3007. BIND_CONSTANT(NOTIFICATION_VP_MOUSE_ENTER);
  3008. BIND_CONSTANT(NOTIFICATION_VP_MOUSE_EXIT);
  3009. BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
  3010. BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
  3011. BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
  3012. BIND_CONSTANT(NOTIFICATION_CRASH);
  3013. BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
  3014. BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
  3015. BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
  3016. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
  3017. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
  3018. BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
  3019. BIND_ENUM_CONSTANT(PROCESS_MODE_INHERIT);
  3020. BIND_ENUM_CONSTANT(PROCESS_MODE_PAUSABLE);
  3021. BIND_ENUM_CONSTANT(PROCESS_MODE_WHEN_PAUSED);
  3022. BIND_ENUM_CONSTANT(PROCESS_MODE_ALWAYS);
  3023. BIND_ENUM_CONSTANT(PROCESS_MODE_DISABLED);
  3024. BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_INHERIT);
  3025. BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_MAIN_THREAD);
  3026. BIND_ENUM_CONSTANT(PROCESS_THREAD_GROUP_SUB_THREAD);
  3027. BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES);
  3028. BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_PHYSICS);
  3029. BIND_BITFIELD_FLAG(FLAG_PROCESS_THREAD_MESSAGES_ALL);
  3030. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_INHERIT);
  3031. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_ON);
  3032. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_OFF);
  3033. BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
  3034. BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
  3035. BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
  3036. BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANTIATION);
  3037. BIND_ENUM_CONSTANT(INTERNAL_MODE_DISABLED);
  3038. BIND_ENUM_CONSTANT(INTERNAL_MODE_FRONT);
  3039. BIND_ENUM_CONSTANT(INTERNAL_MODE_BACK);
  3040. BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_INHERIT);
  3041. BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_ALWAYS);
  3042. BIND_ENUM_CONSTANT(AUTO_TRANSLATE_MODE_DISABLED);
  3043. ADD_SIGNAL(MethodInfo("ready"));
  3044. ADD_SIGNAL(MethodInfo("renamed"));
  3045. ADD_SIGNAL(MethodInfo("tree_entered"));
  3046. ADD_SIGNAL(MethodInfo("tree_exiting"));
  3047. ADD_SIGNAL(MethodInfo("tree_exited"));
  3048. ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  3049. ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  3050. ADD_SIGNAL(MethodInfo("child_order_changed"));
  3051. ADD_SIGNAL(MethodInfo("replacing_by", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  3052. ADD_SIGNAL(MethodInfo("editor_description_changed", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  3053. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_name", "get_name");
  3054. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_unique_name_in_owner", "is_unique_name_in_owner");
  3055. ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_file_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_file_path", "get_scene_file_path");
  3056. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", PROPERTY_USAGE_NONE), "set_owner", "get_owner");
  3057. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", PROPERTY_USAGE_NONE), "", "get_multiplayer");
  3058. ADD_GROUP("Process", "process_");
  3059. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Inherit,Pausable,When Paused,Always,Disabled"), "set_process_mode", "get_process_mode");
  3060. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
  3061. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_physics_priority"), "set_physics_process_priority", "get_physics_process_priority");
  3062. ADD_SUBGROUP("Thread Group", "process_thread");
  3063. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group", PROPERTY_HINT_ENUM, "Inherit,Main Thread,Sub Thread"), "set_process_thread_group", "get_process_thread_group");
  3064. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_group_order"), "set_process_thread_group_order", "get_process_thread_group_order");
  3065. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_thread_messages", PROPERTY_HINT_FLAGS, "Process,Physics Process"), "set_process_thread_messages", "get_process_thread_messages");
  3066. ADD_GROUP("Physics Interpolation", "physics_interpolation_");
  3067. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_mode", PROPERTY_HINT_ENUM, "Inherit,On,Off"), "set_physics_interpolation_mode", "get_physics_interpolation_mode");
  3068. ADD_GROUP("Auto Translate", "auto_translate_");
  3069. ADD_PROPERTY(PropertyInfo(Variant::INT, "auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_auto_translate_mode", "get_auto_translate_mode");
  3070. ADD_GROUP("Editor Description", "editor_");
  3071. ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT), "set_editor_description", "get_editor_description");
  3072. GDVIRTUAL_BIND(_process, "delta");
  3073. GDVIRTUAL_BIND(_physics_process, "delta");
  3074. GDVIRTUAL_BIND(_enter_tree);
  3075. GDVIRTUAL_BIND(_exit_tree);
  3076. GDVIRTUAL_BIND(_ready);
  3077. GDVIRTUAL_BIND(_get_configuration_warnings);
  3078. GDVIRTUAL_BIND(_input, "event");
  3079. GDVIRTUAL_BIND(_shortcut_input, "event");
  3080. GDVIRTUAL_BIND(_unhandled_input, "event");
  3081. GDVIRTUAL_BIND(_unhandled_key_input, "event");
  3082. }
  3083. String Node::_get_name_num_separator() {
  3084. switch (GLOBAL_GET("editor/naming/node_name_num_separator").operator int()) {
  3085. case 0:
  3086. return "";
  3087. case 1:
  3088. return " ";
  3089. case 2:
  3090. return "_";
  3091. case 3:
  3092. return "-";
  3093. }
  3094. return " ";
  3095. }
  3096. Node::Node() {
  3097. orphan_node_count++;
  3098. // Default member initializer for bitfield is a C++20 extension, so:
  3099. data.process_mode = PROCESS_MODE_INHERIT;
  3100. data.physics_interpolation_mode = PHYSICS_INTERPOLATION_MODE_INHERIT;
  3101. data.physics_process = false;
  3102. data.process = false;
  3103. data.physics_process_internal = false;
  3104. data.process_internal = false;
  3105. data.input = false;
  3106. data.shortcut_input = false;
  3107. data.unhandled_input = false;
  3108. data.unhandled_key_input = false;
  3109. data.physics_interpolated = true;
  3110. data.parent_owned = false;
  3111. data.in_constructor = true;
  3112. data.use_placeholder = false;
  3113. data.display_folded = false;
  3114. data.editable_instance = false;
  3115. data.inside_tree = false;
  3116. data.ready_notified = false; // This is a small hack, so if a node is added during _ready() to the tree, it correctly gets the _ready() notification.
  3117. data.ready_first = true;
  3118. }
  3119. Node::~Node() {
  3120. data.grouped.clear();
  3121. data.owned.clear();
  3122. data.children.clear();
  3123. data.children_cache.clear();
  3124. ERR_FAIL_COND(data.parent);
  3125. ERR_FAIL_COND(data.children_cache.size());
  3126. orphan_node_count--;
  3127. }
  3128. ////////////////////////////////
  3129. // Multithreaded locked version of Object functions.
  3130. #ifdef DEBUG_ENABLED
  3131. void Node::set_script(const Variant &p_script) {
  3132. ERR_THREAD_GUARD;
  3133. Object::set_script(p_script);
  3134. }
  3135. Variant Node::get_script() const {
  3136. ERR_THREAD_GUARD_V(Variant());
  3137. return Object::get_script();
  3138. }
  3139. bool Node::has_meta(const StringName &p_name) const {
  3140. ERR_THREAD_GUARD_V(false);
  3141. return Object::has_meta(p_name);
  3142. }
  3143. void Node::set_meta(const StringName &p_name, const Variant &p_value) {
  3144. ERR_THREAD_GUARD;
  3145. Object::set_meta(p_name, p_value);
  3146. }
  3147. void Node::remove_meta(const StringName &p_name) {
  3148. ERR_THREAD_GUARD;
  3149. Object::remove_meta(p_name);
  3150. }
  3151. Variant Node::get_meta(const StringName &p_name, const Variant &p_default) const {
  3152. ERR_THREAD_GUARD_V(Variant());
  3153. return Object::get_meta(p_name, p_default);
  3154. }
  3155. void Node::get_meta_list(List<StringName> *p_list) const {
  3156. ERR_THREAD_GUARD;
  3157. Object::get_meta_list(p_list);
  3158. }
  3159. Error Node::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
  3160. ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
  3161. return Object::emit_signalp(p_name, p_args, p_argcount);
  3162. }
  3163. bool Node::has_signal(const StringName &p_name) const {
  3164. ERR_THREAD_GUARD_V(false);
  3165. return Object::has_signal(p_name);
  3166. }
  3167. void Node::get_signal_list(List<MethodInfo> *p_signals) const {
  3168. ERR_THREAD_GUARD;
  3169. Object::get_signal_list(p_signals);
  3170. }
  3171. void Node::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  3172. ERR_THREAD_GUARD;
  3173. Object::get_signal_connection_list(p_signal, p_connections);
  3174. }
  3175. void Node::get_all_signal_connections(List<Connection> *p_connections) const {
  3176. ERR_THREAD_GUARD;
  3177. Object::get_all_signal_connections(p_connections);
  3178. }
  3179. int Node::get_persistent_signal_connection_count() const {
  3180. ERR_THREAD_GUARD_V(0);
  3181. return Object::get_persistent_signal_connection_count();
  3182. }
  3183. void Node::get_signals_connected_to_this(List<Connection> *p_connections) const {
  3184. ERR_THREAD_GUARD;
  3185. Object::get_signals_connected_to_this(p_connections);
  3186. }
  3187. Error Node::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  3188. ERR_THREAD_GUARD_V(ERR_INVALID_PARAMETER);
  3189. return Object::connect(p_signal, p_callable, p_flags);
  3190. }
  3191. void Node::disconnect(const StringName &p_signal, const Callable &p_callable) {
  3192. ERR_THREAD_GUARD;
  3193. Object::disconnect(p_signal, p_callable);
  3194. }
  3195. bool Node::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  3196. ERR_THREAD_GUARD_V(false);
  3197. return Object::is_connected(p_signal, p_callable);
  3198. }
  3199. #endif