node.cpp 79 KB

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