node.cpp 80 KB

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