node.cpp 124 KB

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