node.cpp 90 KB

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