node.cpp 120 KB

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