node.cpp 85 KB

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