node.cpp 82 KB

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