node.cpp 105 KB

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