node.cpp 120 KB

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