node.cpp 83 KB

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