node.cpp 120 KB

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