node.cpp 81 KB

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