node.cpp 75 KB

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