node.cpp 127 KB

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