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