node.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990
  1. /*************************************************************************/
  2. /* node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. ERR_FAIL_COND_V_MSG(!node, NULL, "Node not found: " + p_path + ".");
  1013. return node;
  1014. }
  1015. bool Node::has_node(const NodePath &p_path) const {
  1016. return get_node_or_null(p_path) != NULL;
  1017. }
  1018. Node *Node::find_node(const String &p_mask, bool p_recursive, bool p_owned) const {
  1019. Node *const *cptr = data.children.ptr();
  1020. int ccount = data.children.size();
  1021. for (int i = 0; i < ccount; i++) {
  1022. if (p_owned && !cptr[i]->data.owner)
  1023. continue;
  1024. if (cptr[i]->data.name.operator String().match(p_mask))
  1025. return cptr[i];
  1026. if (!p_recursive)
  1027. continue;
  1028. Node *ret = cptr[i]->find_node(p_mask, true, p_owned);
  1029. if (ret)
  1030. return ret;
  1031. }
  1032. return NULL;
  1033. }
  1034. Node *Node::get_parent() const {
  1035. return data.parent;
  1036. }
  1037. Node *Node::find_parent(const String &p_mask) const {
  1038. Node *p = data.parent;
  1039. while (p) {
  1040. if (p->data.name.operator String().match(p_mask))
  1041. return p;
  1042. p = p->data.parent;
  1043. }
  1044. return NULL;
  1045. }
  1046. bool Node::is_a_parent_of(const Node *p_node) const {
  1047. ERR_FAIL_NULL_V(p_node, false);
  1048. Node *p = p_node->data.parent;
  1049. while (p) {
  1050. if (p == this)
  1051. return true;
  1052. p = p->data.parent;
  1053. }
  1054. return false;
  1055. }
  1056. bool Node::is_greater_than(const Node *p_node) const {
  1057. ERR_FAIL_NULL_V(p_node, false);
  1058. ERR_FAIL_COND_V(!data.inside_tree, false);
  1059. ERR_FAIL_COND_V(!p_node->data.inside_tree, false);
  1060. ERR_FAIL_COND_V(data.depth < 0, false);
  1061. ERR_FAIL_COND_V(p_node->data.depth < 0, false);
  1062. #ifdef NO_ALLOCA
  1063. Vector<int> this_stack;
  1064. Vector<int> that_stack;
  1065. this_stack.resize(data.depth);
  1066. that_stack.resize(p_node->data.depth);
  1067. #else
  1068. int *this_stack = (int *)alloca(sizeof(int) * data.depth);
  1069. int *that_stack = (int *)alloca(sizeof(int) * p_node->data.depth);
  1070. #endif
  1071. const Node *n = this;
  1072. int idx = data.depth - 1;
  1073. while (n) {
  1074. ERR_FAIL_INDEX_V(idx, data.depth, false);
  1075. this_stack[idx--] = n->data.pos;
  1076. n = n->data.parent;
  1077. }
  1078. ERR_FAIL_COND_V(idx != -1, false);
  1079. n = p_node;
  1080. idx = p_node->data.depth - 1;
  1081. while (n) {
  1082. ERR_FAIL_INDEX_V(idx, p_node->data.depth, false);
  1083. that_stack[idx--] = n->data.pos;
  1084. n = n->data.parent;
  1085. }
  1086. ERR_FAIL_COND_V(idx != -1, false);
  1087. idx = 0;
  1088. bool res;
  1089. while (true) {
  1090. // using -2 since out-of-tree or nonroot nodes have -1
  1091. int this_idx = (idx >= data.depth) ? -2 : this_stack[idx];
  1092. int that_idx = (idx >= p_node->data.depth) ? -2 : that_stack[idx];
  1093. if (this_idx > that_idx) {
  1094. res = true;
  1095. break;
  1096. } else if (this_idx < that_idx) {
  1097. res = false;
  1098. break;
  1099. } else if (this_idx == -2) {
  1100. res = false; // equal
  1101. break;
  1102. }
  1103. idx++;
  1104. }
  1105. return res;
  1106. }
  1107. void Node::get_owned_by(Node *p_by, List<Node *> *p_owned) {
  1108. if (data.owner == p_by)
  1109. p_owned->push_back(this);
  1110. for (int i = 0; i < get_child_count(); i++)
  1111. get_child(i)->get_owned_by(p_by, p_owned);
  1112. }
  1113. void Node::_set_owner_nocheck(Node *p_owner) {
  1114. if (data.owner == p_owner)
  1115. return;
  1116. ERR_FAIL_COND(data.owner);
  1117. data.owner = p_owner;
  1118. data.owner->data.owned.push_back(this);
  1119. data.OW = data.owner->data.owned.back();
  1120. }
  1121. void Node::set_owner(Node *p_owner) {
  1122. if (data.owner) {
  1123. data.owner->data.owned.erase(data.OW);
  1124. data.OW = NULL;
  1125. data.owner = NULL;
  1126. }
  1127. ERR_FAIL_COND(p_owner == this);
  1128. if (!p_owner)
  1129. return;
  1130. Node *check = this->get_parent();
  1131. bool owner_valid = false;
  1132. while (check) {
  1133. if (check == p_owner) {
  1134. owner_valid = true;
  1135. break;
  1136. }
  1137. check = check->data.parent;
  1138. }
  1139. ERR_FAIL_COND(!owner_valid);
  1140. _set_owner_nocheck(p_owner);
  1141. }
  1142. Node *Node::get_owner() const {
  1143. return data.owner;
  1144. }
  1145. Node *Node::find_common_parent_with(const Node *p_node) const {
  1146. if (this == p_node)
  1147. return const_cast<Node *>(p_node);
  1148. Set<const Node *> visited;
  1149. const Node *n = this;
  1150. while (n) {
  1151. visited.insert(n);
  1152. n = n->data.parent;
  1153. }
  1154. const Node *common_parent = p_node;
  1155. while (common_parent) {
  1156. if (visited.has(common_parent))
  1157. break;
  1158. common_parent = common_parent->data.parent;
  1159. }
  1160. if (!common_parent)
  1161. return NULL;
  1162. return const_cast<Node *>(common_parent);
  1163. }
  1164. NodePath Node::get_path_to(const Node *p_node) const {
  1165. ERR_FAIL_NULL_V(p_node, NodePath());
  1166. if (this == p_node)
  1167. return NodePath(".");
  1168. Set<const Node *> visited;
  1169. const Node *n = this;
  1170. while (n) {
  1171. visited.insert(n);
  1172. n = n->data.parent;
  1173. }
  1174. const Node *common_parent = p_node;
  1175. while (common_parent) {
  1176. if (visited.has(common_parent))
  1177. break;
  1178. common_parent = common_parent->data.parent;
  1179. }
  1180. ERR_FAIL_COND_V(!common_parent, NodePath()); //nodes not in the same tree
  1181. visited.clear();
  1182. Vector<StringName> path;
  1183. n = p_node;
  1184. while (n != common_parent) {
  1185. path.push_back(n->get_name());
  1186. n = n->data.parent;
  1187. }
  1188. n = this;
  1189. StringName up = String("..");
  1190. while (n != common_parent) {
  1191. path.push_back(up);
  1192. n = n->data.parent;
  1193. }
  1194. path.invert();
  1195. return NodePath(path, false);
  1196. }
  1197. NodePath Node::get_path() const {
  1198. ERR_FAIL_COND_V_MSG(!is_inside_tree(), NodePath(), "Cannot get path of node as it is not in a scene tree.");
  1199. if (data.path_cache)
  1200. return *data.path_cache;
  1201. const Node *n = this;
  1202. Vector<StringName> path;
  1203. while (n) {
  1204. path.push_back(n->get_name());
  1205. n = n->data.parent;
  1206. }
  1207. path.invert();
  1208. data.path_cache = memnew(NodePath(path, true));
  1209. return *data.path_cache;
  1210. }
  1211. bool Node::is_in_group(const StringName &p_identifier) const {
  1212. return data.grouped.has(p_identifier);
  1213. }
  1214. void Node::add_to_group(const StringName &p_identifier, bool p_persistent) {
  1215. ERR_FAIL_COND(!p_identifier.operator String().length());
  1216. if (data.grouped.has(p_identifier))
  1217. return;
  1218. GroupData gd;
  1219. if (data.tree) {
  1220. gd.group = data.tree->add_to_group(p_identifier, this);
  1221. } else {
  1222. gd.group = NULL;
  1223. }
  1224. gd.persistent = p_persistent;
  1225. data.grouped[p_identifier] = gd;
  1226. }
  1227. void Node::remove_from_group(const StringName &p_identifier) {
  1228. ERR_FAIL_COND(!data.grouped.has(p_identifier));
  1229. Map<StringName, GroupData>::Element *E = data.grouped.find(p_identifier);
  1230. ERR_FAIL_COND(!E);
  1231. if (data.tree)
  1232. data.tree->remove_from_group(E->key(), this);
  1233. data.grouped.erase(E);
  1234. }
  1235. Array Node::_get_groups() const {
  1236. Array groups;
  1237. List<GroupInfo> gi;
  1238. get_groups(&gi);
  1239. for (List<GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  1240. groups.push_back(E->get().name);
  1241. }
  1242. return groups;
  1243. }
  1244. void Node::get_groups(List<GroupInfo> *p_groups) const {
  1245. for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  1246. GroupInfo gi;
  1247. gi.name = E->key();
  1248. gi.persistent = E->get().persistent;
  1249. p_groups->push_back(gi);
  1250. }
  1251. }
  1252. int Node::get_persistent_group_count() const {
  1253. int count = 0;
  1254. for (const Map<StringName, GroupData>::Element *E = data.grouped.front(); E; E = E->next()) {
  1255. if (E->get().persistent) {
  1256. count += 1;
  1257. }
  1258. }
  1259. return count;
  1260. }
  1261. void Node::_print_tree_pretty(const String &prefix, const bool last) {
  1262. String new_prefix = last ? String::utf8(" ┖╴") : String::utf8(" ┠╴");
  1263. print_line(prefix + new_prefix + String(get_name()));
  1264. for (int i = 0; i < data.children.size(); i++) {
  1265. new_prefix = last ? String::utf8(" ") : String::utf8(" ┃ ");
  1266. data.children[i]->_print_tree_pretty(prefix + new_prefix, i == data.children.size() - 1);
  1267. }
  1268. }
  1269. void Node::print_tree_pretty() {
  1270. _print_tree_pretty("", true);
  1271. }
  1272. void Node::print_tree() {
  1273. _print_tree(this);
  1274. }
  1275. void Node::_print_tree(const Node *p_node) {
  1276. print_line(String(p_node->get_path_to(this)));
  1277. for (int i = 0; i < data.children.size(); i++)
  1278. data.children[i]->_print_tree(p_node);
  1279. }
  1280. void Node::_propagate_reverse_notification(int p_notification) {
  1281. data.blocked++;
  1282. for (int i = data.children.size() - 1; i >= 0; i--) {
  1283. data.children[i]->_propagate_reverse_notification(p_notification);
  1284. }
  1285. notification(p_notification, true);
  1286. data.blocked--;
  1287. }
  1288. void Node::_propagate_deferred_notification(int p_notification, bool p_reverse) {
  1289. ERR_FAIL_COND(!is_inside_tree());
  1290. data.blocked++;
  1291. if (!p_reverse)
  1292. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1293. for (int i = 0; i < data.children.size(); i++) {
  1294. data.children[i]->_propagate_deferred_notification(p_notification, p_reverse);
  1295. }
  1296. if (p_reverse)
  1297. MessageQueue::get_singleton()->push_notification(this, p_notification);
  1298. data.blocked--;
  1299. }
  1300. void Node::propagate_notification(int p_notification) {
  1301. data.blocked++;
  1302. notification(p_notification);
  1303. for (int i = 0; i < data.children.size(); i++) {
  1304. data.children[i]->propagate_notification(p_notification);
  1305. }
  1306. data.blocked--;
  1307. }
  1308. void Node::propagate_call(const StringName &p_method, const Array &p_args, const bool p_parent_first) {
  1309. data.blocked++;
  1310. if (p_parent_first && has_method(p_method))
  1311. callv(p_method, p_args);
  1312. for (int i = 0; i < data.children.size(); i++) {
  1313. data.children[i]->propagate_call(p_method, p_args, p_parent_first);
  1314. }
  1315. if (!p_parent_first && has_method(p_method))
  1316. callv(p_method, p_args);
  1317. data.blocked--;
  1318. }
  1319. void Node::_propagate_replace_owner(Node *p_owner, Node *p_by_owner) {
  1320. if (get_owner() == p_owner)
  1321. set_owner(p_by_owner);
  1322. data.blocked++;
  1323. for (int i = 0; i < data.children.size(); i++)
  1324. data.children[i]->_propagate_replace_owner(p_owner, p_by_owner);
  1325. data.blocked--;
  1326. }
  1327. int Node::get_index() const {
  1328. return data.pos;
  1329. }
  1330. void Node::remove_and_skip() {
  1331. ERR_FAIL_COND(!data.parent);
  1332. Node *new_owner = get_owner();
  1333. List<Node *> children;
  1334. while (true) {
  1335. bool clear = true;
  1336. for (int i = 0; i < data.children.size(); i++) {
  1337. Node *c_node = data.children[i];
  1338. if (!c_node->get_owner())
  1339. continue;
  1340. remove_child(c_node);
  1341. c_node->_propagate_replace_owner(this, NULL);
  1342. children.push_back(c_node);
  1343. clear = false;
  1344. break;
  1345. }
  1346. if (clear)
  1347. break;
  1348. }
  1349. while (!children.empty()) {
  1350. Node *c_node = children.front()->get();
  1351. data.parent->add_child(c_node);
  1352. c_node->_propagate_replace_owner(NULL, new_owner);
  1353. children.pop_front();
  1354. }
  1355. data.parent->remove_child(this);
  1356. }
  1357. void Node::set_filename(const String &p_filename) {
  1358. data.filename = p_filename;
  1359. }
  1360. String Node::get_filename() const {
  1361. return data.filename;
  1362. }
  1363. void Node::set_editor_description(const String &p_editor_description) {
  1364. set_meta("_editor_description_", p_editor_description);
  1365. }
  1366. String Node::get_editor_description() const {
  1367. if (has_meta("_editor_description_")) {
  1368. return get_meta("_editor_description_");
  1369. } else {
  1370. return "";
  1371. }
  1372. }
  1373. void Node::set_editable_instance(Node *p_node, bool p_editable) {
  1374. ERR_FAIL_NULL(p_node);
  1375. ERR_FAIL_COND(!is_a_parent_of(p_node));
  1376. NodePath p = get_path_to(p_node);
  1377. if (!p_editable) {
  1378. data.editable_instances.erase(p);
  1379. // Avoid this flag being needlessly saved;
  1380. // also give more visual feedback if editable children is re-enabled
  1381. set_display_folded(false);
  1382. } else {
  1383. data.editable_instances[p] = true;
  1384. }
  1385. }
  1386. bool Node::is_editable_instance(const Node *p_node) const {
  1387. if (!p_node)
  1388. return false; //easier, null is never editable :)
  1389. ERR_FAIL_COND_V(!is_a_parent_of(p_node), false);
  1390. NodePath p = get_path_to(p_node);
  1391. return data.editable_instances.has(p);
  1392. }
  1393. void Node::set_editable_instances(const HashMap<NodePath, int> &p_editable_instances) {
  1394. data.editable_instances = p_editable_instances;
  1395. }
  1396. HashMap<NodePath, int> Node::get_editable_instances() const {
  1397. return data.editable_instances;
  1398. }
  1399. void Node::set_scene_instance_state(const Ref<SceneState> &p_state) {
  1400. data.instance_state = p_state;
  1401. }
  1402. Ref<SceneState> Node::get_scene_instance_state() const {
  1403. return data.instance_state;
  1404. }
  1405. void Node::set_scene_inherited_state(const Ref<SceneState> &p_state) {
  1406. data.inherited_state = p_state;
  1407. }
  1408. Ref<SceneState> Node::get_scene_inherited_state() const {
  1409. return data.inherited_state;
  1410. }
  1411. void Node::set_scene_instance_load_placeholder(bool p_enable) {
  1412. data.use_placeholder = p_enable;
  1413. }
  1414. bool Node::get_scene_instance_load_placeholder() const {
  1415. return data.use_placeholder;
  1416. }
  1417. int Node::get_position_in_parent() const {
  1418. return data.pos;
  1419. }
  1420. Node *Node::_duplicate(int p_flags, Map<const Node *, Node *> *r_duplimap) const {
  1421. Node *node = NULL;
  1422. bool instanced = false;
  1423. if (Object::cast_to<InstancePlaceholder>(this)) {
  1424. const InstancePlaceholder *ip = Object::cast_to<const InstancePlaceholder>(this);
  1425. InstancePlaceholder *nip = memnew(InstancePlaceholder);
  1426. nip->set_instance_path(ip->get_instance_path());
  1427. node = nip;
  1428. } else if ((p_flags & DUPLICATE_USE_INSTANCING) && get_filename() != String()) {
  1429. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  1430. ERR_FAIL_COND_V(res.is_null(), NULL);
  1431. PackedScene::GenEditState ges = PackedScene::GEN_EDIT_STATE_DISABLED;
  1432. #ifdef TOOLS_ENABLED
  1433. if (p_flags & DUPLICATE_FROM_EDITOR)
  1434. ges = PackedScene::GEN_EDIT_STATE_INSTANCE;
  1435. #endif
  1436. node = res->instance(ges);
  1437. ERR_FAIL_COND_V(!node, NULL);
  1438. instanced = true;
  1439. } else {
  1440. Object *obj = ClassDB::instance(get_class());
  1441. ERR_FAIL_COND_V(!obj, NULL);
  1442. node = Object::cast_to<Node>(obj);
  1443. if (!node)
  1444. memdelete(obj);
  1445. ERR_FAIL_COND_V(!node, NULL);
  1446. }
  1447. if (get_filename() != "") { //an instance
  1448. node->set_filename(get_filename());
  1449. }
  1450. StringName script_property_name = CoreStringNames::get_singleton()->_script;
  1451. List<const Node *> hidden_roots;
  1452. List<const Node *> node_tree;
  1453. node_tree.push_front(this);
  1454. if (instanced) {
  1455. // Since nodes in the instanced hierarchy won't be duplicated explicitly, we need to make an inventory
  1456. // of all the nodes in the tree of the instanced scene in order to transfer the values of the properties
  1457. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1458. for (int i = 0; i < N->get()->get_child_count(); ++i) {
  1459. Node *descendant = N->get()->get_child(i);
  1460. // Skip nodes not really belonging to the instanced hierarchy; they'll be processed normally later
  1461. // but remember non-instanced nodes that are hidden below instanced ones
  1462. if (descendant->data.owner != this) {
  1463. if (descendant->get_parent() && descendant->get_parent() != this && descendant->get_parent()->data.owner == this && descendant->data.owner != descendant->get_parent())
  1464. hidden_roots.push_back(descendant);
  1465. continue;
  1466. }
  1467. node_tree.push_back(descendant);
  1468. }
  1469. }
  1470. }
  1471. for (List<const Node *>::Element *N = node_tree.front(); N; N = N->next()) {
  1472. Node *current_node = node->get_node(get_path_to(N->get()));
  1473. ERR_CONTINUE(!current_node);
  1474. if (p_flags & DUPLICATE_SCRIPTS) {
  1475. bool is_valid = false;
  1476. Variant script = N->get()->get(script_property_name, &is_valid);
  1477. if (is_valid) {
  1478. current_node->set(script_property_name, script);
  1479. }
  1480. }
  1481. List<PropertyInfo> plist;
  1482. N->get()->get_property_list(&plist);
  1483. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1484. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1485. continue;
  1486. String name = E->get().name;
  1487. if (name == script_property_name)
  1488. continue;
  1489. Variant value = N->get()->get(name).duplicate(true);
  1490. if (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE) {
  1491. Resource *res = Object::cast_to<Resource>(value);
  1492. if (res) { // Duplicate only if it's a resource
  1493. current_node->set(name, res->duplicate());
  1494. }
  1495. } else {
  1496. current_node->set(name, value);
  1497. }
  1498. }
  1499. }
  1500. if (get_name() != String()) {
  1501. node->set_name(get_name());
  1502. }
  1503. #ifdef TOOLS_ENABLED
  1504. if ((p_flags & DUPLICATE_FROM_EDITOR) && r_duplimap)
  1505. r_duplimap->insert(this, node);
  1506. #endif
  1507. if (p_flags & DUPLICATE_GROUPS) {
  1508. List<GroupInfo> gi;
  1509. get_groups(&gi);
  1510. for (List<GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  1511. #ifdef TOOLS_ENABLED
  1512. if ((p_flags & DUPLICATE_FROM_EDITOR) && !E->get().persistent)
  1513. continue;
  1514. #endif
  1515. node->add_to_group(E->get().name, E->get().persistent);
  1516. }
  1517. }
  1518. for (int i = 0; i < get_child_count(); i++) {
  1519. if (get_child(i)->data.parent_owned)
  1520. continue;
  1521. if (instanced && get_child(i)->data.owner == this)
  1522. continue; //part of instance
  1523. Node *dup = get_child(i)->_duplicate(p_flags, r_duplimap);
  1524. if (!dup) {
  1525. memdelete(node);
  1526. return NULL;
  1527. }
  1528. node->add_child(dup);
  1529. if (i < node->get_child_count() - 1) {
  1530. node->move_child(dup, i);
  1531. }
  1532. }
  1533. for (List<const Node *>::Element *E = hidden_roots.front(); E; E = E->next()) {
  1534. Node *parent = node->get_node(get_path_to(E->get()->data.parent));
  1535. if (!parent) {
  1536. memdelete(node);
  1537. return NULL;
  1538. }
  1539. Node *dup = E->get()->_duplicate(p_flags, r_duplimap);
  1540. if (!dup) {
  1541. memdelete(node);
  1542. return NULL;
  1543. }
  1544. parent->add_child(dup);
  1545. int pos = E->get()->get_position_in_parent();
  1546. if (pos < parent->get_child_count() - 1) {
  1547. parent->move_child(dup, pos);
  1548. }
  1549. }
  1550. return node;
  1551. }
  1552. Node *Node::duplicate(int p_flags) const {
  1553. Node *dupe = _duplicate(p_flags);
  1554. if (dupe && (p_flags & DUPLICATE_SIGNALS)) {
  1555. _duplicate_signals(this, dupe);
  1556. }
  1557. return dupe;
  1558. }
  1559. #ifdef TOOLS_ENABLED
  1560. Node *Node::duplicate_from_editor(Map<const Node *, Node *> &r_duplimap) const {
  1561. Node *dupe = _duplicate(DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS | DUPLICATE_USE_INSTANCING | DUPLICATE_FROM_EDITOR, &r_duplimap);
  1562. // Duplication of signals must happen after all the node descendants have been copied,
  1563. // because re-targeting of connections from some descendant to another is not possible
  1564. // if the emitter node comes later in tree order than the receiver
  1565. _duplicate_signals(this, dupe);
  1566. return dupe;
  1567. }
  1568. #endif
  1569. void Node::_duplicate_and_reown(Node *p_new_parent, const Map<Node *, Node *> &p_reown_map) const {
  1570. if (get_owner() != get_parent()->get_owner())
  1571. return;
  1572. Node *node = NULL;
  1573. if (get_filename() != "") {
  1574. Ref<PackedScene> res = ResourceLoader::load(get_filename());
  1575. ERR_FAIL_COND_MSG(res.is_null(), "Cannot load scene: " + get_filename());
  1576. node = res->instance();
  1577. ERR_FAIL_COND(!node);
  1578. } else {
  1579. Object *obj = ClassDB::instance(get_class());
  1580. ERR_FAIL_COND_MSG(!obj, "Node: Could not duplicate: " + String(get_class()) + ".");
  1581. node = Object::cast_to<Node>(obj);
  1582. if (!node) {
  1583. memdelete(obj);
  1584. ERR_FAIL_MSG("Node: Could not duplicate: " + String(get_class()) + ".");
  1585. }
  1586. }
  1587. List<PropertyInfo> plist;
  1588. get_property_list(&plist);
  1589. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1590. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1591. continue;
  1592. String name = E->get().name;
  1593. Variant value = get(name).duplicate(true);
  1594. node->set(name, value);
  1595. }
  1596. List<GroupInfo> groups;
  1597. get_groups(&groups);
  1598. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next())
  1599. node->add_to_group(E->get().name, E->get().persistent);
  1600. node->set_name(get_name());
  1601. p_new_parent->add_child(node);
  1602. Node *owner = get_owner();
  1603. if (p_reown_map.has(owner))
  1604. owner = p_reown_map[owner];
  1605. if (owner) {
  1606. NodePath p = get_path_to(owner);
  1607. if (owner != this) {
  1608. Node *new_owner = node->get_node(p);
  1609. if (new_owner) {
  1610. node->set_owner(new_owner);
  1611. }
  1612. }
  1613. }
  1614. for (int i = 0; i < get_child_count(); i++) {
  1615. get_child(i)->_duplicate_and_reown(node, p_reown_map);
  1616. }
  1617. }
  1618. // Duplication of signals must happen after all the node descendants have been copied,
  1619. // because re-targeting of connections from some descendant to another is not possible
  1620. // if the emitter node comes later in tree order than the receiver
  1621. void Node::_duplicate_signals(const Node *p_original, Node *p_copy) const {
  1622. if ((this != p_original) && !(p_original->is_a_parent_of(this))) {
  1623. return;
  1624. }
  1625. List<const Node *> process_list;
  1626. process_list.push_back(this);
  1627. while (!process_list.empty()) {
  1628. const Node *n = process_list.front()->get();
  1629. process_list.pop_front();
  1630. List<Connection> conns;
  1631. n->get_all_signal_connections(&conns);
  1632. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  1633. if (E->get().flags & CONNECT_PERSIST) {
  1634. //user connected
  1635. NodePath p = p_original->get_path_to(n);
  1636. Node *copy = p_copy->get_node(p);
  1637. Node *target = Object::cast_to<Node>(E->get().target);
  1638. if (!target) {
  1639. continue;
  1640. }
  1641. NodePath ptarget = p_original->get_path_to(target);
  1642. Node *copytarget = target;
  1643. // Attempt to find a path to the duplicate target, if it seems it's not part
  1644. // of the duplicated and not yet parented hierarchy then at least try to connect
  1645. // to the same target as the original
  1646. if (p_copy->has_node(ptarget)) {
  1647. copytarget = p_copy->get_node(ptarget);
  1648. }
  1649. if (copy && copytarget && !copy->is_connected(E->get().signal, copytarget, E->get().method)) {
  1650. copy->connect(E->get().signal, copytarget, E->get().method, E->get().binds, E->get().flags);
  1651. }
  1652. }
  1653. }
  1654. for (int i = 0; i < n->get_child_count(); i++) {
  1655. process_list.push_back(n->get_child(i));
  1656. }
  1657. }
  1658. }
  1659. Node *Node::duplicate_and_reown(const Map<Node *, Node *> &p_reown_map) const {
  1660. ERR_FAIL_COND_V(get_filename() != "", NULL);
  1661. Object *obj = ClassDB::instance(get_class());
  1662. ERR_FAIL_COND_V_MSG(!obj, NULL, "Node: Could not duplicate: " + String(get_class()) + ".");
  1663. Node *node = Object::cast_to<Node>(obj);
  1664. if (!node) {
  1665. memdelete(obj);
  1666. ERR_FAIL_V_MSG(NULL, "Node: Could not duplicate: " + String(get_class()) + ".");
  1667. }
  1668. node->set_name(get_name());
  1669. List<PropertyInfo> plist;
  1670. get_property_list(&plist);
  1671. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1672. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1673. continue;
  1674. String name = E->get().name;
  1675. node->set(name, get(name));
  1676. }
  1677. List<GroupInfo> groups;
  1678. get_groups(&groups);
  1679. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next())
  1680. node->add_to_group(E->get().name, E->get().persistent);
  1681. for (int i = 0; i < get_child_count(); i++) {
  1682. get_child(i)->_duplicate_and_reown(node, p_reown_map);
  1683. }
  1684. // Duplication of signals must happen after all the node descendants have been copied,
  1685. // because re-targeting of connections from some descendant to another is not possible
  1686. // if the emitter node comes later in tree order than the receiver
  1687. _duplicate_signals(this, node);
  1688. return node;
  1689. }
  1690. static void find_owned_by(Node *p_by, Node *p_node, List<Node *> *p_owned) {
  1691. if (p_node->get_owner() == p_by)
  1692. p_owned->push_back(p_node);
  1693. for (int i = 0; i < p_node->get_child_count(); i++) {
  1694. find_owned_by(p_by, p_node->get_child(i), p_owned);
  1695. }
  1696. }
  1697. struct _NodeReplaceByPair {
  1698. String name;
  1699. Variant value;
  1700. };
  1701. void Node::replace_by(Node *p_node, bool p_keep_data) {
  1702. ERR_FAIL_NULL(p_node);
  1703. ERR_FAIL_COND(p_node->data.parent);
  1704. List<Node *> owned = data.owned;
  1705. List<Node *> owned_by_owner;
  1706. Node *owner = (data.owner == this) ? p_node : data.owner;
  1707. List<_NodeReplaceByPair> replace_data;
  1708. if (p_keep_data) {
  1709. List<PropertyInfo> plist;
  1710. get_property_list(&plist);
  1711. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1712. _NodeReplaceByPair rd;
  1713. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  1714. continue;
  1715. rd.name = E->get().name;
  1716. rd.value = get(rd.name);
  1717. }
  1718. List<GroupInfo> groups;
  1719. get_groups(&groups);
  1720. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next())
  1721. p_node->add_to_group(E->get().name, E->get().persistent);
  1722. }
  1723. _replace_connections_target(p_node);
  1724. if (data.owner) {
  1725. for (int i = 0; i < get_child_count(); i++)
  1726. find_owned_by(data.owner, get_child(i), &owned_by_owner);
  1727. }
  1728. Node *parent = data.parent;
  1729. int pos_in_parent = data.pos;
  1730. if (data.parent) {
  1731. parent->remove_child(this);
  1732. parent->add_child(p_node);
  1733. parent->move_child(p_node, pos_in_parent);
  1734. }
  1735. while (get_child_count()) {
  1736. Node *child = get_child(0);
  1737. remove_child(child);
  1738. if (!child->is_owned_by_parent()) {
  1739. // add the custom children to the p_node
  1740. p_node->add_child(child);
  1741. }
  1742. }
  1743. p_node->set_owner(owner);
  1744. for (int i = 0; i < owned.size(); i++)
  1745. owned[i]->set_owner(p_node);
  1746. for (int i = 0; i < owned_by_owner.size(); i++)
  1747. owned_by_owner[i]->set_owner(owner);
  1748. p_node->set_filename(get_filename());
  1749. for (List<_NodeReplaceByPair>::Element *E = replace_data.front(); E; E = E->next()) {
  1750. p_node->set(E->get().name, E->get().value);
  1751. }
  1752. }
  1753. void Node::_replace_connections_target(Node *p_new_target) {
  1754. List<Connection> cl;
  1755. get_signals_connected_to_this(&cl);
  1756. for (List<Connection>::Element *E = cl.front(); E; E = E->next()) {
  1757. Connection &c = E->get();
  1758. if (c.flags & CONNECT_PERSIST) {
  1759. c.source->disconnect(c.signal, this, c.method);
  1760. 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);
  1761. ERR_CONTINUE_MSG(!valid, "Attempt to connect signal '" + c.source->get_class() + "." + c.signal + "' to nonexistent method '" + c.target->get_class() + "." + c.method + "'.");
  1762. c.source->connect(c.signal, p_new_target, c.method, c.binds, c.flags);
  1763. }
  1764. }
  1765. }
  1766. Vector<Variant> Node::make_binds(VARIANT_ARG_DECLARE) {
  1767. Vector<Variant> ret;
  1768. if (p_arg1.get_type() == Variant::NIL)
  1769. return ret;
  1770. else
  1771. ret.push_back(p_arg1);
  1772. if (p_arg2.get_type() == Variant::NIL)
  1773. return ret;
  1774. else
  1775. ret.push_back(p_arg2);
  1776. if (p_arg3.get_type() == Variant::NIL)
  1777. return ret;
  1778. else
  1779. ret.push_back(p_arg3);
  1780. if (p_arg4.get_type() == Variant::NIL)
  1781. return ret;
  1782. else
  1783. ret.push_back(p_arg4);
  1784. if (p_arg5.get_type() == Variant::NIL)
  1785. return ret;
  1786. else
  1787. ret.push_back(p_arg5);
  1788. return ret;
  1789. }
  1790. bool Node::has_node_and_resource(const NodePath &p_path) const {
  1791. if (!has_node(p_path))
  1792. return false;
  1793. RES res;
  1794. Vector<StringName> leftover_path;
  1795. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  1796. return node;
  1797. }
  1798. Array Node::_get_node_and_resource(const NodePath &p_path) {
  1799. RES res;
  1800. Vector<StringName> leftover_path;
  1801. Node *node = get_node_and_resource(p_path, res, leftover_path, false);
  1802. Array result;
  1803. if (node)
  1804. result.push_back(node);
  1805. else
  1806. result.push_back(Variant());
  1807. if (res.is_valid())
  1808. result.push_back(res);
  1809. else
  1810. result.push_back(Variant());
  1811. result.push_back(NodePath(Vector<StringName>(), leftover_path, false));
  1812. return result;
  1813. }
  1814. Node *Node::get_node_and_resource(const NodePath &p_path, RES &r_res, Vector<StringName> &r_leftover_subpath, bool p_last_is_property) const {
  1815. Node *node = get_node(p_path);
  1816. r_res = RES();
  1817. r_leftover_subpath = Vector<StringName>();
  1818. if (!node)
  1819. return NULL;
  1820. if (p_path.get_subname_count()) {
  1821. int j = 0;
  1822. // If not p_last_is_property, we shouldn't consider the last one as part of the resource
  1823. for (; j < p_path.get_subname_count() - (int)p_last_is_property; j++) {
  1824. Variant new_res_v = j == 0 ? node->get(p_path.get_subname(j)) : r_res->get(p_path.get_subname(j));
  1825. if (new_res_v.get_type() == Variant::NIL) { // Found nothing on that path
  1826. return NULL;
  1827. }
  1828. RES new_res = new_res_v;
  1829. if (new_res.is_null()) { // No longer a resource, assume property
  1830. break;
  1831. }
  1832. r_res = new_res;
  1833. }
  1834. for (; j < p_path.get_subname_count(); j++) {
  1835. // Put the rest of the subpath in the leftover path
  1836. r_leftover_subpath.push_back(p_path.get_subname(j));
  1837. }
  1838. }
  1839. return node;
  1840. }
  1841. void Node::_set_tree(SceneTree *p_tree) {
  1842. SceneTree *tree_changed_a = NULL;
  1843. SceneTree *tree_changed_b = NULL;
  1844. //ERR_FAIL_COND(p_scene && data.parent && !data.parent->data.scene); //nobug if both are null
  1845. if (data.tree) {
  1846. _propagate_exit_tree();
  1847. tree_changed_a = data.tree;
  1848. }
  1849. data.tree = p_tree;
  1850. if (data.tree) {
  1851. _propagate_enter_tree();
  1852. if (!data.parent || data.parent->data.ready_notified) { // No parent (root) or parent ready
  1853. _propagate_ready(); //reverse_notification(NOTIFICATION_READY);
  1854. }
  1855. tree_changed_b = data.tree;
  1856. }
  1857. if (tree_changed_a)
  1858. tree_changed_a->tree_changed();
  1859. if (tree_changed_b)
  1860. tree_changed_b->tree_changed();
  1861. }
  1862. #ifdef DEBUG_ENABLED
  1863. static void _Node_debug_sn(Object *p_obj) {
  1864. Node *n = Object::cast_to<Node>(p_obj);
  1865. if (!n)
  1866. return;
  1867. if (n->is_inside_tree())
  1868. return;
  1869. Node *p = n;
  1870. while (p->get_parent()) {
  1871. p = p->get_parent();
  1872. }
  1873. String path;
  1874. if (p == n)
  1875. path = n->get_name();
  1876. else
  1877. path = String(p->get_name()) + "/" + p->get_path_to(n);
  1878. print_line(itos(p_obj->get_instance_id()) + " - Stray Node: " + path + " (Type: " + n->get_class() + ")");
  1879. }
  1880. #endif // DEBUG_ENABLED
  1881. void Node::_print_stray_nodes() {
  1882. print_stray_nodes();
  1883. }
  1884. void Node::print_stray_nodes() {
  1885. #ifdef DEBUG_ENABLED
  1886. ObjectDB::debug_objects(_Node_debug_sn);
  1887. #endif
  1888. }
  1889. void Node::queue_delete() {
  1890. if (is_inside_tree()) {
  1891. get_tree()->queue_delete(this);
  1892. } else {
  1893. SceneTree::get_singleton()->queue_delete(this);
  1894. }
  1895. }
  1896. Array Node::_get_children() const {
  1897. Array arr;
  1898. int cc = get_child_count();
  1899. arr.resize(cc);
  1900. for (int i = 0; i < cc; i++)
  1901. arr[i] = get_child(i);
  1902. return arr;
  1903. }
  1904. void Node::set_import_path(const NodePath &p_import_path) {
  1905. #ifdef TOOLS_ENABLED
  1906. data.import_path = p_import_path;
  1907. #endif
  1908. }
  1909. NodePath Node::get_import_path() const {
  1910. #ifdef TOOLS_ENABLED
  1911. return data.import_path;
  1912. #else
  1913. return NodePath();
  1914. #endif
  1915. }
  1916. static void _add_nodes_to_options(const Node *p_base, const Node *p_node, List<String> *r_options) {
  1917. #ifdef TOOLS_ENABLED
  1918. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
  1919. #else
  1920. const String quote_style = "\"";
  1921. #endif
  1922. if (p_node != p_base && !p_node->get_owner())
  1923. return;
  1924. String n = p_base->get_path_to(p_node);
  1925. r_options->push_back(quote_style + n + quote_style);
  1926. for (int i = 0; i < p_node->get_child_count(); i++) {
  1927. _add_nodes_to_options(p_base, p_node->get_child(i), r_options);
  1928. }
  1929. }
  1930. void Node::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1931. String pf = p_function;
  1932. if ((pf == "has_node" || pf == "get_node") && p_idx == 0) {
  1933. _add_nodes_to_options(this, this, r_options);
  1934. }
  1935. Object::get_argument_options(p_function, p_idx, r_options);
  1936. }
  1937. void Node::clear_internal_tree_resource_paths() {
  1938. clear_internal_resource_paths();
  1939. for (int i = 0; i < data.children.size(); i++) {
  1940. data.children[i]->clear_internal_tree_resource_paths();
  1941. }
  1942. }
  1943. String Node::get_configuration_warning() const {
  1944. if (get_script_instance() && get_script_instance()->get_script().is_valid() &&
  1945. get_script_instance()->get_script()->is_tool() && get_script_instance()->has_method("_get_configuration_warning")) {
  1946. return get_script_instance()->call("_get_configuration_warning");
  1947. }
  1948. return String();
  1949. }
  1950. void Node::update_configuration_warning() {
  1951. #ifdef TOOLS_ENABLED
  1952. if (!is_inside_tree())
  1953. return;
  1954. 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))) {
  1955. get_tree()->emit_signal(SceneStringNames::get_singleton()->node_configuration_warning_changed, this);
  1956. }
  1957. #endif
  1958. }
  1959. bool Node::is_owned_by_parent() const {
  1960. return data.parent_owned;
  1961. }
  1962. void Node::set_display_folded(bool p_folded) {
  1963. data.display_folded = p_folded;
  1964. }
  1965. bool Node::is_displayed_folded() const {
  1966. return data.display_folded;
  1967. }
  1968. void Node::request_ready() {
  1969. data.ready_first = true;
  1970. }
  1971. void Node::_bind_methods() {
  1972. GLOBAL_DEF("node/name_num_separator", 0);
  1973. 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"));
  1974. GLOBAL_DEF("node/name_casing", NAME_CASING_PASCAL_CASE);
  1975. ProjectSettings::get_singleton()->set_custom_property_info("node/name_casing", PropertyInfo(Variant::INT, "node/name_casing", PROPERTY_HINT_ENUM, "PascalCase,camelCase,snake_case"));
  1976. ClassDB::bind_method(D_METHOD("add_child_below_node", "node", "child_node", "legible_unique_name"), &Node::add_child_below_node, DEFVAL(false));
  1977. ClassDB::bind_method(D_METHOD("set_name", "name"), &Node::set_name);
  1978. ClassDB::bind_method(D_METHOD("get_name"), &Node::get_name);
  1979. ClassDB::bind_method(D_METHOD("add_child", "node", "legible_unique_name"), &Node::add_child, DEFVAL(false));
  1980. ClassDB::bind_method(D_METHOD("remove_child", "node"), &Node::remove_child);
  1981. ClassDB::bind_method(D_METHOD("get_child_count"), &Node::get_child_count);
  1982. ClassDB::bind_method(D_METHOD("get_children"), &Node::_get_children);
  1983. ClassDB::bind_method(D_METHOD("get_child", "idx"), &Node::get_child);
  1984. ClassDB::bind_method(D_METHOD("has_node", "path"), &Node::has_node);
  1985. ClassDB::bind_method(D_METHOD("get_node", "path"), &Node::get_node);
  1986. ClassDB::bind_method(D_METHOD("get_node_or_null", "path"), &Node::get_node_or_null);
  1987. ClassDB::bind_method(D_METHOD("get_parent"), &Node::get_parent);
  1988. ClassDB::bind_method(D_METHOD("find_node", "mask", "recursive", "owned"), &Node::find_node, DEFVAL(true), DEFVAL(true));
  1989. ClassDB::bind_method(D_METHOD("find_parent", "mask"), &Node::find_parent);
  1990. ClassDB::bind_method(D_METHOD("has_node_and_resource", "path"), &Node::has_node_and_resource);
  1991. ClassDB::bind_method(D_METHOD("get_node_and_resource", "path"), &Node::_get_node_and_resource);
  1992. ClassDB::bind_method(D_METHOD("is_inside_tree"), &Node::is_inside_tree);
  1993. ClassDB::bind_method(D_METHOD("is_a_parent_of", "node"), &Node::is_a_parent_of);
  1994. ClassDB::bind_method(D_METHOD("is_greater_than", "node"), &Node::is_greater_than);
  1995. ClassDB::bind_method(D_METHOD("get_path"), &Node::get_path);
  1996. ClassDB::bind_method(D_METHOD("get_path_to", "node"), &Node::get_path_to);
  1997. ClassDB::bind_method(D_METHOD("add_to_group", "group", "persistent"), &Node::add_to_group, DEFVAL(false));
  1998. ClassDB::bind_method(D_METHOD("remove_from_group", "group"), &Node::remove_from_group);
  1999. ClassDB::bind_method(D_METHOD("is_in_group", "group"), &Node::is_in_group);
  2000. ClassDB::bind_method(D_METHOD("move_child", "child_node", "to_position"), &Node::move_child);
  2001. ClassDB::bind_method(D_METHOD("get_groups"), &Node::_get_groups);
  2002. ClassDB::bind_method(D_METHOD("raise"), &Node::raise);
  2003. ClassDB::bind_method(D_METHOD("set_owner", "owner"), &Node::set_owner);
  2004. ClassDB::bind_method(D_METHOD("get_owner"), &Node::get_owner);
  2005. ClassDB::bind_method(D_METHOD("remove_and_skip"), &Node::remove_and_skip);
  2006. ClassDB::bind_method(D_METHOD("get_index"), &Node::get_index);
  2007. ClassDB::bind_method(D_METHOD("print_tree"), &Node::print_tree);
  2008. ClassDB::bind_method(D_METHOD("print_tree_pretty"), &Node::print_tree_pretty);
  2009. ClassDB::bind_method(D_METHOD("set_filename", "filename"), &Node::set_filename);
  2010. ClassDB::bind_method(D_METHOD("get_filename"), &Node::get_filename);
  2011. ClassDB::bind_method(D_METHOD("propagate_notification", "what"), &Node::propagate_notification);
  2012. ClassDB::bind_method(D_METHOD("propagate_call", "method", "args", "parent_first"), &Node::propagate_call, DEFVAL(Array()), DEFVAL(false));
  2013. ClassDB::bind_method(D_METHOD("set_physics_process", "enable"), &Node::set_physics_process);
  2014. ClassDB::bind_method(D_METHOD("get_physics_process_delta_time"), &Node::get_physics_process_delta_time);
  2015. ClassDB::bind_method(D_METHOD("is_physics_processing"), &Node::is_physics_processing);
  2016. ClassDB::bind_method(D_METHOD("get_process_delta_time"), &Node::get_process_delta_time);
  2017. ClassDB::bind_method(D_METHOD("set_process", "enable"), &Node::set_process);
  2018. ClassDB::bind_method(D_METHOD("set_process_priority", "priority"), &Node::set_process_priority);
  2019. ClassDB::bind_method(D_METHOD("get_process_priority"), &Node::get_process_priority);
  2020. ClassDB::bind_method(D_METHOD("is_processing"), &Node::is_processing);
  2021. ClassDB::bind_method(D_METHOD("set_process_input", "enable"), &Node::set_process_input);
  2022. ClassDB::bind_method(D_METHOD("is_processing_input"), &Node::is_processing_input);
  2023. ClassDB::bind_method(D_METHOD("set_process_unhandled_input", "enable"), &Node::set_process_unhandled_input);
  2024. ClassDB::bind_method(D_METHOD("is_processing_unhandled_input"), &Node::is_processing_unhandled_input);
  2025. ClassDB::bind_method(D_METHOD("set_process_unhandled_key_input", "enable"), &Node::set_process_unhandled_key_input);
  2026. ClassDB::bind_method(D_METHOD("is_processing_unhandled_key_input"), &Node::is_processing_unhandled_key_input);
  2027. ClassDB::bind_method(D_METHOD("set_pause_mode", "mode"), &Node::set_pause_mode);
  2028. ClassDB::bind_method(D_METHOD("get_pause_mode"), &Node::get_pause_mode);
  2029. ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process);
  2030. ClassDB::bind_method(D_METHOD("print_stray_nodes"), &Node::_print_stray_nodes);
  2031. ClassDB::bind_method(D_METHOD("get_position_in_parent"), &Node::get_position_in_parent);
  2032. ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded);
  2033. ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded);
  2034. ClassDB::bind_method(D_METHOD("set_process_internal", "enable"), &Node::set_process_internal);
  2035. ClassDB::bind_method(D_METHOD("is_processing_internal"), &Node::is_processing_internal);
  2036. ClassDB::bind_method(D_METHOD("set_physics_process_internal", "enable"), &Node::set_physics_process_internal);
  2037. ClassDB::bind_method(D_METHOD("is_physics_processing_internal"), &Node::is_physics_processing_internal);
  2038. ClassDB::bind_method(D_METHOD("get_tree"), &Node::get_tree);
  2039. ClassDB::bind_method(D_METHOD("duplicate", "flags"), &Node::duplicate, DEFVAL(DUPLICATE_USE_INSTANCING | DUPLICATE_SIGNALS | DUPLICATE_GROUPS | DUPLICATE_SCRIPTS));
  2040. ClassDB::bind_method(D_METHOD("replace_by", "node", "keep_data"), &Node::replace_by, DEFVAL(false));
  2041. ClassDB::bind_method(D_METHOD("set_scene_instance_load_placeholder", "load_placeholder"), &Node::set_scene_instance_load_placeholder);
  2042. ClassDB::bind_method(D_METHOD("get_scene_instance_load_placeholder"), &Node::get_scene_instance_load_placeholder);
  2043. ClassDB::bind_method(D_METHOD("get_viewport"), &Node::get_viewport);
  2044. ClassDB::bind_method(D_METHOD("queue_free"), &Node::queue_delete);
  2045. ClassDB::bind_method(D_METHOD("request_ready"), &Node::request_ready);
  2046. ClassDB::bind_method(D_METHOD("set_network_master", "id", "recursive"), &Node::set_network_master, DEFVAL(true));
  2047. ClassDB::bind_method(D_METHOD("get_network_master"), &Node::get_network_master);
  2048. ClassDB::bind_method(D_METHOD("is_network_master"), &Node::is_network_master);
  2049. ClassDB::bind_method(D_METHOD("get_multiplayer"), &Node::get_multiplayer);
  2050. ClassDB::bind_method(D_METHOD("get_custom_multiplayer"), &Node::get_custom_multiplayer);
  2051. ClassDB::bind_method(D_METHOD("set_custom_multiplayer", "api"), &Node::set_custom_multiplayer);
  2052. ClassDB::bind_method(D_METHOD("rpc_config", "method", "mode"), &Node::rpc_config);
  2053. ClassDB::bind_method(D_METHOD("rset_config", "property", "mode"), &Node::rset_config);
  2054. ClassDB::bind_method(D_METHOD("_set_editor_description", "editor_description"), &Node::set_editor_description);
  2055. ClassDB::bind_method(D_METHOD("_get_editor_description"), &Node::get_editor_description);
  2056. ADD_PROPERTY(PropertyInfo(Variant::STRING, "editor_description", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_editor_description", "_get_editor_description");
  2057. ClassDB::bind_method(D_METHOD("_set_import_path", "import_path"), &Node::set_import_path);
  2058. ClassDB::bind_method(D_METHOD("_get_import_path"), &Node::get_import_path);
  2059. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "_import_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_import_path", "_get_import_path");
  2060. {
  2061. MethodInfo mi;
  2062. mi.arguments.push_back(PropertyInfo(Variant::STRING, "method"));
  2063. mi.name = "rpc";
  2064. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc", &Node::_rpc_bind, mi);
  2065. mi.name = "rpc_unreliable";
  2066. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_unreliable", &Node::_rpc_unreliable_bind, mi);
  2067. mi.arguments.push_front(PropertyInfo(Variant::INT, "peer_id"));
  2068. mi.name = "rpc_id";
  2069. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_id", &Node::_rpc_id_bind, mi);
  2070. mi.name = "rpc_unreliable_id";
  2071. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "rpc_unreliable_id", &Node::_rpc_unreliable_id_bind, mi);
  2072. }
  2073. ClassDB::bind_method(D_METHOD("rset", "property", "value"), &Node::rset);
  2074. ClassDB::bind_method(D_METHOD("rset_id", "peer_id", "property", "value"), &Node::rset_id);
  2075. ClassDB::bind_method(D_METHOD("rset_unreliable", "property", "value"), &Node::rset_unreliable);
  2076. ClassDB::bind_method(D_METHOD("rset_unreliable_id", "peer_id", "property", "value"), &Node::rset_unreliable_id);
  2077. ClassDB::bind_method(D_METHOD("update_configuration_warning"), &Node::update_configuration_warning);
  2078. BIND_CONSTANT(NOTIFICATION_ENTER_TREE);
  2079. BIND_CONSTANT(NOTIFICATION_EXIT_TREE);
  2080. BIND_CONSTANT(NOTIFICATION_MOVED_IN_PARENT);
  2081. BIND_CONSTANT(NOTIFICATION_READY);
  2082. BIND_CONSTANT(NOTIFICATION_PAUSED);
  2083. BIND_CONSTANT(NOTIFICATION_UNPAUSED);
  2084. BIND_CONSTANT(NOTIFICATION_PHYSICS_PROCESS);
  2085. BIND_CONSTANT(NOTIFICATION_PROCESS);
  2086. BIND_CONSTANT(NOTIFICATION_PARENTED);
  2087. BIND_CONSTANT(NOTIFICATION_UNPARENTED);
  2088. BIND_CONSTANT(NOTIFICATION_INSTANCED);
  2089. BIND_CONSTANT(NOTIFICATION_DRAG_BEGIN);
  2090. BIND_CONSTANT(NOTIFICATION_DRAG_END);
  2091. BIND_CONSTANT(NOTIFICATION_PATH_CHANGED);
  2092. BIND_CONSTANT(NOTIFICATION_INTERNAL_PROCESS);
  2093. BIND_CONSTANT(NOTIFICATION_INTERNAL_PHYSICS_PROCESS);
  2094. BIND_CONSTANT(NOTIFICATION_POST_ENTER_TREE);
  2095. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER);
  2096. BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT);
  2097. BIND_CONSTANT(NOTIFICATION_WM_FOCUS_IN);
  2098. BIND_CONSTANT(NOTIFICATION_WM_FOCUS_OUT);
  2099. BIND_CONSTANT(NOTIFICATION_WM_QUIT_REQUEST);
  2100. BIND_CONSTANT(NOTIFICATION_WM_GO_BACK_REQUEST);
  2101. BIND_CONSTANT(NOTIFICATION_WM_UNFOCUS_REQUEST);
  2102. BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
  2103. BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
  2104. BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
  2105. BIND_CONSTANT(NOTIFICATION_CRASH);
  2106. BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
  2107. BIND_CONSTANT(NOTIFICATION_APP_RESUMED);
  2108. BIND_CONSTANT(NOTIFICATION_APP_PAUSED);
  2109. BIND_ENUM_CONSTANT(PAUSE_MODE_INHERIT);
  2110. BIND_ENUM_CONSTANT(PAUSE_MODE_STOP);
  2111. BIND_ENUM_CONSTANT(PAUSE_MODE_PROCESS);
  2112. BIND_ENUM_CONSTANT(DUPLICATE_SIGNALS);
  2113. BIND_ENUM_CONSTANT(DUPLICATE_GROUPS);
  2114. BIND_ENUM_CONSTANT(DUPLICATE_SCRIPTS);
  2115. BIND_ENUM_CONSTANT(DUPLICATE_USE_INSTANCING);
  2116. ADD_SIGNAL(MethodInfo("ready"));
  2117. ADD_SIGNAL(MethodInfo("renamed"));
  2118. ADD_SIGNAL(MethodInfo("tree_entered"));
  2119. ADD_SIGNAL(MethodInfo("tree_exiting"));
  2120. ADD_SIGNAL(MethodInfo("tree_exited"));
  2121. ADD_GROUP("Pause", "pause_");
  2122. ADD_PROPERTY(PropertyInfo(Variant::INT, "pause_mode", PROPERTY_HINT_ENUM, "Inherit,Stop,Process"), "set_pause_mode", "get_pause_mode");
  2123. #ifdef ENABLE_DEPRECATED
  2124. //no longer exists, but remains for compatibility (keep previous scenes folded
  2125. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor/display_folded", PROPERTY_HINT_NONE, "", 0), "set_display_folded", "is_displayed_folded");
  2126. #endif
  2127. ADD_PROPERTY(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "", 0), "set_name", "get_name");
  2128. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename", PROPERTY_HINT_NONE, "", 0), "set_filename", "get_filename");
  2129. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_owner", "get_owner");
  2130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "", "get_multiplayer");
  2131. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "custom_multiplayer", PROPERTY_HINT_RESOURCE_TYPE, "MultiplayerAPI", 0), "set_custom_multiplayer", "get_custom_multiplayer");
  2132. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_priority"), "set_process_priority", "get_process_priority");
  2133. BIND_VMETHOD(MethodInfo("_process", PropertyInfo(Variant::REAL, "delta")));
  2134. BIND_VMETHOD(MethodInfo("_physics_process", PropertyInfo(Variant::REAL, "delta")));
  2135. BIND_VMETHOD(MethodInfo("_enter_tree"));
  2136. BIND_VMETHOD(MethodInfo("_exit_tree"));
  2137. BIND_VMETHOD(MethodInfo("_ready"));
  2138. BIND_VMETHOD(MethodInfo("_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2139. BIND_VMETHOD(MethodInfo("_unhandled_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2140. BIND_VMETHOD(MethodInfo("_unhandled_key_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEventKey")));
  2141. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_configuration_warning"));
  2142. }
  2143. String Node::_get_name_num_separator() {
  2144. switch (ProjectSettings::get_singleton()->get("node/name_num_separator").operator int()) {
  2145. case 0: return "";
  2146. case 1: return " ";
  2147. case 2: return "_";
  2148. case 3: return "-";
  2149. }
  2150. return " ";
  2151. }
  2152. Node::Node() {
  2153. data.pos = -1;
  2154. data.depth = -1;
  2155. data.blocked = 0;
  2156. data.parent = NULL;
  2157. data.tree = NULL;
  2158. data.physics_process = false;
  2159. data.idle_process = false;
  2160. data.process_priority = 0;
  2161. data.physics_process_internal = false;
  2162. data.idle_process_internal = false;
  2163. data.inside_tree = false;
  2164. data.ready_notified = false;
  2165. data.owner = NULL;
  2166. data.OW = NULL;
  2167. data.input = false;
  2168. data.unhandled_input = false;
  2169. data.unhandled_key_input = false;
  2170. data.pause_mode = PAUSE_MODE_INHERIT;
  2171. data.pause_owner = NULL;
  2172. data.network_master = 1; //server by default
  2173. data.path_cache = NULL;
  2174. data.parent_owned = false;
  2175. data.in_constructor = true;
  2176. data.viewport = NULL;
  2177. data.use_placeholder = false;
  2178. data.display_folded = false;
  2179. data.ready_first = true;
  2180. orphan_node_count++;
  2181. }
  2182. Node::~Node() {
  2183. data.grouped.clear();
  2184. data.owned.clear();
  2185. data.children.clear();
  2186. ERR_FAIL_COND(data.parent);
  2187. ERR_FAIL_COND(data.children.size());
  2188. orphan_node_count--;
  2189. }
  2190. ////////////////////////////////