node.cpp 105 KB

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