node.cpp 79 KB

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