node.cpp 93 KB

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