node.cpp 116 KB

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