node.cpp 78 KB

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