node.cpp 80 KB

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