node.cpp 131 KB

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