node.cpp 90 KB

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