node.cpp 79 KB

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