node.cpp 72 KB

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