node.cpp 119 KB

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