2
0

node.cpp 111 KB

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