node.cpp 115 KB

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