node.cpp 75 KB

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