node.cpp 76 KB

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