node.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345
  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/core_string_names.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/message_queue.h"
  34. #include "core/print_string.h"
  35. #include "instance_placeholder.h"
  36. #include "scene/animation/scene_tree_tween.h"
  37. #include "scene/resources/packed_scene.h"
  38. #include "scene/scene_string_names.h"
  39. #include "viewport.h"
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/editor_settings.h"
  42. #endif
  43. VARIANT_ENUM_CAST(Node::PauseMode);
  44. VARIANT_ENUM_CAST(Node::PhysicsInterpolationMode);
  45. int Node::orphan_node_count = 0;
  46. void Node::_notification(int p_notification) {
  47. switch (p_notification) {
  48. case NOTIFICATION_PROCESS: {
  49. if (get_script_instance()) {
  50. Variant time = get_process_delta_time();
  51. const Variant *ptr[1] = { &time };
  52. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_process, ptr, 1);
  53. }
  54. } break;
  55. case NOTIFICATION_PHYSICS_PROCESS: {
  56. if (get_script_instance()) {
  57. Variant time = get_physics_process_delta_time();
  58. const Variant *ptr[1] = { &time };
  59. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_physics_process, ptr, 1);
  60. }
  61. } break;
  62. case NOTIFICATION_ENTER_TREE: {
  63. ERR_FAIL_COND(!get_viewport());
  64. ERR_FAIL_COND(!get_tree());
  65. if (data.pause_mode == PAUSE_MODE_INHERIT) {
  66. if (data.parent) {
  67. data.pause_owner = data.parent->data.pause_owner;
  68. } else {
  69. data.pause_owner = nullptr;
  70. }
  71. } else {
  72. data.pause_owner = this;
  73. }
  74. if (data.physics_interpolation_mode == PHYSICS_INTERPOLATION_MODE_INHERIT) {
  75. bool interpolate = true; // Root node default is for interpolation to be on
  76. if (data.parent) {
  77. interpolate = data.parent->is_physics_interpolated();
  78. }
  79. _propagate_physics_interpolated(interpolate);
  80. }
  81. if (data.input) {
  82. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  83. }
  84. if (data.unhandled_input) {
  85. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  86. }
  87. if (data.unhandled_key_input) {
  88. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  89. }
  90. get_tree()->node_count++;
  91. orphan_node_count--;
  92. } break;
  93. case NOTIFICATION_EXIT_TREE: {
  94. ERR_FAIL_COND(!get_viewport());
  95. ERR_FAIL_COND(!get_tree());
  96. get_tree()->node_count--;
  97. orphan_node_count++;
  98. if (data.input) {
  99. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  100. }
  101. if (data.unhandled_input) {
  102. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  103. }
  104. if (data.unhandled_key_input) {
  105. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  106. }
  107. data.pause_owner = nullptr;
  108. if (data.path_cache) {
  109. memdelete(data.path_cache);
  110. data.path_cache = nullptr;
  111. }
  112. } break;
  113. case NOTIFICATION_PAUSED: {
  114. if (is_physics_interpolated_and_enabled() && is_inside_tree()) {
  115. reset_physics_interpolation();
  116. }
  117. } break;
  118. case NOTIFICATION_PATH_CHANGED: {
  119. if (data.path_cache) {
  120. memdelete(data.path_cache);
  121. data.path_cache = nullptr;
  122. }
  123. } break;
  124. case NOTIFICATION_READY: {
  125. if (get_script_instance()) {
  126. if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_input)) {
  127. set_process_input(true);
  128. }
  129. if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_unhandled_input)) {
  130. set_process_unhandled_input(true);
  131. }
  132. if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_unhandled_key_input)) {
  133. set_process_unhandled_key_input(true);
  134. }
  135. if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_process)) {
  136. set_process(true);
  137. }
  138. if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_physics_process)) {
  139. set_physics_process(true);
  140. }
  141. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_ready, nullptr, 0);
  142. }
  143. } break;
  144. case NOTIFICATION_POSTINITIALIZE: {
  145. data.in_constructor = false;
  146. } break;
  147. case NOTIFICATION_PREDELETE: {
  148. if (data.parent) {
  149. data.parent->remove_child(this);
  150. }
  151. // kill children as cleanly as possible
  152. while (data.children.size()) {
  153. Node *child = data.children[data.children.size() - 1]; //begin from the end because its faster and more consistent with creation
  154. memdelete(child);
  155. }
  156. } break;
  157. }
  158. }
  159. void Node::_propagate_ready() {
  160. data.ready_notified = true;
  161. data.blocked++;
  162. for (int i = 0; i < data.children.size(); i++) {
  163. data.children[i]->_propagate_ready();
  164. }
  165. data.blocked--;
  166. notification(NOTIFICATION_POST_ENTER_TREE);
  167. if (data.ready_first) {
  168. data.ready_first = false;
  169. notification(NOTIFICATION_READY);
  170. emit_signal(SceneStringNames::get_singleton()->ready);
  171. }
  172. }
  173. void Node::_propagate_physics_interpolated(bool p_interpolated) {
  174. switch (data.physics_interpolation_mode) {
  175. case PHYSICS_INTERPOLATION_MODE_INHERIT:
  176. // keep the parent p_interpolated
  177. break;
  178. case PHYSICS_INTERPOLATION_MODE_OFF: {
  179. p_interpolated = false;
  180. } break;
  181. case PHYSICS_INTERPOLATION_MODE_ON: {
  182. p_interpolated = true;
  183. } break;
  184. }
  185. // no change? no need to propagate further
  186. if (data.physics_interpolated == p_interpolated) {
  187. return;
  188. }
  189. data.physics_interpolated = p_interpolated;
  190. // allow a call to the VisualServer etc in derived classes
  191. _physics_interpolated_changed();
  192. update_configuration_warning();
  193. data.blocked++;
  194. for (int i = 0; i < data.children.size(); i++) {
  195. data.children[i]->_propagate_physics_interpolated(p_interpolated);
  196. }
  197. data.blocked--;
  198. }
  199. void Node::_propagate_physics_interpolation_reset_requested(bool p_requested) {
  200. if (is_physics_interpolated()) {
  201. data.physics_interpolation_reset_requested = p_requested;
  202. }
  203. data.blocked++;
  204. for (int i = 0; i < data.children.size(); i++) {
  205. data.children[i]->_propagate_physics_interpolation_reset_requested(p_requested);
  206. }
  207. data.blocked--;
  208. }
  209. void Node::_propagate_enter_tree() {
  210. // this needs to happen to all children before any enter_tree
  211. if (data.parent) {
  212. data.tree = data.parent->data.tree;
  213. data.depth = data.parent->data.depth + 1;
  214. } else {
  215. data.depth = 1;
  216. }
  217. data.viewport = Object::cast_to<Viewport>(this);
  218. if (!data.viewport && data.parent) {
  219. data.viewport = data.parent->data.viewport;
  220. }
  221. data.inside_tree = true;
  222. for (Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  223. E->get().group = data.tree->add_to_group(E->key(), this);
  224. }
  225. notification(NOTIFICATION_ENTER_TREE);
  226. if (get_script_instance()) {
  227. get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_enter_tree, nullptr, 0);
  228. }
  229. emit_signal(SceneStringNames::get_singleton()->tree_entered);
  230. data.tree->node_added(this);
  231. if (data.parent) {
  232. Variant c = this;
  233. const Variant *cptr = &c;
  234. data.parent->emit_signal(SceneStringNames::get_singleton()->child_entered_tree, &cptr, 1);
  235. }
  236. data.blocked++;
  237. //block while adding children
  238. for (int i = 0; i < data.children.size(); i++) {
  239. if (!data.children[i]->is_inside_tree()) { // could have been added in enter_tree
  240. data.children[i]->_propagate_enter_tree();
  241. }
  242. }
  243. data.blocked--;
  244. #ifdef DEBUG_ENABLED
  245. if (ScriptDebugger::get_singleton() && data.filename != String()) {
  246. //used for live edit
  247. data.tree->live_scene_edit_cache[data.filename].insert(this);
  248. }
  249. #endif
  250. // enter groups
  251. }
  252. void Node::_propagate_after_exit_branch(bool p_exiting_tree) {
  253. // Clear owner if it was not part of the pruned branch
  254. if (data.owner) {
  255. bool found = false;
  256. Node *parent = data.parent;
  257. while (parent) {
  258. if (parent == data.owner) {
  259. found = true;
  260. break;
  261. }
  262. parent = parent->data.parent;
  263. }
  264. if (!found) {
  265. if (data.unique_name_in_owner) {
  266. _release_unique_name_in_owner();
  267. }
  268. data.owner->data.owned.erase(data.OW);
  269. data.owner = nullptr;
  270. }
  271. }
  272. data.blocked++;
  273. for (int i = 0; i < data.children.size(); i++) {
  274. data.children[i]->_propagate_after_exit_branch(p_exiting_tree);
  275. }
  276. data.blocked--;
  277. if (p_exiting_tree) {
  278. emit_signal(SceneStringNames::get_singleton()->tree_exited);
  279. }
  280. }
  281. void Node::_propagate_exit_tree() {
  282. //block while removing children
  283. #ifdef DEBUG_ENABLED
  284. if (ScriptDebugger::get_singleton() && data.filename != String()) {
  285. //used for live edit
  286. Map<String, Set<Node *>>::Element *E = data.tree->live_scene_edit_cache.find(data.filename);
  287. if (E) {
  288. E->get().erase(this);
  289. if (E->get().size() == 0) {
  290. data.tree->live_scene_edit_cache.erase(E);
  291. }
  292. }
  293. Map<Node *, Map<ObjectID, Node *>>::Element *F = data.tree->live_edit_remove_list.find(this);
  294. if (F) {
  295. for (Map<ObjectID, Node *>::Element *G = F->get().front(); G; G = G->next()) {
  296. memdelete(G->get());
  297. }
  298. data.tree->live_edit_remove_list.erase(F);
  299. }
  300. }
  301. #endif
  302. data.blocked++;
  303. for (int i = data.children.size() - 1; i >= 0; i--) {
  304. data.children[i]->_propagate_exit_tree();
  305. }
  306. data.blocked--;
  307. if (get_script_instance()) {
  308. get_script_instance()->call_multilevel(SceneStringNames::get_singleton()->_exit_tree, nullptr, 0);
  309. }
  310. emit_signal(SceneStringNames::get_singleton()->tree_exiting);
  311. notification(NOTIFICATION_EXIT_TREE, true);
  312. if (data.tree) {
  313. data.tree->node_removed(this);
  314. }
  315. if (data.parent) {
  316. Variant c = this;
  317. const Variant *cptr = &c;
  318. data.parent->emit_signal(SceneStringNames::get_singleton()->child_exiting_tree, &cptr, 1);
  319. }
  320. // exit groups
  321. for (Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  322. data.tree->remove_from_group(E->key(), this);
  323. E->get().group = nullptr;
  324. }
  325. data.viewport = nullptr;
  326. if (data.tree) {
  327. data.tree->tree_changed();
  328. }
  329. data.inside_tree = false;
  330. data.ready_notified = false;
  331. data.tree = nullptr;
  332. data.depth = -1;
  333. }
  334. void Node::move_child(Node *p_child, int p_pos) {
  335. ERR_FAIL_NULL(p_child);
  336. ERR_FAIL_INDEX_MSG(p_pos, data.children.size() + 1, vformat("Invalid new child position: %d.", p_pos));
  337. ERR_FAIL_COND_MSG(p_child->data.parent != this, "Child is not a child of this node.");
  338. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, move_child() failed. Consider using call_deferred(\"move_child\") instead (or \"popup\" if this is from a popup).");
  339. // Specifying one place beyond the end
  340. // means the same as moving to the last position
  341. if (p_pos == data.children.size()) {
  342. p_pos--;
  343. }
  344. if (p_child->data.pos == p_pos) {
  345. return; //do nothing
  346. }
  347. int motion_from = MIN(p_pos, p_child->data.pos);
  348. int motion_to = MAX(p_pos, p_child->data.pos);
  349. data.children.remove(p_child->data.pos);
  350. data.children.insert(p_pos, 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. data.children[i]->data.pos = i;
  358. }
  359. // notification second
  360. move_child_notify(p_child);
  361. Viewport::notify_canvas_parent_children_moved(*this, motion_from, motion_to + 1);
  362. p_child->_propagate_groups_dirty();
  363. data.blocked--;
  364. }
  365. void Node::raise() {
  366. if (!data.parent) {
  367. return;
  368. }
  369. data.parent->move_child(this, data.parent->data.children.size() - 1);
  370. }
  371. void Node::_propagate_groups_dirty() {
  372. for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  373. if (E->get().group) {
  374. E->get().group->changed = true;
  375. }
  376. }
  377. for (int i = 0; i < data.children.size(); i++) {
  378. data.children[i]->_propagate_groups_dirty();
  379. }
  380. }
  381. void Node::add_child_notify(Node *p_child) {
  382. // to be used when not wanted
  383. }
  384. void Node::remove_child_notify(Node *p_child) {
  385. // to be used when not wanted
  386. }
  387. void Node::move_child_notify(Node *p_child) {
  388. // to be used when not wanted
  389. }
  390. void Node::owner_changed_notify() {
  391. }
  392. void Node::_physics_interpolated_changed() {}
  393. void Node::set_physics_process(bool p_process) {
  394. if (data.physics_process == p_process) {
  395. return;
  396. }
  397. data.physics_process = p_process;
  398. if (data.physics_process) {
  399. add_to_group("physics_process", false);
  400. } else {
  401. remove_from_group("physics_process");
  402. }
  403. _change_notify("physics_process");
  404. }
  405. bool Node::is_physics_processing() const {
  406. return data.physics_process;
  407. }
  408. void Node::set_physics_process_internal(bool p_process_internal) {
  409. if (data.physics_process_internal == p_process_internal) {
  410. return;
  411. }
  412. data.physics_process_internal = p_process_internal;
  413. if (data.physics_process_internal) {
  414. add_to_group("physics_process_internal", false);
  415. } else {
  416. remove_from_group("physics_process_internal");
  417. }
  418. _change_notify("physics_process_internal");
  419. }
  420. bool Node::is_physics_processing_internal() const {
  421. return data.physics_process_internal;
  422. }
  423. void Node::set_pause_mode(PauseMode p_mode) {
  424. if (data.pause_mode == p_mode) {
  425. return;
  426. }
  427. bool prev_inherits = data.pause_mode == PAUSE_MODE_INHERIT;
  428. bool prev_can_process = is_inside_tree() && can_process();
  429. data.pause_mode = p_mode;
  430. if (!is_inside_tree()) {
  431. return; //pointless
  432. }
  433. if ((data.pause_mode == PAUSE_MODE_INHERIT) != prev_inherits) {
  434. Node *owner = nullptr;
  435. if (data.pause_mode == PAUSE_MODE_INHERIT) {
  436. if (data.parent) {
  437. owner = data.parent->data.pause_owner;
  438. }
  439. } else {
  440. owner = this;
  441. }
  442. _propagate_pause_owner(owner);
  443. }
  444. if (prev_can_process != can_process()) {
  445. _propagate_pause_change_notification(can_process() ? NOTIFICATION_UNPAUSED : NOTIFICATION_PAUSED);
  446. }
  447. }
  448. Node::PauseMode Node::get_pause_mode() const {
  449. return data.pause_mode;
  450. }
  451. void Node::_propagate_pause_owner(Node *p_owner) {
  452. if (this != p_owner && data.pause_mode != PAUSE_MODE_INHERIT) {
  453. return;
  454. }
  455. data.pause_owner = p_owner;
  456. for (int i = 0; i < data.children.size(); i++) {
  457. data.children[i]->_propagate_pause_owner(p_owner);
  458. }
  459. }
  460. void Node::_propagate_pause_change_notification(int p_notification) {
  461. notification(p_notification);
  462. for (int i = 0; i < data.children.size(); i++) {
  463. if (data.children[i]->data.pause_mode == PAUSE_MODE_INHERIT) {
  464. data.children[i]->_propagate_pause_change_notification(p_notification);
  465. }
  466. }
  467. }
  468. void Node::set_network_master(int p_peer_id, bool p_recursive) {
  469. data.network_master = p_peer_id;
  470. if (p_recursive) {
  471. for (int i = 0; i < data.children.size(); i++) {
  472. data.children[i]->set_network_master(p_peer_id, true);
  473. }
  474. }
  475. }
  476. int Node::get_network_master() const {
  477. return data.network_master;
  478. }
  479. bool Node::is_network_master() const {
  480. ERR_FAIL_COND_V(!is_inside_tree(), false);
  481. return get_multiplayer()->get_network_unique_id() == data.network_master;
  482. }
  483. /***** RPC CONFIG ********/
  484. void Node::rpc_config(const StringName &p_method, MultiplayerAPI::RPCMode p_mode) {
  485. if (p_mode == MultiplayerAPI::RPC_MODE_DISABLED) {
  486. data.rpc_methods.erase(p_method);
  487. } else {
  488. data.rpc_methods[p_method] = p_mode;
  489. };
  490. }
  491. void Node::rset_config(const StringName &p_property, MultiplayerAPI::RPCMode p_mode) {
  492. if (p_mode == MultiplayerAPI::RPC_MODE_DISABLED) {
  493. data.rpc_properties.erase(p_property);
  494. } else {
  495. data.rpc_properties[p_property] = p_mode;
  496. };
  497. }
  498. /***** RPC FUNCTIONS ********/
  499. void Node::rpc(const StringName &p_method, VARIANT_ARG_DECLARE) {
  500. VARIANT_ARGPTRS;
  501. int argc = 0;
  502. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  503. if (argptr[i]->get_type() == Variant::NIL) {
  504. break;
  505. }
  506. argc++;
  507. }
  508. rpcp(0, false, p_method, argptr, argc);
  509. }
  510. void Node::rpc_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  511. VARIANT_ARGPTRS;
  512. int argc = 0;
  513. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  514. if (argptr[i]->get_type() == Variant::NIL) {
  515. break;
  516. }
  517. argc++;
  518. }
  519. rpcp(p_peer_id, false, p_method, argptr, argc);
  520. }
  521. void Node::rpc_unreliable(const StringName &p_method, VARIANT_ARG_DECLARE) {
  522. VARIANT_ARGPTRS;
  523. int argc = 0;
  524. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  525. if (argptr[i]->get_type() == Variant::NIL) {
  526. break;
  527. }
  528. argc++;
  529. }
  530. rpcp(0, true, p_method, argptr, argc);
  531. }
  532. void Node::rpc_unreliable_id(int p_peer_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  533. VARIANT_ARGPTRS;
  534. int argc = 0;
  535. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  536. if (argptr[i]->get_type() == Variant::NIL) {
  537. break;
  538. }
  539. argc++;
  540. }
  541. rpcp(p_peer_id, true, p_method, argptr, argc);
  542. }
  543. Variant Node::_rpc_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  544. if (p_argcount < 1) {
  545. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  546. r_error.argument = 1;
  547. return Variant();
  548. }
  549. if (p_args[0]->get_type() != Variant::STRING) {
  550. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  551. r_error.argument = 0;
  552. r_error.expected = Variant::STRING;
  553. return Variant();
  554. }
  555. StringName method = *p_args[0];
  556. rpcp(0, false, method, &p_args[1], p_argcount - 1);
  557. r_error.error = Variant::CallError::CALL_OK;
  558. return Variant();
  559. }
  560. Variant Node::_rpc_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  561. if (p_argcount < 2) {
  562. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  563. r_error.argument = 2;
  564. return Variant();
  565. }
  566. if (p_args[0]->get_type() != Variant::INT) {
  567. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  568. r_error.argument = 0;
  569. r_error.expected = Variant::INT;
  570. return Variant();
  571. }
  572. if (p_args[1]->get_type() != Variant::STRING) {
  573. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  574. r_error.argument = 1;
  575. r_error.expected = Variant::STRING;
  576. return Variant();
  577. }
  578. int peer_id = *p_args[0];
  579. StringName method = *p_args[1];
  580. rpcp(peer_id, false, method, &p_args[2], p_argcount - 2);
  581. r_error.error = Variant::CallError::CALL_OK;
  582. return Variant();
  583. }
  584. Variant Node::_rpc_unreliable_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  585. if (p_argcount < 1) {
  586. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  587. r_error.argument = 1;
  588. return Variant();
  589. }
  590. if (p_args[0]->get_type() != Variant::STRING) {
  591. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  592. r_error.argument = 0;
  593. r_error.expected = Variant::STRING;
  594. return Variant();
  595. }
  596. StringName method = *p_args[0];
  597. rpcp(0, true, method, &p_args[1], p_argcount - 1);
  598. r_error.error = Variant::CallError::CALL_OK;
  599. return Variant();
  600. }
  601. Variant Node::_rpc_unreliable_id_bind(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  602. if (p_argcount < 2) {
  603. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  604. r_error.argument = 2;
  605. return Variant();
  606. }
  607. if (p_args[0]->get_type() != Variant::INT) {
  608. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  609. r_error.argument = 0;
  610. r_error.expected = Variant::INT;
  611. return Variant();
  612. }
  613. if (p_args[1]->get_type() != Variant::STRING) {
  614. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  615. r_error.argument = 1;
  616. r_error.expected = Variant::STRING;
  617. return Variant();
  618. }
  619. int peer_id = *p_args[0];
  620. StringName method = *p_args[1];
  621. rpcp(peer_id, true, method, &p_args[2], p_argcount - 2);
  622. r_error.error = Variant::CallError::CALL_OK;
  623. return Variant();
  624. }
  625. void Node::rpcp(int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) {
  626. ERR_FAIL_COND(!is_inside_tree());
  627. get_multiplayer()->rpcp(this, p_peer_id, p_unreliable, p_method, p_arg, p_argcount);
  628. }
  629. void Node::rsetp(int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) {
  630. ERR_FAIL_COND(!is_inside_tree());
  631. get_multiplayer()->rsetp(this, p_peer_id, p_unreliable, p_property, p_value);
  632. }
  633. /******** RSET *********/
  634. void Node::rset(const StringName &p_property, const Variant &p_value) {
  635. rsetp(0, false, p_property, p_value);
  636. }
  637. void Node::rset_id(int p_peer_id, const StringName &p_property, const Variant &p_value) {
  638. rsetp(p_peer_id, false, p_property, p_value);
  639. }
  640. void Node::rset_unreliable(const StringName &p_property, const Variant &p_value) {
  641. rsetp(0, true, p_property, p_value);
  642. }
  643. void Node::rset_unreliable_id(int p_peer_id, const StringName &p_property, const Variant &p_value) {
  644. rsetp(p_peer_id, true, p_property, p_value);
  645. }
  646. //////////// end of rpc
  647. Ref<MultiplayerAPI> Node::get_multiplayer() const {
  648. if (multiplayer.is_valid()) {
  649. return multiplayer;
  650. }
  651. if (!is_inside_tree()) {
  652. return Ref<MultiplayerAPI>();
  653. }
  654. return get_tree()->get_multiplayer();
  655. }
  656. Ref<MultiplayerAPI> Node::get_custom_multiplayer() const {
  657. return multiplayer;
  658. }
  659. void Node::set_custom_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {
  660. multiplayer = p_multiplayer;
  661. }
  662. const Map<StringName, MultiplayerAPI::RPCMode>::Element *Node::get_node_rpc_mode(const StringName &p_method) {
  663. return data.rpc_methods.find(p_method);
  664. }
  665. const Map<StringName, MultiplayerAPI::RPCMode>::Element *Node::get_node_rset_mode(const StringName &p_property) {
  666. return data.rpc_properties.find(p_property);
  667. }
  668. bool Node::can_process_notification(int p_what) const {
  669. switch (p_what) {
  670. case NOTIFICATION_PHYSICS_PROCESS:
  671. return data.physics_process;
  672. case NOTIFICATION_PROCESS:
  673. return data.idle_process;
  674. case NOTIFICATION_INTERNAL_PROCESS:
  675. return data.idle_process_internal;
  676. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
  677. return data.physics_process_internal;
  678. }
  679. return true;
  680. }
  681. bool Node::can_process() const {
  682. ERR_FAIL_COND_V(!is_inside_tree(), false);
  683. if (get_tree()->is_paused()) {
  684. if (data.pause_mode == PAUSE_MODE_STOP) {
  685. return false;
  686. }
  687. if (data.pause_mode == PAUSE_MODE_PROCESS) {
  688. return true;
  689. }
  690. if (data.pause_mode == PAUSE_MODE_INHERIT) {
  691. if (!data.pause_owner) {
  692. return false; //clearly no pause owner by default
  693. }
  694. if (data.pause_owner->data.pause_mode == PAUSE_MODE_PROCESS) {
  695. return true;
  696. }
  697. if (data.pause_owner->data.pause_mode == PAUSE_MODE_STOP) {
  698. return false;
  699. }
  700. }
  701. }
  702. return true;
  703. }
  704. void Node::set_physics_interpolation_mode(PhysicsInterpolationMode p_mode) {
  705. if (data.physics_interpolation_mode == p_mode) {
  706. return;
  707. }
  708. data.physics_interpolation_mode = p_mode;
  709. bool interpolate = true; // default for root node
  710. switch (p_mode) {
  711. case PHYSICS_INTERPOLATION_MODE_INHERIT: {
  712. if (is_inside_tree() && data.parent) {
  713. interpolate = data.parent->is_physics_interpolated();
  714. }
  715. } break;
  716. case PHYSICS_INTERPOLATION_MODE_OFF: {
  717. interpolate = false;
  718. } break;
  719. case PHYSICS_INTERPOLATION_MODE_ON: {
  720. interpolate = true;
  721. } break;
  722. }
  723. _propagate_physics_interpolated(interpolate);
  724. // Auto-reset on changing interpolation mode.
  725. if (is_physics_interpolated() && is_inside_tree()) {
  726. propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  727. }
  728. }
  729. void Node::reset_physics_interpolation() {
  730. if (is_inside_tree()) {
  731. propagate_notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  732. // If `reset_physics_interpolation()` is called explicitly by the user
  733. // (e.g. from scripts) then we prevent deferred auto-resets taking place.
  734. // The user is trusted to call reset in the right order, and auto-reset
  735. // will interfere with their control of prev / curr, so should be turned off.
  736. _propagate_physics_interpolation_reset_requested(false);
  737. }
  738. }
  739. float Node::get_physics_process_delta_time() const {
  740. if (data.tree) {
  741. return data.tree->get_physics_process_time();
  742. } else {
  743. return 0;
  744. }
  745. }
  746. float Node::get_process_delta_time() const {
  747. if (data.tree) {
  748. return data.tree->get_idle_process_time();
  749. } else {
  750. return 0;
  751. }
  752. }
  753. void Node::set_process(bool p_idle_process) {
  754. if (data.idle_process == p_idle_process) {
  755. return;
  756. }
  757. data.idle_process = p_idle_process;
  758. if (data.idle_process) {
  759. add_to_group("idle_process", false);
  760. } else {
  761. remove_from_group("idle_process");
  762. }
  763. _change_notify("idle_process");
  764. }
  765. bool Node::is_processing() const {
  766. return data.idle_process;
  767. }
  768. void Node::set_process_internal(bool p_idle_process_internal) {
  769. if (data.idle_process_internal == p_idle_process_internal) {
  770. return;
  771. }
  772. data.idle_process_internal = p_idle_process_internal;
  773. if (data.idle_process_internal) {
  774. add_to_group("idle_process_internal", false);
  775. } else {
  776. remove_from_group("idle_process_internal");
  777. }
  778. _change_notify("idle_process_internal");
  779. }
  780. bool Node::is_processing_internal() const {
  781. return data.idle_process_internal;
  782. }
  783. void Node::set_process_priority(int p_priority) {
  784. data.process_priority = p_priority;
  785. // Make sure we are in SceneTree.
  786. if (data.tree == nullptr) {
  787. return;
  788. }
  789. if (is_processing()) {
  790. data.tree->make_group_changed("idle_process");
  791. }
  792. if (is_processing_internal()) {
  793. data.tree->make_group_changed("idle_process_internal");
  794. }
  795. if (is_physics_processing()) {
  796. data.tree->make_group_changed("physics_process");
  797. }
  798. if (is_physics_processing_internal()) {
  799. data.tree->make_group_changed("physics_process_internal");
  800. }
  801. }
  802. int Node::get_process_priority() const {
  803. return data.process_priority;
  804. }
  805. void Node::set_process_input(bool p_enable) {
  806. if (p_enable == data.input) {
  807. return;
  808. }
  809. data.input = p_enable;
  810. if (!is_inside_tree()) {
  811. return;
  812. }
  813. if (p_enable) {
  814. add_to_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  815. } else {
  816. remove_from_group("_vp_input" + itos(get_viewport()->get_instance_id()));
  817. }
  818. }
  819. bool Node::is_processing_input() const {
  820. return data.input;
  821. }
  822. void Node::set_process_unhandled_input(bool p_enable) {
  823. if (p_enable == data.unhandled_input) {
  824. return;
  825. }
  826. data.unhandled_input = p_enable;
  827. if (!is_inside_tree()) {
  828. return;
  829. }
  830. if (p_enable) {
  831. add_to_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  832. } else {
  833. remove_from_group("_vp_unhandled_input" + itos(get_viewport()->get_instance_id()));
  834. }
  835. }
  836. bool Node::is_processing_unhandled_input() const {
  837. return data.unhandled_input;
  838. }
  839. void Node::set_process_unhandled_key_input(bool p_enable) {
  840. if (p_enable == data.unhandled_key_input) {
  841. return;
  842. }
  843. data.unhandled_key_input = p_enable;
  844. if (!is_inside_tree()) {
  845. return;
  846. }
  847. if (p_enable) {
  848. add_to_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  849. } else {
  850. remove_from_group("_vp_unhandled_key_input" + itos(get_viewport()->get_instance_id()));
  851. }
  852. }
  853. bool Node::is_processing_unhandled_key_input() const {
  854. return data.unhandled_key_input;
  855. }
  856. void Node::_set_physics_interpolated_client_side(bool p_enable) {
  857. data.physics_interpolated_client_side = p_enable;
  858. }
  859. void Node::_set_physics_interpolation_reset_requested(bool p_enable) {
  860. data.physics_interpolation_reset_requested = p_enable;
  861. }
  862. void Node::_set_use_identity_transform(bool p_enable) {
  863. data.use_identity_transform = p_enable;
  864. }
  865. StringName Node::get_name() const {
  866. return data.name;
  867. }
  868. void Node::_set_name_nocheck(const StringName &p_name) {
  869. data.name = p_name;
  870. #ifdef DEV_ENABLED
  871. _name_changed_notify();
  872. #endif
  873. }
  874. #ifdef DEV_ENABLED
  875. void Node::_name_changed_notify() {
  876. }
  877. #endif
  878. void Node::set_name(const String &p_name) {
  879. String name = p_name.validate_node_name();
  880. ERR_FAIL_COND(name == "");
  881. if (data.unique_name_in_owner && data.owner) {
  882. _release_unique_name_in_owner();
  883. }
  884. data.name = name;
  885. if (data.parent) {
  886. data.parent->_validate_child_name(this);
  887. }
  888. if (data.unique_name_in_owner && data.owner) {
  889. _acquire_unique_name_in_owner();
  890. }
  891. propagate_notification(NOTIFICATION_PATH_CHANGED);
  892. if (is_inside_tree()) {
  893. emit_signal("renamed");
  894. get_tree()->node_renamed(this);
  895. get_tree()->tree_changed();
  896. }
  897. #ifdef DEV_ENABLED
  898. _name_changed_notify();
  899. #endif
  900. }
  901. static bool node_hrcr = false;
  902. static SafeRefCount node_hrcr_count;
  903. void Node::init_node_hrcr() {
  904. node_hrcr_count.init(1);
  905. }
  906. void Node::set_human_readable_collision_renaming(bool p_enabled) {
  907. node_hrcr = p_enabled;
  908. }
  909. #ifdef TOOLS_ENABLED
  910. String Node::validate_child_name(Node *p_child) {
  911. StringName name = p_child->data.name;
  912. _generate_serial_child_name(p_child, name);
  913. return name;
  914. }
  915. #endif
  916. void Node::_validate_child_name(Node *p_child, bool p_force_human_readable) {
  917. /* Make sure the name is unique */
  918. if (node_hrcr || p_force_human_readable) {
  919. //this approach to autoset node names is human readable but very slow
  920. //it's turned on while running in the editor
  921. StringName name = p_child->data.name;
  922. _generate_serial_child_name(p_child, name);
  923. p_child->data.name = name;
  924. } else {
  925. //this approach to autoset node names is fast but not as readable
  926. //it's the default and reserves the '@' character for unique names.
  927. bool unique = true;
  928. if (p_child->data.name == StringName()) {
  929. //new unique name must be assigned
  930. unique = false;
  931. } else {
  932. //check if exists
  933. Node **children = data.children.ptr();
  934. int cc = data.children.size();
  935. for (int i = 0; i < cc; i++) {
  936. if (children[i] == p_child) {
  937. continue;
  938. }
  939. if (children[i]->data.name == p_child->data.name) {
  940. unique = false;
  941. break;
  942. }
  943. }
  944. }
  945. if (!unique) {
  946. ERR_FAIL_COND(!node_hrcr_count.ref());
  947. String name = "@" + String(p_child->get_name()) + "@" + itos(node_hrcr_count.get());
  948. p_child->data.name = name;
  949. }
  950. }
  951. }
  952. // Return s + 1 as if it were an integer
  953. String increase_numeric_string(const String &s) {
  954. String res = s;
  955. bool carry = res.length() > 0;
  956. for (int i = res.length() - 1; i >= 0; i--) {
  957. if (!carry) {
  958. break;
  959. }
  960. CharType n = s[i];
  961. if (n == '9') { // keep carry as true: 9 + 1
  962. res[i] = '0';
  963. } else {
  964. res[i] = s[i] + 1;
  965. carry = false;
  966. }
  967. }
  968. if (carry) {
  969. res = "1" + res;
  970. }
  971. return res;
  972. }
  973. void Node::_generate_serial_child_name(const Node *p_child, StringName &name) const {
  974. if (name == StringName()) {
  975. //no name and a new nade is needed, create one.
  976. name = p_child->get_class();
  977. // Adjust casing according to project setting. The current type name is expected to be in PascalCase.
  978. switch (ProjectSettings::get_singleton()->get("node/name_casing").operator int()) {
  979. case NAME_CASING_PASCAL_CASE:
  980. break;
  981. case NAME_CASING_CAMEL_CASE: {
  982. String n = name;
  983. n[0] = n.to_lower()[0];
  984. name = n;
  985. } break;
  986. case NAME_CASING_SNAKE_CASE:
  987. name = String(name).camelcase_to_underscore(true);
  988. break;
  989. }
  990. }
  991. //quickly test if proposed name exists
  992. int cc = data.children.size(); //children count
  993. const Node *const *children_ptr = data.children.ptr();
  994. {
  995. bool exists = false;
  996. for (int i = 0; i < cc; i++) {
  997. if (children_ptr[i] == p_child) { //exclude self in renaming if its already a child
  998. continue;
  999. }
  1000. if (children_ptr[i]->data.name == name) {
  1001. exists = true;
  1002. }
  1003. }
  1004. if (!exists) {
  1005. return; //if it does not exist, it does not need validation
  1006. }
  1007. }
  1008. // Extract trailing number
  1009. String name_string = name;
  1010. String nums;
  1011. for (int i = name_string.length() - 1; i >= 0; i--) {
  1012. CharType n = name_string[i];
  1013. if (n >= '0' && n <= '9') {
  1014. nums = String::chr(name_string[i]) + nums;
  1015. } else {
  1016. break;
  1017. }
  1018. }
  1019. String nnsep = _get_name_num_separator();
  1020. int name_last_index = name_string.length() - nnsep.length() - nums.length();
  1021. // Assign the base name + separator to name if we have numbers preceded by a separator
  1022. if (nums.length() > 0 && name_string.substr(name_last_index, nnsep.length()) == nnsep) {
  1023. name_string = name_string.substr(0, name_last_index + nnsep.length());
  1024. } else {
  1025. nums = "";
  1026. }
  1027. for (;;) {
  1028. StringName attempt = name_string + nums;
  1029. bool exists = false;
  1030. for (int i = 0; i < cc; i++) {
  1031. if (children_ptr[i] == p_child) {
  1032. continue;
  1033. }
  1034. if (children_ptr[i]->data.name == attempt) {
  1035. exists = true;
  1036. }
  1037. }
  1038. if (!exists) {
  1039. name = attempt;
  1040. return;
  1041. } else {
  1042. if (nums.length() == 0) {
  1043. // Name was undecorated so skip to 2 for a more natural result
  1044. nums = "2";
  1045. name_string += nnsep; // Add separator because nums.length() > 0 was false
  1046. } else {
  1047. nums = increase_numeric_string(nums);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. void Node::_add_child_nocheck(Node *p_child, const StringName &p_name) {
  1053. //add a child node quickly, without name validation
  1054. p_child->_set_name_nocheck(p_name);
  1055. p_child->data.pos = data.children.size();
  1056. data.children.push_back(p_child);
  1057. p_child->data.parent = this;
  1058. p_child->notification(NOTIFICATION_PARENTED);
  1059. if (data.tree) {
  1060. p_child->_set_tree(data.tree);
  1061. }
  1062. /* Notify */
  1063. //recognize children created in this node constructor
  1064. p_child->data.parent_owned = data.in_constructor;
  1065. add_child_notify(p_child);
  1066. }
  1067. void Node::add_child(Node *p_child, bool p_force_readable_name) {
  1068. ERR_FAIL_NULL(p_child);
  1069. ERR_FAIL_COND_MSG(p_child == this, vformat("Can't add child '%s' to itself.", p_child->get_name())); // adding to itself!
  1070. 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
  1071. #ifdef DEBUG_ENABLED
  1072. ERR_FAIL_COND_MSG(p_child->is_a_parent_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()));
  1073. #endif
  1074. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, add_node() failed. Consider using call_deferred(\"add_child\", child) instead.");
  1075. /* Validate name */
  1076. _validate_child_name(p_child, p_force_readable_name);
  1077. _add_child_nocheck(p_child, p_child->data.name);
  1078. }
  1079. void Node::add_child_below_node(Node *p_node, Node *p_child, bool p_force_readable_name) {
  1080. ERR_FAIL_NULL(p_node);
  1081. ERR_FAIL_NULL(p_child);
  1082. add_child(p_child, p_force_readable_name);
  1083. if (p_node->data.parent == this) {
  1084. move_child(p_child, p_node->get_position_in_parent() + 1);
  1085. } else {
  1086. WARN_PRINT("Cannot move under node " + p_node->get_name() + " as " + p_child->get_name() + " does not share a parent.");
  1087. }
  1088. }
  1089. void Node::remove_child(Node *p_child) {
  1090. ERR_FAIL_NULL(p_child);
  1091. ERR_FAIL_COND_MSG(data.blocked > 0, "Parent node is busy setting up children, remove_node() failed. Consider using call_deferred(\"remove_child\", child) instead.");
  1092. int child_count = data.children.size();
  1093. Node **children = data.children.ptr();
  1094. int idx = -1;
  1095. if (p_child->data.pos >= 0 && p_child->data.pos < child_count) {
  1096. if (children[p_child->data.pos] == p_child) {
  1097. idx = p_child->data.pos;
  1098. }
  1099. }
  1100. if (idx == -1) { //maybe removed while unparenting or something and index was not updated, so just in case the above fails, try this.
  1101. for (int i = 0; i < child_count; i++) {
  1102. if (children[i] == p_child) {
  1103. idx = i;
  1104. break;
  1105. }
  1106. }
  1107. }
  1108. ERR_FAIL_COND_MSG(idx == -1, vformat("Cannot remove child node '%s' as it is not a child of this node.", p_child->get_name()));
  1109. //ERR_FAIL_COND( p_child->data.blocked > 0 );
  1110. //if (data.scene) { does not matter
  1111. p_child->_set_tree(nullptr);
  1112. //}
  1113. remove_child_notify(p_child);
  1114. p_child->notification(NOTIFICATION_UNPARENTED);
  1115. data.children.remove(idx);
  1116. //update pointer and size
  1117. child_count = data.children.size();
  1118. children = data.children.ptr();
  1119. for (int i = idx; i < child_count; i++) {
  1120. children[i]->data.pos = i;
  1121. }
  1122. Viewport::notify_canvas_parent_children_moved(*this, idx, child_count);
  1123. Viewport::notify_canvas_parent_child_count_reduced(*this);
  1124. p_child->data.parent = nullptr;
  1125. p_child->data.pos = -1;
  1126. p_child->_propagate_after_exit_branch(data.inside_tree);
  1127. }
  1128. int Node::get_child_count() const {
  1129. return data.children.size();
  1130. }
  1131. Node *Node::get_child(int p_index) const {
  1132. ERR_FAIL_INDEX_V(p_index, data.children.size(), nullptr);
  1133. return data.children[p_index];
  1134. }
  1135. Node *Node::_get_child_by_name(const StringName &p_name) const {
  1136. int cc = data.children.size();
  1137. Node *const *cd = data.children.ptr();
  1138. for (int i = 0; i < cc; i++) {
  1139. if (cd[i]->data.name == p_name) {
  1140. return cd[i];
  1141. }
  1142. }
  1143. return nullptr;
  1144. }
  1145. Node *Node::get_node_or_null(const NodePath &p_path) const {
  1146. if (p_path.is_empty()) {
  1147. return nullptr;
  1148. }
  1149. 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.");
  1150. Node *current = nullptr;
  1151. Node *root = nullptr;
  1152. if (!p_path.is_absolute()) {
  1153. current = const_cast<Node *>(this); //start from this
  1154. } else {
  1155. root = const_cast<Node *>(this);
  1156. while (root->data.parent) {
  1157. root = root->data.parent; //start from root
  1158. }
  1159. }
  1160. for (int i = 0; i < p_path.get_name_count(); i++) {
  1161. StringName name = p_path.get_name(i);
  1162. Node *next = nullptr;
  1163. if (name == SceneStringNames::get_singleton()->dot) { // .
  1164. next = current;
  1165. } else if (name == SceneStringNames::get_singleton()->doubledot) { // ..
  1166. if (current == nullptr || !current->data.parent) {
  1167. return nullptr;
  1168. }
  1169. next = current->data.parent;
  1170. } else if (current == nullptr) {
  1171. if (name == root->get_name()) {
  1172. next = root;
  1173. }
  1174. } else if (name.is_node_unique_name()) {
  1175. Node **unique = current->data.owned_unique_nodes.getptr(name);
  1176. if (!unique && current->data.owner) {
  1177. unique = current->data.owner->data.owned_unique_nodes.getptr(name);
  1178. }
  1179. if (!unique) {
  1180. return nullptr;
  1181. }
  1182. next = *unique;
  1183. } else {
  1184. next = nullptr;
  1185. for (int j = 0; j < current->data.children.size(); j++) {
  1186. Node *child = current->data.children[j];
  1187. if (child->data.name == name) {
  1188. next = child;
  1189. break;
  1190. }
  1191. }
  1192. if (next == nullptr) {
  1193. return nullptr;
  1194. };
  1195. }
  1196. current = next;
  1197. }
  1198. return current;
  1199. }
  1200. Node *Node::get_node(const NodePath &p_path) const {
  1201. Node *node = get_node_or_null(p_path);
  1202. if (unlikely(!node)) {
  1203. // Try to get a clear description of this node in the error message.
  1204. String desc;
  1205. if (is_inside_tree()) {
  1206. desc = get_path();
  1207. } else {
  1208. desc = get_name();
  1209. if (desc.empty()) {
  1210. desc = get_class();
  1211. }
  1212. }
  1213. if (p_path.is_absolute()) {
  1214. ERR_FAIL_V_MSG(nullptr,
  1215. vformat("(Node not found: \"%s\" (absolute path attempted from \"%s\").)", p_path, desc));
  1216. } else {
  1217. ERR_FAIL_V_MSG(nullptr,
  1218. vformat("(Node not found: \"%s\" (relative to \"%s\").)", p_path, desc));
  1219. }
  1220. }
  1221. return node;
  1222. }
  1223. bool Node::has_node(const NodePath &p_path) const {
  1224. return get_node_or_null(p_path) != nullptr;
  1225. }
  1226. Node *Node::find_node(const String &p_mask, bool p_recursive, bool p_owned) const {
  1227. Node *const *cptr = data.children.ptr();
  1228. int ccount = data.children.size();
  1229. for (int i = 0; i < ccount; i++) {
  1230. if (p_owned && !cptr[i]->data.owner) {
  1231. continue;
  1232. }
  1233. if (cptr[i]->data.name.operator String().match(p_mask)) {
  1234. return cptr[i];
  1235. }
  1236. if (!p_recursive) {
  1237. continue;
  1238. }
  1239. Node *ret = cptr[i]->find_node(p_mask, true, p_owned);
  1240. if (ret) {
  1241. return ret;
  1242. }
  1243. }
  1244. return nullptr;
  1245. }
  1246. Node *Node::get_parent() const {
  1247. return data.parent;
  1248. }
  1249. Node *Node::find_parent(const String &p_mask) const {
  1250. Node *p = data.parent;
  1251. while (p) {
  1252. if (p->data.name.operator String().match(p_mask)) {
  1253. return p;
  1254. }
  1255. p = p->data.parent;
  1256. }
  1257. return nullptr;
  1258. }
  1259. bool Node::is_a_parent_of(const Node *p_node) const {
  1260. ERR_FAIL_NULL_V(p_node, false);
  1261. Node *p = p_node->data.parent;
  1262. while (p) {
  1263. if (p == this) {
  1264. return true;
  1265. }
  1266. p = p->data.parent;
  1267. }
  1268. return false;
  1269. }
  1270. bool Node::is_greater_than(const Node *p_node) const {
  1271. ERR_FAIL_NULL_V(p_node, false);
  1272. ERR_FAIL_COND_V(!data.inside_tree, false);
  1273. ERR_FAIL_COND_V(!p_node->data.inside_tree, false);
  1274. ERR_FAIL_COND_V(data.depth < 0, false);
  1275. ERR_FAIL_COND_V(p_node->data.depth < 0, false);
  1276. #ifdef NO_ALLOCA
  1277. Vector<int> this_stack;
  1278. Vector<int> that_stack;
  1279. this_stack.resize(data.depth);
  1280. that_stack.resize(p_node->data.depth);
  1281. #else
  1282. int *this_stack = (int *)alloca(sizeof(int) * data.depth);
  1283. int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
  1284. #endif
  1285. const Node *n = this;
  1286. int idx = data.depth - 1;
  1287. while (n) {
  1288. ERR_FAIL_INDEX_V(idx, data.depth, false);
  1289. this_stack[idx--] = n->data.pos;
  1290. n = n->data.parent;
  1291. }
  1292. ERR_FAIL_COND_V(idx != -1, false);
  1293. n = p_node;
  1294. idx = p_node->data.depth - 1;
  1295. while (n) {
  1296. ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
  1297. that_stack[idx--] = n->data.pos;
  1298. n = n->data.parent;
  1299. }
  1300. ERR_FAIL_COND_V(idx != -1, false);
  1301. idx = 0;
  1302. bool res;
  1303. while (true) {
  1304. // using -2 since out-of-tree or nonroot nodes have -1
  1305. int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
  1306. int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
  1307. if (this_idx > that_idx) {
  1308. res = true;
  1309. break;
  1310. } else if (this_idx < that_idx) {
  1311. res = false;
  1312. break;
  1313. } else if (this_idx == -2) {
  1314. res = false; // equal
  1315. break;
  1316. }
  1317. idx++;
  1318. }
  1319. return res;
  1320. }
  1321. void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
  1322. if (data.owner == p_by) {
  1323. p_owned->push_back(this);
  1324. }
  1325. for (int i = 0; i < get_child_count(); i++) {
  1326. get_child(i)->get_owned_by(p_by, p_owned);
  1327. }
  1328. }
  1329. void Node::_set_owner_nocheck(Node *p_owner) {
  1330. if (data.owner == p_owner) {
  1331. return;
  1332. }
  1333. ERR_FAIL_COND(data.owner);
  1334. data.owner = p_owner;
  1335. data.owner->data.owned.push_back(this);
  1336. data.OW = data.owner->data.owned.back();
  1337. owner_changed_notify();
  1338. }
  1339. void Node::_release_unique_name_in_owner() {
  1340. ERR_FAIL_NULL(data.owner); // Sanity check.
  1341. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1342. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1343. if (which == nullptr || *which != this) {
  1344. return; // Ignore.
  1345. }
  1346. data.owner->data.owned_unique_nodes.erase(key);
  1347. }
  1348. void Node::_acquire_unique_name_in_owner() {
  1349. ERR_FAIL_NULL(data.owner); // Sanity check.
  1350. StringName key = StringName(UNIQUE_NODE_PREFIX + data.name.operator String());
  1351. Node **which = data.owner->data.owned_unique_nodes.getptr(key);
  1352. if (which != nullptr && *which != this) {
  1353. WARN_PRINT(vformat(RTR("Setting node name '%s' to be unique within scene for '%s', but it's already claimed by '%s'. This node is no longer set unique."), get_name(), is_inside_tree() ? get_path() : data.owner->get_path_to(this), is_inside_tree() ? (*which)->get_path() : data.owner->get_path_to(*which)));
  1354. data.unique_name_in_owner = false;
  1355. return;
  1356. }
  1357. data.owner->data.owned_unique_nodes[key] = this;
  1358. }
  1359. void Node::set_unique_name_in_owner(bool p_enabled) {
  1360. if (data.unique_name_in_owner == p_enabled) {
  1361. return;
  1362. }
  1363. if (data.unique_name_in_owner && data.owner != nullptr) {
  1364. _release_unique_name_in_owner();
  1365. }
  1366. data.unique_name_in_owner = p_enabled;
  1367. if (data.unique_name_in_owner && data.owner != nullptr) {
  1368. _acquire_unique_name_in_owner();
  1369. }
  1370. update_configuration_warning();
  1371. }
  1372. bool Node::is_unique_name_in_owner() const {
  1373. return data.unique_name_in_owner;
  1374. }
  1375. void Node::set_owner(Node *p_owner) {
  1376. if (data.owner) {
  1377. if (data.unique_name_in_owner) {
  1378. _release_unique_name_in_owner();
  1379. }
  1380. data.owner->data.owned.erase(data.OW);
  1381. data.OW = nullptr;
  1382. data.owner = nullptr;
  1383. }
  1384. ERR_FAIL_COND(p_owner == this);
  1385. if (!p_owner) {
  1386. return;
  1387. }
  1388. Node *check = this->get_parent();
  1389. bool owner_valid = false;
  1390. while (check) {
  1391. if (check == p_owner) {
  1392. owner_valid = true;
  1393. break;
  1394. }
  1395. check = check->data.parent;
  1396. }
  1397. ERR_FAIL_COND(!owner_valid);
  1398. _set_owner_nocheck(p_owner);
  1399. if (data.unique_name_in_owner) {
  1400. _acquire_unique_name_in_owner();
  1401. }
  1402. }
  1403. Node *Node::get_owner() const {
  1404. return data.owner;
  1405. }
  1406. Node *Node::find_common_parent_with(const Node *p_node) const {
  1407. if (this == p_node) {
  1408. return const_cast<Node *>(p_node);
  1409. }
  1410. Set<const Node *> visited;
  1411. const Node *n = this;
  1412. while (n) {
  1413. visited.insert(n);
  1414. n = n->data.parent;
  1415. }
  1416. const Node *common_parent = p_node;
  1417. while (common_parent) {
  1418. if (visited.has(common_parent)) {
  1419. break;
  1420. }
  1421. common_parent = common_parent->data.parent;
  1422. }
  1423. if (!common_parent) {
  1424. return nullptr;
  1425. }
  1426. return const_cast<Node *>(common_parent);
  1427. }
  1428. NodePath Node::get_path_to(const Node *p_node) const {
  1429. ERR_FAIL_NULL_V(p_node, NodePath());
  1430. if (this == p_node) {
  1431. return NodePath(".");
  1432. }
  1433. Set<const Node *> visited;
  1434. const Node *n = this;
  1435. while (n) {
  1436. visited.insert(n);
  1437. n = n->data.parent;
  1438. }
  1439. const Node *common_parent = p_node;
  1440. while (common_parent) {
  1441. if (visited.has(common_parent)) {
  1442. break;
  1443. }
  1444. common_parent = common_parent->data.parent;
  1445. }
  1446. ERR_FAIL_COND_V(!common_parent, NodePath()); //nodes not in the same tree
  1447. visited.clear();
  1448. Vector<StringName> path;
  1449. n = p_node;
  1450. while (n != common_parent) {
  1451. path.push_back(n->get_name());
  1452. n = n->data.parent;
  1453. }
  1454. n = this;
  1455. StringName up = String("..");
  1456. while (n != common_parent) {
  1457. path.push_back(up);
  1458. n = n->data.parent;
  1459. }
  1460. path.invert();
  1461. return NodePath(path, false);
  1462. }
  1463. NodePath Node::get_path() const {
  1464. ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
  1465. if (data.path_cache) {
  1466. return *data.path_cache;
  1467. }
  1468. const Node *n = this;
  1469. Vector<StringName> path;
  1470. while (n) {
  1471. path.push_back(n->get_name());
  1472. n = n->data.parent;
  1473. }
  1474. path.invert();
  1475. data.path_cache = memnew(NodePath(path, true));
  1476. return *data.path_cache;
  1477. }
  1478. bool Node::is_in_group(const StringName &p_identifier) const {
  1479. return data.grouped.has(p_identifier);
  1480. }
  1481. void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
  1482. ERR_FAIL_COND(!p_identifier.operator String().length());
  1483. if (data.grouped.has(p_identifier)) {
  1484. return;
  1485. }
  1486. GroupData gd;
  1487. if (data.tree) {
  1488. gd.group = data.tree->add_to_group(p_identifier, this);
  1489. } else {
  1490. gd.group = nullptr;
  1491. }
  1492. gd.persistent = p_persistent;
  1493. data.grouped[p_identifier] = gd;
  1494. }
  1495. void Node::remove_from_group(const StringName &p_identifier) {
  1496. ERR_FAIL_COND(!data.grouped.has(p_identifier));
  1497. Map<StringName, GroupData>::Element *E = data.grouped.find(p_identifier);
  1498. ERR_FAIL_COND(!E);
  1499. if (data.tree) {
  1500. data.tree->remove_from_group(E->key(), this);
  1501. }
  1502. data.grouped.erase(E);
  1503. }
  1504. Array Node::_get_groups() const {
  1505. Array groups;
  1506. List<GroupInfo> gi;
  1507. get_groups(&gi);
  1508. for (List<GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  1509. groups.push_back(E->get().name);
  1510. }
  1511. return groups;
  1512. }
  1513. void Node::get_groups(List<GroupInfo> *p_groups) const {
  1514. for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  1515. GroupInfo gi;
  1516. gi.name = E->key();
  1517. gi.persistent = E->get().persistent;
  1518. p_groups->push_back(gi);
  1519. }
  1520. }
  1521. int Node::get_persistent_group_count() const {
  1522. int count = 0;
  1523. for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  1524. if (E->get().persistent) {
  1525. count += 1;
  1526. }
  1527. }
  1528. return count;
  1529. }
  1530. void Node::_print_tree_pretty(const String &prefix, const bool last) {
  1531. String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴");
  1532. print_line(prefix + new_prefix + String(get_name()));
  1533. for (int i = 0; i < data.children.size(); i++) {
  1534. new_prefix = last ? String::utf8(" ") : String::utf8(" ┃ ");
  1535. data.children[i]->_print_tree_pretty(prefix + new_prefix, i == data.children.size() - 1);
  1536. }
  1537. }
  1538. void Node::print_tree_pretty() {
  1539. _print_tree_pretty("", true);
  1540. }
  1541. void Node::print_tree() {
  1542. _print_tree(this);
  1543. }
  1544. void Node::_print_tree(const Node *p_node) {
  1545. print_line(String(p_node->get_path_to(this)));
  1546. for (int i = 0; i < data.children.size(); i++) {
  1547. data.children[i]->_print_tree(p_node);
  1548. }
  1549. }
  1550. void Node::_propagate_reverse_notification(int p_notification) {
  1551. data.blocked++;
  1552. for (int i = data.children.size() - 1; i >= 0; i--) {
  1553. data.children[i]->_propagate_reverse_notification(p_notification);
  1554. }
  1555. notification(p_notification, true);
  1556. data.blocked--;
  1557. }
  1558. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  1559. ERR_FAIL_COND(!is_inside_tree());
  1560. data.blocked++;
  1561. if (!p_reverse) {
  1562. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1563. }
  1564. for (int i = 0; i < data.children.size(); i++) {
  1565. data.children[i]->_propagate_deferred_notification(p_notification, p_reverse);
  1566. }
  1567. if (p_reverse) {
  1568. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1569. }
  1570. data.blocked--;
  1571. }
  1572. void Node::propagate_notification(int p_notification) {
  1573. data.blocked++;
  1574. notification(p_notification);
  1575. for (int i = 0; i < data.children.size(); i++) {
  1576. data.children[i]->propagate_notification(p_notification);
  1577. }
  1578. data.blocked--;
  1579. }
  1580. void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
  1581. data.blocked++;
  1582. if (p_parent_first && has_method(p_method)) {
  1583. callv(p_method, p_args);
  1584. }
  1585. for (int i = 0; i < data.children.size(); i++) {
  1586. data.children[i]->propagate_call(p_method, p_args, p_parent_first);
  1587. }
  1588. if (!p_parent_first && has_method(p_method)) {
  1589. callv(p_method, p_args);
  1590. }
  1591. data.blocked--;
  1592. }
  1593. void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
  1594. if (get_owner() == p_owner) {
  1595. set_owner(p_by_owner);
  1596. }
  1597. data.blocked++;
  1598. for (int i = 0; i < data.children.size(); i++) {
  1599. data.children[i]->_propagate_replace_owner(p_owner, p_by_owner);
  1600. }
  1601. data.blocked--;
  1602. }
  1603. int Node::get_index() const {
  1604. return data.pos;
  1605. }
  1606. Ref<SceneTreeTween> Node::create_tween() {
  1607. ERR_FAIL_COND_V_MSG(!data.tree, nullptr, "Can't create SceneTreeTween when not inside scene tree.");
  1608. Ref<SceneTreeTween> tween = get_tree()->create_tween();
  1609. tween->bind_node(this);
  1610. return tween;
  1611. }
  1612. void Node::remove_and_skip() {
  1613. ERR_FAIL_COND(!data.parent);
  1614. Node *new_owner = get_owner();
  1615. List<Node *> children;
  1616. while (true) {
  1617. bool clear = true;
  1618. for (int i = 0; i < data.children.size(); i++) {
  1619. Node *c_node = data.children[i];
  1620. if (!c_node->get_owner()) {
  1621. continue;
  1622. }
  1623. remove_child(c_node);
  1624. c_node->_propagate_replace_owner(this, nullptr);
  1625. children.push_back(c_node);
  1626. clear = false;
  1627. break;
  1628. }
  1629. if (clear) {
  1630. break;
  1631. }
  1632. }
  1633. while (!children.empty()) {
  1634. Node *c_node = children.front()->get();
  1635. data.parent->add_child(c_node);
  1636. c_node->_propagate_replace_owner(nullptr, new_owner);
  1637. children.pop_front();
  1638. }
  1639. data.parent->remove_child(this);
  1640. }
  1641. void Node::set_filename(const String &p_filename) {
  1642. data.filename = p_filename;
  1643. }
  1644. String Node::get_filename() const {
  1645. return data.filename;
  1646. }
  1647. void Node::set_editor_description(const String &p_editor_description) {
  1648. set_meta("_editor_description_", p_editor_description);
  1649. }
  1650. String Node::get_editor_description() const {
  1651. if (has_meta("_editor_description_")) {
  1652. return get_meta("_editor_description_");
  1653. } else {
  1654. return "";
  1655. }
  1656. }
  1657. void Node::set_editable_instance(Node *p_node, bool p_editable) {
  1658. ERR_FAIL_NULL(p_node);
  1659. ERR_FAIL_COND(!is_a_parent_of(p_node));
  1660. if (!p_editable) {
  1661. p_node->data.editable_instance = false;
  1662. // Avoid this flag being needlessly saved;
  1663. // also give more visual feedback if editable children is re-enabled
  1664. set_display_folded(false);
  1665. } else {
  1666. p_node->data.editable_instance = true;
  1667. }
  1668. }
  1669. bool Node::is_editable_instance(const Node *p_node) const {
  1670. if (!p_node) {
  1671. return false; //easier, null is never editable :)
  1672. }
  1673. ERR_FAIL_COND_V(!is_a_parent_of(p_node), false);
  1674. return p_node->data.editable_instance;
  1675. }
  1676. Node *Node::get_deepest_editable_node(Node *p_start_node) const {
  1677. ERR_FAIL_NULL_V(p_start_node, nullptr);
  1678. ERR_FAIL_COND_V(!is_a_parent_of(p_start_node), p_start_node);
  1679. Node const *iterated_item = p_start_node;
  1680. Node *node = p_start_node;
  1681. while (iterated_item->get_owner() && iterated_item->get_owner() != this) {
  1682. if (!is_editable_instance(iterated_item->get_owner())) {
  1683. node = iterated_item->get_owner();
  1684. }
  1685. iterated_item = iterated_item->get_owner();
  1686. }
  1687. return node;
  1688. }
  1689. #ifdef TOOLS_ENABLED
  1690. void Node::set_property_pinned(const StringName &p_property, bool p_pinned) {
  1691. bool current_pinned = false;
  1692. bool has_pinned = has_meta("_edit_pinned_properties_");
  1693. Array pinned;
  1694. String psa = get_property_store_alias(p_property);
  1695. if (has_pinned) {
  1696. pinned = get_meta("_edit_pinned_properties_");
  1697. current_pinned = pinned.has(psa);
  1698. }
  1699. if (current_pinned != p_pinned) {
  1700. if (p_pinned) {
  1701. pinned.append(psa);
  1702. if (!has_pinned) {
  1703. set_meta("_edit_pinned_properties_", pinned);
  1704. }
  1705. } else {
  1706. pinned.erase(psa);
  1707. if (pinned.empty()) {
  1708. remove_meta("_edit_pinned_properties_");
  1709. }
  1710. }
  1711. }
  1712. }
  1713. bool Node::is_property_pinned(const StringName &p_property) const {
  1714. if (!has_meta("_edit_pinned_properties_")) {
  1715. return false;
  1716. }
  1717. Array pinned = get_meta("_edit_pinned_properties_");
  1718. String psa = get_property_store_alias(p_property);
  1719. return pinned.has(psa);
  1720. }
  1721. StringName Node::get_property_store_alias(const StringName &p_property) const {
  1722. return p_property;
  1723. }
  1724. #endif
  1725. void Node::get_storable_properties(Set<StringName> &r_storable_properties) const {
  1726. List<PropertyInfo> pi;
  1727. get_property_list(&pi);
  1728. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  1729. if ((E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1730. r_storable_properties.insert(E->get().name);
  1731. }
  1732. }
  1733. }
  1734. String Node::to_string() {
  1735. // This code doesn't print the script's name, it calls to_string() if you override it in a Node's script,
  1736. // which you only do if you specifically want to customize how the node should be represented by print().
  1737. if (get_script_instance()) {
  1738. bool valid;
  1739. String ret = get_script_instance()->to_string(&valid);
  1740. if (valid) {
  1741. return ret;
  1742. }
  1743. }
  1744. return (get_name() ? String(get_name()) + ":" : "") + Object::to_string();
  1745. }
  1746. void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
  1747. data.instance_state = p_state;
  1748. }
  1749. Ref<SceneState> Node::get_scene_instance_state() const {
  1750. return data.instance_state;
  1751. }
  1752. void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
  1753. data.inherited_state = p_state;
  1754. }
  1755. Ref<SceneState> Node::get_scene_inherited_state() const {
  1756. return data.inherited_state;
  1757. }
  1758. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  1759. data.use_placeholder = p_enable;
  1760. }
  1761. bool Node::get_scene_instance_load_placeholder() const {
  1762. return data.use_placeholder;
  1763. }
  1764. int Node::get_position_in_parent() const {
  1765. return data.pos;
  1766. }
  1767. Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const {
  1768. Node *node = nullptr;
  1769. bool instanced = false;
  1770. // No need to load a packed scene more than once if features
  1771. // several times in the branch being duplicated.
  1772. struct LoadedPackedScene {
  1773. Ref<PackedScene> scene;
  1774. String filename;
  1775. };
  1776. LocalVector<LoadedPackedScene> loaded_scenes;
  1777. if (Object::cast_to<InstancePlaceholder>(this)) {
  1778. const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
  1779. InstancePlaceholder *nip = memnew(InstancePlaceholder);
  1780. nip->set_instance_path(ip->get_instance_path());
  1781. node = nip;
  1782. } else if ((p_flags & DUPLICATE_USE_INSTANCING) && get_filename() != String()) {
  1783. // already loaded?
  1784. int found = -1;
  1785. for (unsigned int n = 0; n < loaded_scenes.size(); n++) {
  1786. if (loaded_scenes[n].filename == get_filename()) {
  1787. found = n;
  1788. break;
  1789. }
  1790. }
  1791. Ref<PackedScene> res = Ref<PackedScene>();
  1792. if (found != -1) {
  1793. res = loaded_scenes[found].scene;
  1794. } else {
  1795. LoadedPackedScene ps;
  1796. ps.filename = get_filename();
  1797. ps.scene = ResourceLoader::load(get_filename());
  1798. res = ps.scene;
  1799. loaded_scenes.push_back(ps);
  1800. }
  1801. ERR_FAIL_COND_V(res.is_null(), nullptr);
  1802. PackedScene::GenEditState ges = PackedScene::GEN_EDIT_STATE_DISABLED;
  1803. #ifdef TOOLS_ENABLED
  1804. if (p_flags & DUPLICATE_FROM_EDITOR) {
  1805. ges = PackedScene::GEN_EDIT_STATE_INSTANCE;
  1806. }
  1807. #endif
  1808. node = res->instance(ges);
  1809. ERR_FAIL_COND_V(!node, nullptr);
  1810. node->set_scene_instance_load_placeholder(get_scene_instance_load_placeholder());
  1811. instanced = true;
  1812. } else {
  1813. Object *obj = ClassDB::instance(get_class());
  1814. ERR_FAIL_COND_V(!obj, nullptr);
  1815. node = Object::cast_to<Node>(obj);
  1816. if (!node) {
  1817. memdelete(obj);
  1818. }
  1819. ERR_FAIL_COND_V(!node, nullptr);
  1820. }
  1821. if (get_filename() != "") { //an instance
  1822. node->set_filename(get_filename());
  1823. node->data.editable_instance = data.editable_instance;
  1824. }
  1825. StringName script_property_name = CoreStringNames::get_singleton()->_script;
  1826. List<const Node *> hidden_roots;
  1827. List<const Node *> node_tree;
  1828. node_tree.push_front(this);
  1829. if (instanced) {
  1830. // Since nodes in the instanced hierarchy won't be duplicated explicitly, we need to make an inventory
  1831. // of all the nodes in the tree of the instanced scene in order to transfer the values of the properties
  1832. Vector<const Node *> instance_roots;
  1833. instance_roots.push_back(this);
  1834. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1835. for (int i = 0; i < N->get()->get_child_count(); ++i) {
  1836. Node *descendant = N->get()->get_child(i);
  1837. // Skip nodes not really belonging to the instanced hierarchy; they'll be processed normally later
  1838. // but remember non-instanced nodes that are hidden below instanced ones
  1839. if (instance_roots.find(descendant->get_owner()) == -1) {
  1840. if (descendant->get_parent() && descendant->get_parent() != this && descendant->data.owner != descendant->get_parent()) {
  1841. hidden_roots.push_back(descendant);
  1842. }
  1843. continue;
  1844. }
  1845. node_tree.push_back(descendant);
  1846. if (descendant->get_filename() != "" && instance_roots.find(descendant->get_owner()) != -1) {
  1847. instance_roots.push_back(descendant);
  1848. }
  1849. }
  1850. }
  1851. }
  1852. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1853. Node *current_node = node->get_node(get_path_to(N->get()));
  1854. ERR_CONTINUE(!current_node);
  1855. if (p_flags & DUPLICATE_SCRIPTS) {
  1856. bool is_valid = false;
  1857. Variant script = N->get()->get(script_property_name, &is_valid);
  1858. if (is_valid) {
  1859. current_node->set(script_property_name, script);
  1860. }
  1861. }
  1862. List<PropertyInfo> plist;
  1863. N->get()->get_property_list(&plist);
  1864. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1865. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1866. continue;
  1867. }
  1868. String name = E->get().name;
  1869. if (name == script_property_name) {
  1870. continue;
  1871. }
  1872. Variant value = N->get()->get(name).duplicate(true);
  1873. if (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE) {
  1874. Resource *res = Object::cast_to<Resource>(value);
  1875. if (res) { // Duplicate only if it's a resource
  1876. current_node->set(name, res->duplicate());
  1877. }
  1878. } else {
  1879. current_node->set(name, value);
  1880. }
  1881. }
  1882. }
  1883. if (get_name() != String()) {
  1884. node->set_name(get_name());
  1885. }
  1886. #ifdef TOOLS_ENABLED
  1887. if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap) {
  1888. r_duplimap->insert(this, node);
  1889. }
  1890. #endif
  1891. if (p_flags & DUPLICATE_GROUPS) {
  1892. List<GroupInfo> gi;
  1893. get_groups(&gi);
  1894. for (List<GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  1895. #ifdef TOOLS_ENABLED
  1896. if ((p_flags & DUPLICATE_FROM_EDITOR) && !E->get().persistent) {
  1897. continue;
  1898. }
  1899. #endif
  1900. node->add_to_group(E->get().name, E->get().persistent);
  1901. }
  1902. }
  1903. for (int i = 0; i < get_child_count(); i++) {
  1904. if (get_child(i)->data.parent_owned) {
  1905. continue;
  1906. }
  1907. if (instanced && get_child(i)->data.owner == this) {
  1908. continue; //part of instance
  1909. }
  1910. Node *dup = get_child(i)->_duplicate(p_flags, r_duplimap);
  1911. if (!dup) {
  1912. memdelete(node);
  1913. return nullptr;
  1914. }
  1915. node->add_child(dup);
  1916. if (i < node->get_child_count() - 1) {
  1917. node->move_child(dup, i);
  1918. }
  1919. }
  1920. for (List<const Node *>::Element *E = hidden_roots.front(); E; E = E->next()) {
  1921. Node *parent = node->get_node(get_path_to(E->get()->data.parent));
  1922. if (!parent) {
  1923. memdelete(node);
  1924. return nullptr;
  1925. }
  1926. Node *dup = E->get()->_duplicate(p_flags, r_duplimap);
  1927. if (!dup) {
  1928. memdelete(node);
  1929. return nullptr;
  1930. }
  1931. parent->add_child(dup);
  1932. int pos = E->get()->get_position_in_parent();
  1933. if (pos < parent->get_child_count() - 1) {
  1934. parent->move_child(dup, pos);
  1935. }
  1936. }
  1937. return node;
  1938. }
  1939. Node *Node::duplicate(int p_flags) const {
  1940. Node *dupe = _duplicate(p_flags);
  1941. if (dupe && (p_flags & DUPLICATE_SIGNALS)) {
  1942. _duplicate_signals(this, dupe);
  1943. }
  1944. return dupe;
  1945. }
  1946. #ifdef TOOLS_ENABLED
  1947. Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const {
  1948. return duplicate_from_editor(r_duplimap, Map<RES, RES>());
  1949. }
  1950. Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap, const Map<RES, RES> &p_resource_remap) const {
  1951. Node *dupe = _duplicate(DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANCING | DUPLICATE_FROM_EDITOR, &r_duplimap);
  1952. // This is used by SceneTreeDock's paste functionality. When pasting to foreign scene, resources are duplicated.
  1953. if (!p_resource_remap.empty()) {
  1954. remap_node_resources(dupe, p_resource_remap);
  1955. }
  1956. // Duplication of signals must happen after all the node descendants have been copied,
  1957. // because re-targeting of connections from some descendant to another is not possible
  1958. // if the emitter node comes later in tree order than the receiver
  1959. _duplicate_signals(this, dupe);
  1960. return dupe;
  1961. }
  1962. void Node::remap_node_resources(Node *p_node, const Map<RES, RES> &p_resource_remap) const {
  1963. List<PropertyInfo> props;
  1964. p_node->get_property_list(&props);
  1965. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1966. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1967. continue;
  1968. }
  1969. Variant v = p_node->get(E->get().name);
  1970. if (v.is_ref()) {
  1971. RES res = v;
  1972. if (res.is_valid()) {
  1973. if (p_resource_remap.has(res)) {
  1974. p_node->set(E->get().name, p_resource_remap[res]);
  1975. remap_nested_resources(res, p_resource_remap);
  1976. }
  1977. }
  1978. }
  1979. }
  1980. for (int i = 0; i < p_node->get_child_count(); i++) {
  1981. remap_node_resources(p_node->get_child(i), p_resource_remap);
  1982. }
  1983. }
  1984. void Node::remap_nested_resources(RES p_resource, const Map<RES, RES> &p_resource_remap) const {
  1985. List<PropertyInfo> props;
  1986. p_resource->get_property_list(&props);
  1987. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1988. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1989. continue;
  1990. }
  1991. Variant v = p_resource->get(E->get().name);
  1992. if (v.is_ref()) {
  1993. RES res = v;
  1994. if (res.is_valid()) {
  1995. if (p_resource_remap.has(res)) {
  1996. p_resource->set(E->get().name, p_resource_remap[res]);
  1997. remap_nested_resources(res, p_resource_remap);
  1998. }
  1999. }
  2000. }
  2001. }
  2002. }
  2003. #endif
  2004. void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const {
  2005. if (get_owner() != get_parent()->get_owner()) {
  2006. return;
  2007. }
  2008. Node *node = nullptr;
  2009. if (get_filename() != "") {
  2010. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  2011. ERR_FAIL_COND_MSG(res.is_null(), "Cannot load scene: " + get_filename());
  2012. node = res->instance();
  2013. ERR_FAIL_COND(!node);
  2014. } else {
  2015. Object *obj = ClassDB::instance(get_class());
  2016. ERR_FAIL_COND_MSG(!obj, "Node: Could not duplicate: " + String(get_class()) + ".");
  2017. node = Object::cast_to<Node>(obj);
  2018. if (!node) {
  2019. memdelete(obj);
  2020. ERR_FAIL_MSG("Node: Could not duplicate: " + String(get_class()) + ".");
  2021. }
  2022. }
  2023. List<PropertyInfo> plist;
  2024. get_property_list(&plist);
  2025. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  2026. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2027. continue;
  2028. }
  2029. String name = E->get().name;
  2030. Variant value = get(name).duplicate(true);
  2031. node->set(name, value);
  2032. }
  2033. List<GroupInfo> groups;
  2034. get_groups(&groups);
  2035. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  2036. node->add_to_group(E->get().name, E->get().persistent);
  2037. }
  2038. node->set_name(get_name());
  2039. p_new_parent->add_child(node);
  2040. Node *owner = get_owner();
  2041. if (p_reown_map.has(owner)) {
  2042. owner = p_reown_map[owner];
  2043. }
  2044. if (owner) {
  2045. NodePath p = get_path_to(owner);
  2046. if (owner != this) {
  2047. Node *new_owner = node->get_node(p);
  2048. if (new_owner) {
  2049. node->set_owner(new_owner);
  2050. }
  2051. }
  2052. }
  2053. for (int i = 0; i < get_child_count(); i++) {
  2054. get_child(i)->_duplicate_and_reown(node, p_reown_map);
  2055. }
  2056. }
  2057. // Duplication of signals must happen after all the node descendants have been copied,
  2058. // because re-targeting of connections from some descendant to another is not possible
  2059. // if the emitter node comes later in tree order than the receiver
  2060. void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
  2061. if ((this != p_original) && !(p_original->is_a_parent_of(this))) {
  2062. return;
  2063. }
  2064. List<const Node *> process_list;
  2065. process_list.push_back(this);
  2066. while (!process_list.empty()) {
  2067. const Node *n = process_list.front()->get();
  2068. process_list.pop_front();
  2069. List<Connection> conns;
  2070. n->get_all_signal_connections(&conns);
  2071. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  2072. if (E->get().flags & CONNECT_PERSIST) {
  2073. //user connected
  2074. NodePath p = p_original->get_path_to(n);
  2075. Node *copy = p_copy->get_node(p);
  2076. Node *target = Object::cast_to<Node>(E->get().target);
  2077. if (!target) {
  2078. continue;
  2079. }
  2080. NodePath ptarget = p_original->get_path_to(target);
  2081. Node *copytarget = target;
  2082. // Attempt to find a path to the duplicate target, if it seems it's not part
  2083. // of the duplicated and not yet parented hierarchy then at least try to connect
  2084. // to the same target as the original
  2085. if (p_copy->has_node(ptarget)) {
  2086. copytarget = p_copy->get_node(ptarget);
  2087. }
  2088. if (copy && copytarget && !copy->is_connected(E->get().signal, copytarget, E->get().method)) {
  2089. copy->connect(E->get().signal, copytarget, E->get().method, E->get().binds, E->get().flags);
  2090. }
  2091. }
  2092. }
  2093. for (int i = 0; i < n->get_child_count(); i++) {
  2094. process_list.push_back(n->get_child(i));
  2095. }
  2096. }
  2097. }
  2098. Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const {
  2099. ERR_FAIL_COND_V(get_filename() != "", nullptr);
  2100. Object *obj = ClassDB::instance(get_class());
  2101. ERR_FAIL_COND_V_MSG(!obj, nullptr, "Node: Could not duplicate: " + String(get_class()) + ".");
  2102. Node *node = Object::cast_to<Node>(obj);
  2103. if (!node) {
  2104. memdelete(obj);
  2105. ERR_FAIL_V_MSG(nullptr, "Node: Could not duplicate: " + String(get_class()) + ".");
  2106. }
  2107. node->set_name(get_name());
  2108. List<PropertyInfo> plist;
  2109. get_property_list(&plist);
  2110. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  2111. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2112. continue;
  2113. }
  2114. String name = E->get().name;
  2115. node->set(name, get(name));
  2116. }
  2117. List<GroupInfo> groups;
  2118. get_groups(&groups);
  2119. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  2120. node->add_to_group(E->get().name, E->get().persistent);
  2121. }
  2122. for (int i = 0; i < get_child_count(); i++) {
  2123. get_child(i)->_duplicate_and_reown(node, p_reown_map);
  2124. }
  2125. // Duplication of signals must happen after all the node descendants have been copied,
  2126. // because re-targeting of connections from some descendant to another is not possible
  2127. // if the emitter node comes later in tree order than the receiver
  2128. _duplicate_signals(this, node);
  2129. return node;
  2130. }
  2131. static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
  2132. if (p_node->get_owner() == p_by) {
  2133. p_owned->push_back(p_node);
  2134. }
  2135. for (int i = 0; i < p_node->get_child_count(); i++) {
  2136. find_owned_by(p_by, p_node->get_child(i), p_owned);
  2137. }
  2138. }
  2139. struct _NodeReplaceByPair {
  2140. String name;
  2141. Variant value;
  2142. };
  2143. void Node::replace_by(Node *p_node, bool p_keep_data) {
  2144. ERR_FAIL_NULL(p_node);
  2145. ERR_FAIL_COND(p_node->data.parent);
  2146. List<Node *> owned = data.owned;
  2147. List<Node *> owned_by_owner;
  2148. Node *owner = (data.owner == this) ? p_node : data.owner;
  2149. List<_NodeReplaceByPair> replace_data;
  2150. if (p_keep_data) {
  2151. List<PropertyInfo> plist;
  2152. get_property_list(&plist);
  2153. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  2154. _NodeReplaceByPair rd;
  2155. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  2156. continue;
  2157. }
  2158. rd.name = E->get().name;
  2159. rd.value = get(rd.name);
  2160. }
  2161. List<GroupInfo> groups;
  2162. get_groups(&groups);
  2163. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  2164. p_node->add_to_group(E->get().name, E->get().persistent);
  2165. }
  2166. }
  2167. _replace_connections_target(p_node);
  2168. if (data.owner) {
  2169. for (int i = 0; i < get_child_count(); i++) {
  2170. find_owned_by(data.owner, get_child(i), &owned_by_owner);
  2171. }
  2172. }
  2173. Node *parent = data.parent;
  2174. int pos_in_parent = data.pos;
  2175. if (data.parent) {
  2176. parent->remove_child(this);
  2177. parent->add_child(p_node);
  2178. parent->move_child(p_node, pos_in_parent);
  2179. }
  2180. while (get_child_count()) {
  2181. Node *child = get_child(0);
  2182. remove_child(child);
  2183. if (!child->is_owned_by_parent()) {
  2184. // add the custom children to the p_node
  2185. p_node->add_child(child);
  2186. }
  2187. }
  2188. p_node->set_owner(owner);
  2189. for (int i = 0; i < owned.size(); i++) {
  2190. owned[i]->set_owner(p_node);
  2191. }
  2192. for (int i = 0; i < owned_by_owner.size(); i++) {
  2193. owned_by_owner[i]->set_owner(owner);
  2194. }
  2195. p_node->set_filename(get_filename());
  2196. for (List<_NodeReplaceByPair>::Element *E = replace_data.front(); E; E = E->next()) {
  2197. p_node->set(E->get().name, E->get().value);
  2198. }
  2199. }
  2200. void Node::_replace_connections_target(Node *p_new_target) {
  2201. List<Connection> cl;
  2202. get_signals_connected_to_this(&cl);
  2203. for (List<Connection>::Element *E = cl.front(); E; E = E->next()) {
  2204. Connection &c = E->get();
  2205. if (c.flags & CONNECT_PERSIST) {
  2206. c.source->disconnect(c.signal, this, c.method);
  2207. bool valid = p_new_target->has_method(c.method) || Ref<Script>(p_new_target->get_script()).is_null() || Ref<Script>(p_new_target->get_script())->has_method(c.method);
  2208. ERR_CONTINUE_MSG(!valid, "Attempt to connect signal '" + c.source->get_class() + "." + c.signal + "' to nonexistent method '" + c.target->get_class() + "." + c.method + "'.");
  2209. c.source->connect(c.signal, p_new_target, c.method, c.binds, c.flags);
  2210. }
  2211. }
  2212. }
  2213. Vector<Variant> Node::make_binds(VARIANT_ARG_DECLARE) {
  2214. Vector<Variant> ret;
  2215. if (p_arg1.get_type() == Variant::NIL) {
  2216. return ret;
  2217. } else {
  2218. ret.push_back(p_arg1);
  2219. }
  2220. if (p_arg2.get_type() == Variant::NIL) {
  2221. return ret;
  2222. } else {
  2223. ret.push_back(p_arg2);
  2224. }
  2225. if (p_arg3.get_type() == Variant::NIL) {
  2226. return ret;
  2227. } else {
  2228. ret.push_back(p_arg3);
  2229. }
  2230. if (p_arg4.get_type() == Variant::NIL) {
  2231. return ret;
  2232. } else {
  2233. ret.push_back(p_arg4);
  2234. }
  2235. if (p_arg5.get_type() == Variant::NIL) {
  2236. return ret;
  2237. } else {
  2238. ret.push_back(p_arg5);
  2239. }
  2240. return ret;
  2241. }
  2242. bool Node::has_node_and_resource(const NodePath &p_path) const {
  2243. if (!has_node(p_path)) {
  2244. return false;
  2245. }
  2246. RES res;
  2247. Vector<StringName> leftover_path;
  2248. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  2249. return node;
  2250. }
  2251. Array Node::_get_node_and_resource(const NodePath &p_path) {
  2252. RES res;
  2253. Vector<StringName> leftover_path;
  2254. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  2255. Array result;
  2256. if (node) {
  2257. result.push_back(node);
  2258. } else {
  2259. result.push_back(Variant());
  2260. }
  2261. if (res.is_valid()) {
  2262. result.push_back(res);
  2263. } else {
  2264. result.push_back(Variant());
  2265. }
  2266. result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
  2267. return result;
  2268. }
  2269. Node *Node::get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
  2270. Node *node = get_node(p_path);
  2271. r_res = RES();
  2272. r_leftover_subpath = Vector<StringName>();
  2273. if (!node) {
  2274. return nullptr;
  2275. }
  2276. if (p_path.get_subname_count()) {
  2277. int j = 0;
  2278. // If not p_last_is_property, we shouldn't consider the last one as part of the resource
  2279. for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
  2280. Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j)) : r_res->get(p_path.get_subname(j));
  2281. if (new_res_v.get_type() == Variant::NIL) { // Found nothing on that path
  2282. return nullptr;
  2283. }
  2284. RES new_res = new_res_v;
  2285. if (new_res.is_null()) { // No longer a resource, assume property
  2286. break;
  2287. }
  2288. r_res = new_res;
  2289. }
  2290. for (; j < p_path.get_subname_count(); j++) {
  2291. // Put the rest of the subpath in the leftover path
  2292. r_leftover_subpath.push_back(p_path.get_subname(j));
  2293. }
  2294. }
  2295. return node;
  2296. }
  2297. void Node::_set_tree(SceneTree *p_tree) {
  2298. SceneTree *tree_changed_a = nullptr;
  2299. SceneTree *tree_changed_b = nullptr;
  2300. //ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  2301. if (data.tree) {
  2302. _propagate_exit_tree();
  2303. tree_changed_a = data.tree;
  2304. }
  2305. data.tree = p_tree;
  2306. if (data.tree) {
  2307. _propagate_enter_tree();
  2308. if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
  2309. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  2310. }
  2311. tree_changed_b = data.tree;
  2312. }
  2313. if (tree_changed_a) {
  2314. tree_changed_a->tree_changed();
  2315. }
  2316. if (tree_changed_b) {
  2317. tree_changed_b->tree_changed();
  2318. }
  2319. }
  2320. #ifdef DEBUG_ENABLED
  2321. static void _Node_debug_sn(Object *p_obj) {
  2322. Node *n = Object::cast_to<Node>(p_obj);
  2323. if (!n) {
  2324. return;
  2325. }
  2326. if (n->is_inside_tree()) {
  2327. return;
  2328. }
  2329. Node *p = n;
  2330. while (p->get_parent()) {
  2331. p = p->get_parent();
  2332. }
  2333. String path;
  2334. if (p == n) {
  2335. path = n->get_name();
  2336. } else {
  2337. path = String(p->get_name()) + "/" + p->get_path_to(n);
  2338. }
  2339. String script_file_string;
  2340. if (!n->get_script().is_null()) {
  2341. Ref<Script> script = n->get_script();
  2342. if (script.is_valid()) {
  2343. script_file_string = ", Script: " + script->get_path();
  2344. }
  2345. }
  2346. print_line(itos(p_obj->get_instance_id()) + " - Stray Node: " + path + " (Type: " + n->get_class() + script_file_string + ")");
  2347. }
  2348. #endif // DEBUG_ENABLED
  2349. void Node::_print_stray_nodes() {
  2350. print_stray_nodes();
  2351. }
  2352. void Node::print_stray_nodes() {
  2353. #ifdef DEBUG_ENABLED
  2354. ObjectDB::debug_objects(_Node_debug_sn);
  2355. #endif
  2356. }
  2357. void Node::queue_delete() {
  2358. // There are users which instantiate multiple scene trees for their games.
  2359. // Use the node's own tree to handle its deletion when relevant.
  2360. if (is_inside_tree()) {
  2361. get_tree()->queue_delete(this);
  2362. } else {
  2363. SceneTree *tree = SceneTree::get_singleton();
  2364. ERR_FAIL_NULL_MSG(tree, "Can't queue free a node when no SceneTree is available.");
  2365. tree->queue_delete(this);
  2366. }
  2367. }
  2368. Array Node::_get_children() const {
  2369. Array arr;
  2370. int cc = get_child_count();
  2371. arr.resize(cc);
  2372. for (int i = 0; i < cc; i++) {
  2373. arr[i] = get_child(i);
  2374. }
  2375. return arr;
  2376. }
  2377. void Node::set_import_path(const NodePath &p_import_path) {
  2378. #ifdef TOOLS_ENABLED
  2379. data.import_path = p_import_path;
  2380. #endif
  2381. }
  2382. NodePath Node::get_import_path() const {
  2383. #ifdef TOOLS_ENABLED
  2384. return data.import_path;
  2385. #else
  2386. return NodePath();
  2387. #endif
  2388. }
  2389. static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
  2390. #ifdef TOOLS_ENABLED
  2391. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2392. #else
  2393. const String quote_style = "\"";
  2394. #endif
  2395. if (p_node != p_base && !p_node->get_owner()) {
  2396. return;
  2397. }
  2398. String n = p_base->get_path_to(p_node);
  2399. r_options->push_back(quote_style + n + quote_style);
  2400. for (int i = 0; i < p_node->get_child_count(); i++) {
  2401. _add_nodes_to_options(p_base, p_node->get_child(i), r_options);
  2402. }
  2403. }
  2404. void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  2405. String pf = p_function;
  2406. if ((pf == "has_node" || pf == "get_node") && p_idx == 0) {
  2407. _add_nodes_to_options(this, this, r_options);
  2408. }
  2409. Object::get_argument_options(p_function, p_idx, r_options);
  2410. }
  2411. void Node::clear_internal_tree_resource_paths() {
  2412. clear_internal_resource_paths();
  2413. for (int i = 0; i < data.children.size(); i++) {
  2414. data.children[i]->clear_internal_tree_resource_paths();
  2415. }
  2416. }
  2417. String Node::get_configuration_warning() const {
  2418. if (get_script_instance() && get_script_instance()->get_script().is_valid() &&
  2419. get_script_instance()->get_script()->is_tool() && get_script_instance()->has_method("_get_configuration_warning")) {
  2420. return get_script_instance()->call("_get_configuration_warning");
  2421. }
  2422. return String();
  2423. }
  2424. void Node::update_configuration_warning() {
  2425. #ifdef TOOLS_ENABLED
  2426. if (!is_inside_tree()) {
  2427. return;
  2428. }
  2429. if (get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_a_parent_of(this))) {
  2430. get_tree()->emit_signal(SceneStringNames::get_singleton()->node_configuration_warning_changed, this);
  2431. }
  2432. #endif
  2433. }
  2434. bool Node::is_owned_by_parent() const {
  2435. return data.parent_owned;
  2436. }
  2437. void Node::set_display_folded(bool p_folded) {
  2438. data.display_folded = p_folded;
  2439. }
  2440. bool Node::is_displayed_folded() const {
  2441. return data.display_folded;
  2442. }
  2443. bool Node::is_node_ready() const {
  2444. return !data.ready_first;
  2445. }
  2446. void Node::request_ready() {
  2447. data.ready_first = true;
  2448. }
  2449. void Node::_bind_methods() {
  2450. GLOBAL_DEF("node/name_num_separator", 0);
  2451. ProjectSettings::get_singleton()->set_custom_property_info("node/name_num_separator", PropertyInfo(Variant::INT, "node/name_num_separator", PROPERTY_HINT_ENUM, "None,Space,Underscore,Dash"));
  2452. GLOBAL_DEF("node/name_casing", NAME_CASING_PASCAL_CASE);
  2453. ProjectSettings::get_singleton()->set_custom_property_info("node/name_casing", PropertyInfo(Variant::INT, "node/name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case"));
  2454. ClassDB::bind_method(D_METHOD("add_child_below_node", "node", "child_node", "force_readable_name"), &Node::add_child_below_node, DEFVAL(false));
  2455. ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
  2456. ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
  2457. ClassDB::bind_method(D_METHOD("add_child", "node", "force_readable_name"), &Node::add_child, DEFVAL(false));
  2458. ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
  2459. ClassDB::bind_method(D_METHOD("get_child_count"), &Node::get_child_count);
  2460. ClassDB::bind_method(D_METHOD("get_children"), &Node::_get_children);
  2461. ClassDB::bind_method(D_METHOD("get_child", "idx"), &Node::get_child);
  2462. ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
  2463. ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
  2464. ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
  2465. ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
  2466. ClassDB::bind_method(D_METHOD("find_node", "mask", "recursive", "owned"), &Node::find_node, DEFVAL(true), DEFVAL(true));
  2467. ClassDB::bind_method(D_METHOD("find_parent", "mask"), &Node::find_parent);
  2468. ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
  2469. ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
  2470. ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
  2471. ClassDB::bind_method(D_METHOD("is_a_parent_of", "node"), &Node::is_a_parent_of);
  2472. ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
  2473. ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
  2474. ClassDB::bind_method(D_METHOD("get_path_to", "node"), &Node::get_path_to);
  2475. ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
  2476. ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
  2477. ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
  2478. ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_position"), &Node::move_child);
  2479. ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
  2480. ClassDB::bind_method(D_METHOD("raise"), &Node::raise);
  2481. ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
  2482. ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
  2483. ClassDB::bind_method(D_METHOD("remove_and_skip"), &Node::remove_and_skip);
  2484. ClassDB::bind_method(D_METHOD("get_index"), &Node::get_index);
  2485. ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
  2486. ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
  2487. ClassDB::bind_method(D_METHOD("set_filename", "filename"), &Node::set_filename);
  2488. ClassDB::bind_method(D_METHOD("get_filename"), &Node::get_filename);
  2489. ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
  2490. ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
  2491. ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
  2492. ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
  2493. ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
  2494. ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
  2495. ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
  2496. ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
  2497. ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
  2498. ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
  2499. ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
  2500. ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
  2501. ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
  2502. ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
  2503. ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
  2504. ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
  2505. ClassDB::bind_method(D_METHOD("set_pause_mode", "mode"), &Node::set_pause_mode);
  2506. ClassDB::bind_method(D_METHOD("get_pause_mode"), &Node::get_pause_mode);
  2507. ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
  2508. ClassDB::bind_method(D_METHOD("print_stray_nodes"), &Node::_print_stray_nodes);
  2509. ClassDB::bind_method(D_METHOD("get_position_in_parent"), &Node::get_position_in_parent);
  2510. ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
  2511. ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
  2512. ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
  2513. ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
  2514. ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
  2515. ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
  2516. ClassDB::bind_method(D_METHOD("set_physics_interpolation_mode", "mode"), &Node::set_physics_interpolation_mode);
  2517. ClassDB::bind_method(D_METHOD("get_physics_interpolation_mode"), &Node::get_physics_interpolation_mode);
  2518. ClassDB::bind_method(D_METHOD("is_physics_interpolated"), &Node::is_physics_interpolated);
  2519. ClassDB::bind_method(D_METHOD("is_physics_interpolated_and_enabled"), &Node::is_physics_interpolated_and_enabled);
  2520. ClassDB::bind_method(D_METHOD("reset_physics_interpolation"), &Node::reset_physics_interpolation);
  2521. ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
  2522. ClassDB::bind_method(D_METHOD("create_tween"), &Node::create_tween);
  2523. ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANCING | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
  2524. ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_data"), &Node::replace_by, DEFVAL(false));
  2525. ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
  2526. ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
  2527. ClassDB::bind_method(D_METHOD("set_editable_instance", "node", "is_editable"), &Node::set_editable_instance);
  2528. ClassDB::bind_method(D_METHOD("is_editable_instance", "node"), &Node::is_editable_instance);
  2529. ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
  2530. ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_delete);
  2531. ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
  2532. ClassDB::bind_method(D_METHOD("is_node_ready"), &Node::is_node_ready);
  2533. ClassDB::bind_method(D_METHOD("set_network_master", "id", "recursive"), &Node::set_network_master, DEFVAL(true));
  2534. ClassDB::bind_method(D_METHOD("get_network_master"), &Node::get_network_master);
  2535. ClassDB::bind_method(D_METHOD("is_network_master"), &Node::is_network_master);
  2536. ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
  2537. ClassDB::bind_method(D_METHOD("get_custom_multiplayer"), &Node::get_custom_multiplayer);
  2538. ClassDB::bind_method(D_METHOD("set_custom_multiplayer", "api"), &Node::set_custom_multiplayer);
  2539. ClassDB::bind_method(D_METHOD("rpc_config", "method", "mode"), &Node::rpc_config);
  2540. ClassDB::bind_method(D_METHOD("rset_config", "property", "mode"), &Node::rset_config);
  2541. ClassDB::bind_method(D_METHOD("_set_editor_description", "editor_description"), &Node::set_editor_description);
  2542. ClassDB::bind_method(D_METHOD("_get_editor_description"), &Node::get_editor_description);
  2543. ADD_GROUP("Editor Description", "editor_");
  2544. ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_editor_description", "_get_editor_description");
  2545. ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
  2546. ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);
  2547. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "_import_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_import_path", "_get_import_path");
  2548. #ifdef TOOLS_ENABLED
  2549. ClassDB::bind_method(D_METHOD("_set_property_pinned", "property", "pinned"), &Node::set_property_pinned);
  2550. #endif
  2551. ClassDB::bind_method(D_METHOD("set_unique_name_in_owner", "enable"), &Node::set_unique_name_in_owner);
  2552. ClassDB::bind_method(D_METHOD("is_unique_name_in_owner"), &Node::is_unique_name_in_owner);
  2553. {
  2554. MethodInfo mi;
  2555. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  2556. mi.name = "rpc";
  2557. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
  2558. mi.name = "rpc_unreliable";
  2559. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_unreliable", &Node::_rpc_unreliable_bind, mi);
  2560. mi.arguments.push_front(PropertyInfo(Variant::INT, "peer_id"));
  2561. mi.name = "rpc_id";
  2562. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
  2563. mi.name = "rpc_unreliable_id";
  2564. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_unreliable_id", &Node::_rpc_unreliable_id_bind, mi);
  2565. }
  2566. ClassDB::bind_method(D_METHOD("rset", "property", "value"), &Node::rset);
  2567. ClassDB::bind_method(D_METHOD("rset_id", "peer_id", "property", "value"), &Node::rset_id);
  2568. ClassDB::bind_method(D_METHOD("rset_unreliable", "property", "value"), &Node::rset_unreliable);
  2569. ClassDB::bind_method(D_METHOD("rset_unreliable_id", "peer_id", "property", "value"), &Node::rset_unreliable_id);
  2570. ClassDB::bind_method(D_METHOD("update_configuration_warning"), &Node::update_configuration_warning);
  2571. BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
  2572. BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
  2573. BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
  2574. BIND_CONSTANT(NOTIFICATION_READY);
  2575. BIND_CONSTANT(NOTIFICATION_PAUSED);
  2576. BIND_CONSTANT(NOTIFICATION_UNPAUSED);
  2577. BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
  2578. BIND_CONSTANT(NOTIFICATION_PROCESS);
  2579. BIND_CONSTANT(NOTIFICATION_PARENTED);
  2580. BIND_CONSTANT(NOTIFICATION_UNPARENTED);
  2581. BIND_CONSTANT(NOTIFICATION_INSTANCED);
  2582. BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
  2583. BIND_CONSTANT(NOTIFICATION_DRAG_END);
  2584. BIND_CONSTANT(NOTIFICATION_PATH_CHANGED);
  2585. BIND_CONSTANT(NOTIFICATION_CHILD_ORDER_CHANGED);
  2586. BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
  2587. BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  2588. BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
  2589. BIND_CONSTANT(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  2590. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
  2591. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
  2592. BIND_CONSTANT(NOTIFICATION_WM_FOCUS_IN);
  2593. BIND_CONSTANT(NOTIFICATION_WM_FOCUS_OUT);
  2594. BIND_CONSTANT(NOTIFICATION_WM_QUIT_REQUEST);
  2595. BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
  2596. BIND_CONSTANT(NOTIFICATION_WM_UNFOCUS_REQUEST);
  2597. BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
  2598. BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
  2599. BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
  2600. BIND_CONSTANT(NOTIFICATION_CRASH);
  2601. BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
  2602. BIND_CONSTANT(NOTIFICATION_APP_RESUMED);
  2603. BIND_CONSTANT(NOTIFICATION_APP_PAUSED);
  2604. BIND_ENUM_CONSTANT(PAUSE_MODE_INHERIT);
  2605. BIND_ENUM_CONSTANT(PAUSE_MODE_STOP);
  2606. BIND_ENUM_CONSTANT(PAUSE_MODE_PROCESS);
  2607. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_INHERIT);
  2608. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_OFF);
  2609. BIND_ENUM_CONSTANT(PHYSICS_INTERPOLATION_MODE_ON);
  2610. BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
  2611. BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
  2612. BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
  2613. BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANCING);
  2614. ADD_SIGNAL(MethodInfo("ready"));
  2615. ADD_SIGNAL(MethodInfo("renamed"));
  2616. ADD_SIGNAL(MethodInfo("tree_entered"));
  2617. ADD_SIGNAL(MethodInfo("tree_exiting"));
  2618. ADD_SIGNAL(MethodInfo("tree_exited"));
  2619. ADD_SIGNAL(MethodInfo("child_entered_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  2620. ADD_SIGNAL(MethodInfo("child_exiting_tree", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Node")));
  2621. ADD_SIGNAL(MethodInfo("child_order_changed"));
  2622. ADD_PROPERTY(PropertyInfo(Variant::INT, "pause_mode", PROPERTY_HINT_ENUM, "Inherit,Stop,Process"), "set_pause_mode", "get_pause_mode");
  2623. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_interpolation_mode", PROPERTY_HINT_ENUM, "Inherit,Off,On"), "set_physics_interpolation_mode", "get_physics_interpolation_mode");
  2624. #ifdef ENABLE_DEPRECATED
  2625. //no longer exists, but remains for compatibility (keep previous scenes folded
  2626. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor/display_folded", PROPERTY_HINT_NONE, "", 0), "set_display_folded", "is_displayed_folded");
  2627. #endif
  2628. ADD_PROPERTY(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "", 0), "set_name", "get_name");
  2629. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "unique_name_in_owner", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_unique_name_in_owner", "is_unique_name_in_owner");
  2630. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename", PROPERTY_HINT_NONE, "", 0), "set_filename", "get_filename");
  2631. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_owner", "get_owner");
  2632. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "", "get_multiplayer");
  2633. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_custom_multiplayer", "get_custom_multiplayer");
  2634. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
  2635. BIND_VMETHOD(MethodInfo("_process", PropertyInfo(Variant::REAL, "delta")));
  2636. BIND_VMETHOD(MethodInfo("_physics_process", PropertyInfo(Variant::REAL, "delta")));
  2637. BIND_VMETHOD(MethodInfo("_enter_tree"));
  2638. BIND_VMETHOD(MethodInfo("_exit_tree"));
  2639. BIND_VMETHOD(MethodInfo("_ready"));
  2640. BIND_VMETHOD(MethodInfo("_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2641. BIND_VMETHOD(MethodInfo("_unhandled_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2642. BIND_VMETHOD(MethodInfo("_unhandled_key_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEventKey")));
  2643. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_configuration_warning"));
  2644. }
  2645. String Node::_get_name_num_separator() {
  2646. switch (ProjectSettings::get_singleton()->get("node/name_num_separator").operator int()) {
  2647. case 0:
  2648. return "";
  2649. case 1:
  2650. return " ";
  2651. case 2:
  2652. return "_";
  2653. case 3:
  2654. return "-";
  2655. }
  2656. return " ";
  2657. }
  2658. Node::Node() {
  2659. _define_ancestry(AncestralClass::NODE);
  2660. data.pos = -1;
  2661. data.depth = -1;
  2662. data.blocked = 0;
  2663. data.parent = nullptr;
  2664. data.tree = nullptr;
  2665. data.physics_process = false;
  2666. data.idle_process = false;
  2667. data.process_priority = 0;
  2668. data.physics_process_internal = false;
  2669. data.idle_process_internal = false;
  2670. data.inside_tree = false;
  2671. data.ready_notified = false;
  2672. data.physics_interpolated = true;
  2673. data.physics_interpolation_reset_requested = false;
  2674. data.physics_interpolated_client_side = false;
  2675. data.use_identity_transform = false;
  2676. data.owner = nullptr;
  2677. data.OW = nullptr;
  2678. data.input = false;
  2679. data.unhandled_input = false;
  2680. data.unhandled_key_input = false;
  2681. data.pause_mode = PAUSE_MODE_INHERIT;
  2682. data.physics_interpolation_mode = PHYSICS_INTERPOLATION_MODE_INHERIT;
  2683. data.pause_owner = nullptr;
  2684. data.network_master = 1; //server by default
  2685. data.path_cache = nullptr;
  2686. data.parent_owned = false;
  2687. data.in_constructor = true;
  2688. data.viewport = nullptr;
  2689. data.use_placeholder = false;
  2690. data.display_folded = false;
  2691. data.ready_first = true;
  2692. data.editable_instance = false;
  2693. orphan_node_count++;
  2694. }
  2695. Node::~Node() {
  2696. data.grouped.clear();
  2697. data.owned.clear();
  2698. data.children.clear();
  2699. ERR_FAIL_COND(data.parent);
  2700. ERR_FAIL_COND(data.children.size());
  2701. orphan_node_count--;
  2702. }
  2703. ////////////////////////////////