node.cpp 118 KB

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