node.cpp 107 KB

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