packed_scene.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /**************************************************************************/
  2. /* packed_scene.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "packed_scene.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/core_string_names.h"
  34. #include "core/io/missing_resource.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/templates/local_vector.h"
  37. #include "scene/2d/node_2d.h"
  38. #include "scene/3d/node_3d.h"
  39. #include "scene/gui/control.h"
  40. #include "scene/main/instance_placeholder.h"
  41. #include "scene/main/missing_node.h"
  42. #include "scene/property_utils.h"
  43. #define PACKED_SCENE_VERSION 3
  44. #define META_POINTER_PROPERTY_BASE "metadata/_editor_prop_ptr_"
  45. bool SceneState::can_instantiate() const {
  46. return nodes.size() > 0;
  47. }
  48. static Array _sanitize_node_pinned_properties(Node *p_node) {
  49. Array pinned = p_node->get_meta("_edit_pinned_properties_", Array());
  50. if (pinned.is_empty()) {
  51. return Array();
  52. }
  53. HashSet<StringName> storable_properties;
  54. p_node->get_storable_properties(storable_properties);
  55. int i = 0;
  56. do {
  57. if (storable_properties.has(pinned[i])) {
  58. i++;
  59. } else {
  60. pinned.remove_at(i);
  61. }
  62. } while (i < pinned.size());
  63. if (pinned.is_empty()) {
  64. p_node->remove_meta("_edit_pinned_properties_");
  65. }
  66. return pinned;
  67. }
  68. Node *SceneState::instantiate(GenEditState p_edit_state) const {
  69. // Nodes where instantiation failed (because something is missing.)
  70. List<Node *> stray_instances;
  71. #define NODE_FROM_ID(p_name, p_id) \
  72. Node *p_name; \
  73. if (p_id & FLAG_ID_IS_PATH) { \
  74. NodePath np = node_paths[p_id & FLAG_MASK]; \
  75. p_name = ret_nodes[0]->get_node_or_null(np); \
  76. } else { \
  77. ERR_FAIL_INDEX_V(p_id &FLAG_MASK, nc, nullptr); \
  78. p_name = ret_nodes[p_id & FLAG_MASK]; \
  79. }
  80. int nc = nodes.size();
  81. ERR_FAIL_COND_V(nc == 0, nullptr);
  82. const StringName *snames = nullptr;
  83. int sname_count = names.size();
  84. if (sname_count) {
  85. snames = &names[0];
  86. }
  87. const Variant *props = nullptr;
  88. int prop_count = variants.size();
  89. if (prop_count) {
  90. props = &variants[0];
  91. }
  92. //Vector<Variant> properties;
  93. const NodeData *nd = &nodes[0];
  94. Node **ret_nodes = (Node **)alloca(sizeof(Node *) * nc);
  95. bool gen_node_path_cache = p_edit_state != GEN_EDIT_STATE_DISABLED && node_path_cache.is_empty();
  96. HashMap<Ref<Resource>, Ref<Resource>> resources_local_to_scene;
  97. LocalVector<DeferredNodePathProperties> deferred_node_paths;
  98. for (int i = 0; i < nc; i++) {
  99. const NodeData &n = nd[i];
  100. Node *parent = nullptr;
  101. String old_parent_path;
  102. if (i > 0) {
  103. ERR_FAIL_COND_V_MSG(n.parent == -1, nullptr, vformat("Invalid scene: node %s does not specify its parent node.", snames[n.name]));
  104. NODE_FROM_ID(nparent, n.parent);
  105. #ifdef DEBUG_ENABLED
  106. if (!nparent && (n.parent & FLAG_ID_IS_PATH)) {
  107. WARN_PRINT(String("Parent path '" + String(node_paths[n.parent & FLAG_MASK]) + "' for node '" + String(snames[n.name]) + "' has vanished when instantiating: '" + get_path() + "'.").ascii().get_data());
  108. old_parent_path = String(node_paths[n.parent & FLAG_MASK]).trim_prefix("./").replace("/", "@");
  109. nparent = ret_nodes[0];
  110. }
  111. #endif
  112. parent = nparent;
  113. } else {
  114. // i == 0 is root node.
  115. ERR_FAIL_COND_V_MSG(n.parent != -1, nullptr, vformat("Invalid scene: root node %s cannot specify a parent node.", snames[n.name]));
  116. ERR_FAIL_COND_V_MSG(n.type == TYPE_INSTANTIATED && base_scene_idx < 0, nullptr, vformat("Invalid scene: root node %s in an instance, but there's no base scene.", snames[n.name]));
  117. }
  118. Node *node = nullptr;
  119. MissingNode *missing_node = nullptr;
  120. if (i == 0 && base_scene_idx >= 0) {
  121. //scene inheritance on root node
  122. Ref<PackedScene> sdata = props[base_scene_idx];
  123. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  124. node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE); //only main gets main edit state
  125. ERR_FAIL_COND_V(!node, nullptr);
  126. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  127. node->set_scene_inherited_state(sdata->get_state());
  128. }
  129. } else if (n.instance >= 0) {
  130. //instance a scene into this node
  131. if (n.instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  132. String scene_path = props[n.instance & FLAG_MASK];
  133. if (disable_placeholders) {
  134. Ref<PackedScene> sdata = ResourceLoader::load(scene_path, "PackedScene");
  135. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  136. node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  137. ERR_FAIL_COND_V(!node, nullptr);
  138. } else {
  139. InstancePlaceholder *ip = memnew(InstancePlaceholder);
  140. ip->set_instance_path(scene_path);
  141. node = ip;
  142. }
  143. node->set_scene_instance_load_placeholder(true);
  144. } else {
  145. Ref<PackedScene> sdata = props[n.instance & FLAG_MASK];
  146. ERR_FAIL_COND_V(!sdata.is_valid(), nullptr);
  147. node = sdata->instantiate(p_edit_state == GEN_EDIT_STATE_DISABLED ? PackedScene::GEN_EDIT_STATE_DISABLED : PackedScene::GEN_EDIT_STATE_INSTANCE);
  148. ERR_FAIL_COND_V(!node, nullptr);
  149. }
  150. } else if (n.type == TYPE_INSTANTIATED) {
  151. //get the node from somewhere, it likely already exists from another instance
  152. if (parent) {
  153. node = parent->_get_child_by_name(snames[n.name]);
  154. #ifdef DEBUG_ENABLED
  155. if (!node) {
  156. WARN_PRINT(String("Node '" + String(ret_nodes[0]->get_path_to(parent)) + "/" + String(snames[n.name]) + "' was modified from inside an instance, but it has vanished.").ascii().get_data());
  157. }
  158. #endif
  159. }
  160. } else {
  161. //node belongs to this scene and must be created
  162. Object *obj = ClassDB::instantiate(snames[n.type]);
  163. node = Object::cast_to<Node>(obj);
  164. if (!node) {
  165. if (obj) {
  166. memdelete(obj);
  167. obj = nullptr;
  168. }
  169. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) {
  170. missing_node = memnew(MissingNode);
  171. missing_node->set_original_class(snames[n.type]);
  172. missing_node->set_recording_properties(true);
  173. node = missing_node;
  174. obj = missing_node;
  175. } else {
  176. WARN_PRINT(vformat("Node %s of type %s cannot be created. A placeholder will be created instead.", snames[n.name], snames[n.type]).ascii().get_data());
  177. if (n.parent >= 0 && n.parent < nc && ret_nodes[n.parent]) {
  178. if (Object::cast_to<Control>(ret_nodes[n.parent])) {
  179. obj = memnew(Control);
  180. } else if (Object::cast_to<Node2D>(ret_nodes[n.parent])) {
  181. obj = memnew(Node2D);
  182. #ifndef _3D_DISABLED
  183. } else if (Object::cast_to<Node3D>(ret_nodes[n.parent])) {
  184. obj = memnew(Node3D);
  185. #endif // _3D_DISABLED
  186. }
  187. }
  188. if (!obj) {
  189. obj = memnew(Node);
  190. }
  191. node = Object::cast_to<Node>(obj);
  192. }
  193. }
  194. }
  195. if (node) {
  196. // may not have found the node (part of instantiated scene and removed)
  197. // if found all is good, otherwise ignore
  198. //properties
  199. int nprop_count = n.properties.size();
  200. if (nprop_count) {
  201. const NodeData::Property *nprops = &n.properties[0];
  202. Dictionary missing_resource_properties;
  203. for (int j = 0; j < nprop_count; j++) {
  204. bool valid;
  205. ERR_FAIL_INDEX_V(nprops[j].value, prop_count, nullptr);
  206. if (nprops[j].name & FLAG_PATH_PROPERTY_IS_NODE) {
  207. uint32_t name_idx = nprops[j].name & (FLAG_PATH_PROPERTY_IS_NODE - 1);
  208. ERR_FAIL_UNSIGNED_INDEX_V(name_idx, (uint32_t)sname_count, nullptr);
  209. if (Engine::get_singleton()->is_editor_hint()) {
  210. // If editor, just set the metadata and be it
  211. node->set(META_POINTER_PROPERTY_BASE + String(snames[name_idx]), props[nprops[j].value]);
  212. } else {
  213. // Do an actual deferred sed of the property path.
  214. DeferredNodePathProperties dnp;
  215. dnp.path = props[nprops[j].value];
  216. dnp.base = node;
  217. dnp.property = snames[name_idx];
  218. deferred_node_paths.push_back(dnp);
  219. }
  220. continue;
  221. }
  222. ERR_FAIL_INDEX_V(nprops[j].name, sname_count, nullptr);
  223. if (snames[nprops[j].name] == CoreStringNames::get_singleton()->_script) {
  224. //work around to avoid old script variables from disappearing, should be the proper fix to:
  225. //https://github.com/godotengine/godot/issues/2958
  226. //store old state
  227. List<Pair<StringName, Variant>> old_state;
  228. if (node->get_script_instance()) {
  229. node->get_script_instance()->get_property_state(old_state);
  230. }
  231. node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
  232. //restore old state for new script, if exists
  233. for (const Pair<StringName, Variant> &E : old_state) {
  234. node->set(E.first, E.second);
  235. }
  236. } else {
  237. Variant value = props[nprops[j].value];
  238. if (value.get_type() == Variant::OBJECT) {
  239. //handle resources that are local to scene by duplicating them if needed
  240. Ref<Resource> res = value;
  241. if (res.is_valid()) {
  242. if (res->is_local_to_scene()) {
  243. // In a situation where a local-to-scene resource is used in a child node of a non-editable instance,
  244. // we need to avoid the parent scene from overriding the resource potentially also used in the root
  245. // of the instantiated scene. That would to the instance having two different instances of the resource.
  246. // Since at this point it's too late to propagate the resource instance in the parent scene to all the relevant
  247. // nodes in the instance (and that would require very complex bookkepping), what we do instead is
  248. // tampering the resource object already there with the values from the node in the parent scene and
  249. // then tell this node to reference that resource.
  250. if (n.instance >= 0) {
  251. Ref<Resource> node_res = node->get(snames[nprops[j].name]);
  252. if (node_res.is_valid()) {
  253. node_res->copy_from(res);
  254. node_res->configure_for_local_scene(node, resources_local_to_scene);
  255. value = node_res;
  256. }
  257. } else {
  258. HashMap<Ref<Resource>, Ref<Resource>>::Iterator E = resources_local_to_scene.find(res);
  259. Node *base = i == 0 ? node : ret_nodes[0];
  260. if (E) {
  261. value = E->value;
  262. } else {
  263. if (p_edit_state == GEN_EDIT_STATE_MAIN) {
  264. //for the main scene, use the resource as is
  265. res->configure_for_local_scene(base, resources_local_to_scene);
  266. resources_local_to_scene[res] = res;
  267. } else {
  268. //for instances, a copy must be made
  269. Ref<Resource> local_dupe = res->duplicate_for_local_scene(base, resources_local_to_scene);
  270. resources_local_to_scene[res] = local_dupe;
  271. value = local_dupe;
  272. }
  273. }
  274. }
  275. //must make a copy, because this res is local to scene
  276. }
  277. }
  278. }
  279. if (value.get_type() == Variant::ARRAY) {
  280. Array set_array = value;
  281. bool is_get_valid = false;
  282. Variant get_value = node->get(snames[nprops[j].name], &is_get_valid);
  283. if (is_get_valid && get_value.get_type() == Variant::ARRAY) {
  284. Array get_array = get_value;
  285. if (!set_array.is_same_typed(get_array)) {
  286. value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script());
  287. }
  288. }
  289. }
  290. if (p_edit_state == GEN_EDIT_STATE_INSTANCE && value.get_type() != Variant::OBJECT) {
  291. value = value.duplicate(true); // Duplicate arrays and dictionaries for the editor
  292. }
  293. bool set_valid = true;
  294. if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled() && value.get_type() == Variant::OBJECT) {
  295. Ref<MissingResource> mr = value;
  296. if (mr.is_valid()) {
  297. missing_resource_properties[snames[nprops[j].name]] = mr;
  298. set_valid = false;
  299. }
  300. }
  301. if (set_valid) {
  302. node->set(snames[nprops[j].name], value, &valid);
  303. }
  304. }
  305. }
  306. if (!missing_resource_properties.is_empty()) {
  307. node->set_meta(META_MISSING_RESOURCES, missing_resource_properties);
  308. }
  309. }
  310. //name
  311. //groups
  312. for (int j = 0; j < n.groups.size(); j++) {
  313. ERR_FAIL_INDEX_V(n.groups[j], sname_count, nullptr);
  314. node->add_to_group(snames[n.groups[j]], true);
  315. }
  316. if (n.instance >= 0 || n.type != TYPE_INSTANTIATED || i == 0) {
  317. //if node was not part of instance, must set its name, parenthood and ownership
  318. if (i > 0) {
  319. if (parent) {
  320. parent->_add_child_nocheck(node, snames[n.name]);
  321. if (n.index >= 0 && n.index < parent->get_child_count() - 1) {
  322. parent->move_child(node, n.index);
  323. }
  324. } else {
  325. //it may be possible that an instantiated scene has changed
  326. //and the node has nowhere to go anymore
  327. stray_instances.push_back(node); //can't be added, go to stray list
  328. }
  329. } else {
  330. if (Engine::get_singleton()->is_editor_hint()) {
  331. //validate name if using editor, to avoid broken
  332. node->set_name(snames[n.name]);
  333. } else {
  334. node->_set_name_nocheck(snames[n.name]);
  335. }
  336. }
  337. }
  338. if (!old_parent_path.is_empty()) {
  339. node->_set_name_nocheck(old_parent_path + "@" + node->get_name());
  340. }
  341. if (n.owner >= 0) {
  342. NODE_FROM_ID(owner, n.owner);
  343. if (owner) {
  344. node->_set_owner_nocheck(owner);
  345. if (node->data.unique_name_in_owner) {
  346. node->_acquire_unique_name_in_owner();
  347. }
  348. }
  349. }
  350. // We only want to deal with pinned flag if instantiating as pure main (no instance, no inheriting.)
  351. if (p_edit_state == GEN_EDIT_STATE_MAIN) {
  352. _sanitize_node_pinned_properties(node);
  353. } else {
  354. node->remove_meta("_edit_pinned_properties_");
  355. }
  356. }
  357. if (missing_node) {
  358. missing_node->set_recording_properties(false);
  359. }
  360. ret_nodes[i] = node;
  361. if (node && gen_node_path_cache && ret_nodes[0]) {
  362. NodePath n2 = ret_nodes[0]->get_path_to(node);
  363. node_path_cache[n2] = i;
  364. }
  365. }
  366. for (const DeferredNodePathProperties &dnp : deferred_node_paths) {
  367. Node *other = dnp.base->get_node_or_null(dnp.path);
  368. dnp.base->set(dnp.property, other);
  369. }
  370. for (KeyValue<Ref<Resource>, Ref<Resource>> &E : resources_local_to_scene) {
  371. if (E.value->get_local_scene() == ret_nodes[0]) {
  372. E.value->setup_local_to_scene();
  373. }
  374. }
  375. //do connections
  376. int cc = connections.size();
  377. const ConnectionData *cdata = connections.ptr();
  378. for (int i = 0; i < cc; i++) {
  379. const ConnectionData &c = cdata[i];
  380. //ERR_FAIL_INDEX_V( c.from, nc, nullptr );
  381. //ERR_FAIL_INDEX_V( c.to, nc, nullptr );
  382. NODE_FROM_ID(cfrom, c.from);
  383. NODE_FROM_ID(cto, c.to);
  384. if (!cfrom || !cto) {
  385. continue;
  386. }
  387. Callable callable(cto, snames[c.method]);
  388. if (c.unbinds > 0) {
  389. callable = callable.unbind(c.unbinds);
  390. } else if (!c.binds.is_empty()) {
  391. Vector<Variant> binds;
  392. if (c.binds.size()) {
  393. binds.resize(c.binds.size());
  394. for (int j = 0; j < c.binds.size(); j++) {
  395. binds.write[j] = props[c.binds[j]];
  396. }
  397. }
  398. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
  399. for (int j = 0; j < binds.size(); j++) {
  400. argptrs[j] = &binds[j];
  401. }
  402. callable = callable.bindp(argptrs, binds.size());
  403. }
  404. cfrom->connect(snames[c.signal], callable, CONNECT_PERSIST | c.flags | (p_edit_state == GEN_EDIT_STATE_MAIN ? 0 : CONNECT_INHERITED));
  405. }
  406. //Node *s = ret_nodes[0];
  407. //remove nodes that could not be added, likely as a result that
  408. while (stray_instances.size()) {
  409. memdelete(stray_instances.front()->get());
  410. stray_instances.pop_front();
  411. }
  412. for (int i = 0; i < editable_instances.size(); i++) {
  413. Node *ei = ret_nodes[0]->get_node_or_null(editable_instances[i]);
  414. if (ei) {
  415. ret_nodes[0]->set_editable_instance(ei, true);
  416. }
  417. }
  418. return ret_nodes[0];
  419. }
  420. static int _nm_get_string(const String &p_string, HashMap<StringName, int> &name_map) {
  421. if (name_map.has(p_string)) {
  422. return name_map[p_string];
  423. }
  424. int idx = name_map.size();
  425. name_map[p_string] = idx;
  426. return idx;
  427. }
  428. static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map) {
  429. if (variant_map.has(p_variant)) {
  430. return variant_map[p_variant];
  431. }
  432. int idx = variant_map.size();
  433. variant_map[p_variant] = idx;
  434. return idx;
  435. }
  436. Error SceneState::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) {
  437. // this function handles all the work related to properly packing scenes, be it
  438. // instantiated or inherited.
  439. // given the complexity of this process, an attempt will be made to properly
  440. // document it. if you fail to understand something, please ask!
  441. //discard nodes that do not belong to be processed
  442. if (p_node != p_owner && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  443. return OK;
  444. }
  445. bool is_editable_instance = false;
  446. // save the child instantiated scenes that are chosen as editable, so they can be restored
  447. // upon load back
  448. if (p_node != p_owner && !p_node->get_scene_file_path().is_empty() && p_owner->is_editable_instance(p_node)) {
  449. editable_instances.push_back(p_owner->get_path_to(p_node));
  450. // Node is the root of an editable instance.
  451. is_editable_instance = true;
  452. } else if (p_node->get_owner() && p_owner->is_ancestor_of(p_node->get_owner()) && p_owner->is_editable_instance(p_node->get_owner())) {
  453. // Node is part of an editable instance.
  454. is_editable_instance = true;
  455. }
  456. NodeData nd;
  457. nd.name = _nm_get_string(p_node->get_name(), name_map);
  458. nd.instance = -1; //not instantiated by default
  459. //really convoluted condition, but it basically checks that index is only saved when part of an inherited scene OR the node parent is from the edited scene
  460. if (p_owner->get_scene_inherited_state().is_null() && (p_node == p_owner || (p_node->get_owner() == p_owner && (p_node->get_parent() == p_owner || p_node->get_parent()->get_owner() == p_owner)))) {
  461. //do not save index, because it belongs to saved scene and scene is not inherited
  462. nd.index = -1;
  463. } else if (p_node == p_owner) {
  464. //This (hopefully) happens if the node is a scene root, so its index is irrelevant.
  465. nd.index = -1;
  466. } else {
  467. //part of an inherited scene, or parent is from an instantiated scene
  468. nd.index = p_node->get_index();
  469. }
  470. // if this node is part of an instantiated scene or sub-instantiated scene
  471. // we need to get the corresponding instance states.
  472. // with the instance states, we can query for identical properties/groups
  473. // and only save what has changed
  474. bool instantiated_by_owner = false;
  475. Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, p_owner, &instantiated_by_owner);
  476. if (!p_node->get_scene_file_path().is_empty() && p_node->get_owner() == p_owner && instantiated_by_owner) {
  477. if (p_node->get_scene_instance_load_placeholder()) {
  478. //it's a placeholder, use the placeholder path
  479. nd.instance = _vm_get_variant(p_node->get_scene_file_path(), variant_map);
  480. nd.instance |= FLAG_INSTANCE_IS_PLACEHOLDER;
  481. } else {
  482. //must instance ourselves
  483. Ref<PackedScene> instance = ResourceLoader::load(p_node->get_scene_file_path());
  484. if (!instance.is_valid()) {
  485. return ERR_CANT_OPEN;
  486. }
  487. nd.instance = _vm_get_variant(instance, variant_map);
  488. }
  489. }
  490. // all setup, we then proceed to check all properties for the node
  491. // and save the ones that are worth saving
  492. List<PropertyInfo> plist;
  493. p_node->get_property_list(&plist);
  494. Array pinned_props = _sanitize_node_pinned_properties(p_node);
  495. Dictionary missing_resource_properties = p_node->get_meta(META_MISSING_RESOURCES, Dictionary());
  496. for (const PropertyInfo &E : plist) {
  497. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  498. continue;
  499. }
  500. if (E.name == META_PROPERTY_MISSING_RESOURCES) {
  501. continue; // Ignore this property when packing.
  502. }
  503. if (E.name.begins_with(META_POINTER_PROPERTY_BASE)) {
  504. continue; // do not save.
  505. }
  506. // If instance or inheriting, not saving if property requested so.
  507. if (!states_stack.is_empty()) {
  508. if ((E.usage & PROPERTY_USAGE_NO_INSTANCE_STATE)) {
  509. continue;
  510. }
  511. }
  512. StringName name = E.name;
  513. Variant value = p_node->get(name);
  514. bool use_deferred_node_path_bit = false;
  515. if (E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_NODE_TYPE) {
  516. value = p_node->get(META_POINTER_PROPERTY_BASE + E.name);
  517. if (value.get_type() != Variant::NODE_PATH) {
  518. continue; //was never set, ignore.
  519. }
  520. use_deferred_node_path_bit = true;
  521. } else if (E.type == Variant::OBJECT && missing_resource_properties.has(E.name)) {
  522. // Was this missing resource overridden? If so do not save the old value.
  523. Ref<Resource> ures = value;
  524. if (ures.is_null()) {
  525. value = missing_resource_properties[E.name];
  526. }
  527. }
  528. if (!pinned_props.has(name)) {
  529. bool is_valid_default = false;
  530. Variant default_value = PropertyUtils::get_property_default_value(p_node, name, &is_valid_default, &states_stack, true);
  531. if (is_valid_default && !PropertyUtils::is_property_value_different(value, default_value)) {
  532. continue;
  533. }
  534. }
  535. NodeData::Property prop;
  536. prop.name = _nm_get_string(name, name_map);
  537. prop.value = _vm_get_variant(value, variant_map);
  538. if (use_deferred_node_path_bit) {
  539. prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
  540. }
  541. nd.properties.push_back(prop);
  542. }
  543. // save the groups this node is into
  544. // discard groups that come from the original scene
  545. List<Node::GroupInfo> groups;
  546. p_node->get_groups(&groups);
  547. for (const Node::GroupInfo &gi : groups) {
  548. if (!gi.persistent) {
  549. continue;
  550. }
  551. bool skip = false;
  552. for (const SceneState::PackState &ia : states_stack) {
  553. //check all levels of pack to see if the group was added somewhere
  554. if (ia.state->is_node_in_group(ia.node, gi.name)) {
  555. skip = true;
  556. break;
  557. }
  558. }
  559. if (skip) {
  560. continue;
  561. }
  562. nd.groups.push_back(_nm_get_string(gi.name, name_map));
  563. }
  564. // save the right owner
  565. // for the saved scene root this is -1
  566. // for nodes of the saved scene this is 0
  567. // for nodes of instantiated scenes this is >0
  568. if (p_node == p_owner) {
  569. //saved scene root
  570. nd.owner = -1;
  571. } else if (p_node->get_owner() == p_owner) {
  572. //part of saved scene
  573. nd.owner = 0;
  574. } else {
  575. nd.owner = -1;
  576. }
  577. MissingNode *missing_node = Object::cast_to<MissingNode>(p_node);
  578. // Save the right type. If this node was created by an instance
  579. // then flag that the node should not be created but reused
  580. if (states_stack.is_empty() && !is_editable_instance) {
  581. //This node is not part of an instantiation process, so save the type.
  582. if (missing_node != nullptr) {
  583. // It's a missing node (type non existent on load).
  584. nd.type = _nm_get_string(missing_node->get_original_class(), name_map);
  585. } else {
  586. nd.type = _nm_get_string(p_node->get_class(), name_map);
  587. }
  588. } else {
  589. // this node is part of an instantiated process, so do not save the type.
  590. // instead, save that it was instantiated
  591. nd.type = TYPE_INSTANTIATED;
  592. }
  593. // determine whether to save this node or not
  594. // if this node is part of an instantiated sub-scene, we can skip storing it if basically
  595. // no properties changed and no groups were added to it.
  596. // below condition is true for all nodes of the scene being saved, and ones in subscenes
  597. // that hold changes
  598. bool save_node = nd.properties.size() || nd.groups.size(); // some local properties or groups exist
  599. save_node = save_node || p_node == p_owner; // owner is always saved
  600. save_node = save_node || (p_node->get_owner() == p_owner && instantiated_by_owner); //part of scene and not instanced
  601. int idx = nodes.size();
  602. int parent_node = NO_PARENT_SAVED;
  603. if (save_node) {
  604. //don't save the node if nothing and subscene
  605. node_map[p_node] = idx;
  606. //ok validate parent node
  607. if (p_parent_idx == NO_PARENT_SAVED) {
  608. int sidx;
  609. if (nodepath_map.has(p_node->get_parent())) {
  610. sidx = nodepath_map[p_node->get_parent()];
  611. } else {
  612. sidx = nodepath_map.size();
  613. nodepath_map[p_node->get_parent()] = sidx;
  614. }
  615. nd.parent = FLAG_ID_IS_PATH | sidx;
  616. } else {
  617. nd.parent = p_parent_idx;
  618. }
  619. parent_node = idx;
  620. nodes.push_back(nd);
  621. }
  622. for (int i = 0; i < p_node->get_child_count(); i++) {
  623. Node *c = p_node->get_child(i);
  624. Error err = _parse_node(p_owner, c, parent_node, name_map, variant_map, node_map, nodepath_map);
  625. if (err) {
  626. return err;
  627. }
  628. }
  629. return OK;
  630. }
  631. Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<StringName, int> &name_map, HashMap<Variant, int, VariantHasher, VariantComparator> &variant_map, HashMap<Node *, int> &node_map, HashMap<Node *, int> &nodepath_map) {
  632. if (p_node != p_owner && p_node->get_owner() && p_node->get_owner() != p_owner && !p_owner->is_editable_instance(p_node->get_owner())) {
  633. return OK;
  634. }
  635. List<MethodInfo> _signals;
  636. p_node->get_signal_list(&_signals);
  637. _signals.sort();
  638. //ERR_FAIL_COND_V( !node_map.has(p_node), ERR_BUG);
  639. //NodeData &nd = nodes[node_map[p_node]];
  640. for (const MethodInfo &E : _signals) {
  641. List<Node::Connection> conns;
  642. p_node->get_signal_connection_list(E.name, &conns);
  643. conns.sort();
  644. for (const Node::Connection &F : conns) {
  645. const Node::Connection &c = F;
  646. if (!(c.flags & CONNECT_PERSIST)) { //only persistent connections get saved
  647. continue;
  648. }
  649. // only connections that originate or end into main saved scene are saved
  650. // everything else is discarded
  651. Node *target = Object::cast_to<Node>(c.callable.get_object());
  652. if (!target) {
  653. continue;
  654. }
  655. Vector<Variant> binds;
  656. int unbinds = 0;
  657. Callable base_callable;
  658. if (c.callable.is_custom()) {
  659. CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(c.callable.get_custom());
  660. if (ccb) {
  661. binds = ccb->get_binds();
  662. base_callable = ccb->get_callable();
  663. }
  664. CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(c.callable.get_custom());
  665. if (ccu) {
  666. unbinds = ccu->get_unbinds();
  667. base_callable = ccu->get_callable();
  668. }
  669. } else {
  670. base_callable = c.callable;
  671. }
  672. //find if this connection already exists
  673. Node *common_parent = target->find_common_parent_with(p_node);
  674. ERR_CONTINUE(!common_parent);
  675. if (common_parent != p_owner && common_parent->get_scene_file_path().is_empty()) {
  676. common_parent = common_parent->get_owner();
  677. }
  678. bool exists = false;
  679. //go through ownership chain to see if this exists
  680. while (common_parent) {
  681. Ref<SceneState> ps;
  682. if (common_parent == p_owner) {
  683. ps = common_parent->get_scene_inherited_state();
  684. } else {
  685. ps = common_parent->get_scene_instance_state();
  686. }
  687. if (ps.is_valid()) {
  688. NodePath signal_from = common_parent->get_path_to(p_node);
  689. NodePath signal_to = common_parent->get_path_to(target);
  690. if (ps->has_connection(signal_from, c.signal.get_name(), signal_to, base_callable.get_method())) {
  691. exists = true;
  692. break;
  693. }
  694. }
  695. if (common_parent == p_owner) {
  696. break;
  697. } else {
  698. common_parent = common_parent->get_owner();
  699. }
  700. }
  701. if (exists) { //already exists (comes from instance or inheritance), so don't save
  702. continue;
  703. }
  704. {
  705. Node *nl = p_node;
  706. bool exists2 = false;
  707. while (nl) {
  708. if (nl == p_owner) {
  709. Ref<SceneState> state = nl->get_scene_inherited_state();
  710. if (state.is_valid()) {
  711. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  712. int to_node = state->find_node_by_path(nl->get_path_to(target));
  713. if (from_node >= 0 && to_node >= 0) {
  714. //this one has state for this node, save
  715. if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
  716. exists2 = true;
  717. break;
  718. }
  719. }
  720. }
  721. nl = nullptr;
  722. } else {
  723. if (!nl->get_scene_file_path().is_empty()) {
  724. //is an instance
  725. Ref<SceneState> state = nl->get_scene_instance_state();
  726. if (state.is_valid()) {
  727. int from_node = state->find_node_by_path(nl->get_path_to(p_node));
  728. int to_node = state->find_node_by_path(nl->get_path_to(target));
  729. if (from_node >= 0 && to_node >= 0) {
  730. //this one has state for this node, save
  731. if (state->is_connection(from_node, c.signal.get_name(), to_node, base_callable.get_method())) {
  732. exists2 = true;
  733. break;
  734. }
  735. }
  736. }
  737. }
  738. nl = nl->get_owner();
  739. }
  740. }
  741. if (exists2) {
  742. continue;
  743. }
  744. }
  745. int src_id;
  746. if (node_map.has(p_node)) {
  747. src_id = node_map[p_node];
  748. } else {
  749. if (nodepath_map.has(p_node)) {
  750. src_id = FLAG_ID_IS_PATH | nodepath_map[p_node];
  751. } else {
  752. int sidx = nodepath_map.size();
  753. nodepath_map[p_node] = sidx;
  754. src_id = FLAG_ID_IS_PATH | sidx;
  755. }
  756. }
  757. int target_id;
  758. if (node_map.has(target)) {
  759. target_id = node_map[target];
  760. } else {
  761. if (nodepath_map.has(target)) {
  762. target_id = FLAG_ID_IS_PATH | nodepath_map[target];
  763. } else {
  764. int sidx = nodepath_map.size();
  765. nodepath_map[target] = sidx;
  766. target_id = FLAG_ID_IS_PATH | sidx;
  767. }
  768. }
  769. ConnectionData cd;
  770. cd.from = src_id;
  771. cd.to = target_id;
  772. cd.method = _nm_get_string(base_callable.get_method(), name_map);
  773. cd.signal = _nm_get_string(c.signal.get_name(), name_map);
  774. cd.flags = c.flags;
  775. cd.unbinds = unbinds;
  776. for (int i = 0; i < binds.size(); i++) {
  777. cd.binds.push_back(_vm_get_variant(binds[i], variant_map));
  778. }
  779. connections.push_back(cd);
  780. }
  781. }
  782. for (int i = 0; i < p_node->get_child_count(); i++) {
  783. Node *c = p_node->get_child(i);
  784. Error err = _parse_connections(p_owner, c, name_map, variant_map, node_map, nodepath_map);
  785. if (err) {
  786. return err;
  787. }
  788. }
  789. return OK;
  790. }
  791. Error SceneState::pack(Node *p_scene) {
  792. ERR_FAIL_NULL_V(p_scene, ERR_INVALID_PARAMETER);
  793. clear();
  794. Node *scene = p_scene;
  795. HashMap<StringName, int> name_map;
  796. HashMap<Variant, int, VariantHasher, VariantComparator> variant_map;
  797. HashMap<Node *, int> node_map;
  798. HashMap<Node *, int> nodepath_map;
  799. // If using scene inheritance, pack the scene it inherits from.
  800. if (scene->get_scene_inherited_state().is_valid()) {
  801. String scene_path = scene->get_scene_inherited_state()->get_path();
  802. Ref<PackedScene> instance = ResourceLoader::load(scene_path);
  803. if (instance.is_valid()) {
  804. base_scene_idx = _vm_get_variant(instance, variant_map);
  805. }
  806. }
  807. // Instanced, only direct sub-scenes are supported of course.
  808. Error err = _parse_node(scene, scene, -1, name_map, variant_map, node_map, nodepath_map);
  809. if (err) {
  810. clear();
  811. ERR_FAIL_V(err);
  812. }
  813. err = _parse_connections(scene, scene, name_map, variant_map, node_map, nodepath_map);
  814. if (err) {
  815. clear();
  816. ERR_FAIL_V(err);
  817. }
  818. names.resize(name_map.size());
  819. for (const KeyValue<StringName, int> &E : name_map) {
  820. names.write[E.value] = E.key;
  821. }
  822. variants.resize(variant_map.size());
  823. for (const KeyValue<Variant, int> &E : variant_map) {
  824. int idx = E.value;
  825. variants.write[idx] = E.key;
  826. }
  827. node_paths.resize(nodepath_map.size());
  828. for (const KeyValue<Node *, int> &E : nodepath_map) {
  829. node_paths.write[E.value] = scene->get_path_to(E.key);
  830. }
  831. if (Engine::get_singleton()->is_editor_hint()) {
  832. // Build node path cache
  833. for (const KeyValue<Node *, int> &E : node_map) {
  834. node_path_cache[scene->get_path_to(E.key)] = E.value;
  835. }
  836. }
  837. return OK;
  838. }
  839. void SceneState::set_path(const String &p_path) {
  840. path = p_path;
  841. }
  842. String SceneState::get_path() const {
  843. return path;
  844. }
  845. void SceneState::clear() {
  846. names.clear();
  847. variants.clear();
  848. nodes.clear();
  849. connections.clear();
  850. node_path_cache.clear();
  851. node_paths.clear();
  852. editable_instances.clear();
  853. base_scene_idx = -1;
  854. }
  855. Error SceneState::copy_from(const Ref<SceneState> &p_scene_state) {
  856. ERR_FAIL_COND_V(p_scene_state.is_null(), ERR_INVALID_PARAMETER);
  857. clear();
  858. for (const StringName &E : p_scene_state->names) {
  859. names.append(E);
  860. }
  861. for (const Variant &E : p_scene_state->variants) {
  862. variants.append(E);
  863. }
  864. for (const SceneState::NodeData &E : p_scene_state->nodes) {
  865. nodes.append(E);
  866. }
  867. for (const SceneState::ConnectionData &E : p_scene_state->connections) {
  868. connections.append(E);
  869. }
  870. for (KeyValue<NodePath, int> &E : p_scene_state->node_path_cache) {
  871. node_path_cache.insert(E.key, E.value);
  872. }
  873. for (const NodePath &E : p_scene_state->node_paths) {
  874. node_paths.append(E);
  875. }
  876. for (const NodePath &E : p_scene_state->editable_instances) {
  877. editable_instances.append(E);
  878. }
  879. base_scene_idx = p_scene_state->base_scene_idx;
  880. return OK;
  881. }
  882. Ref<SceneState> SceneState::get_base_scene_state() const {
  883. if (base_scene_idx >= 0) {
  884. Ref<PackedScene> ps = variants[base_scene_idx];
  885. if (ps.is_valid()) {
  886. return ps->get_state();
  887. }
  888. }
  889. return Ref<SceneState>();
  890. }
  891. void SceneState::update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene) {
  892. ERR_FAIL_COND(p_packed_scene.is_null());
  893. for (const NodeData &nd : nodes) {
  894. if (nd.instance >= 0) {
  895. if (!(nd.instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
  896. int instance_id = nd.instance & FLAG_MASK;
  897. Ref<PackedScene> original_packed_scene = variants[instance_id];
  898. if (original_packed_scene.is_valid()) {
  899. if (original_packed_scene->get_path() == p_path) {
  900. variants.remove_at(instance_id);
  901. variants.insert(instance_id, p_packed_scene);
  902. }
  903. }
  904. }
  905. }
  906. }
  907. }
  908. int SceneState::find_node_by_path(const NodePath &p_node) const {
  909. ERR_FAIL_COND_V_MSG(node_path_cache.size() == 0, -1, "This operation requires the node cache to have been built.");
  910. if (!node_path_cache.has(p_node)) {
  911. if (get_base_scene_state().is_valid()) {
  912. int idx = get_base_scene_state()->find_node_by_path(p_node);
  913. if (idx != -1) {
  914. int rkey = _find_base_scene_node_remap_key(idx);
  915. if (rkey == -1) {
  916. rkey = nodes.size() + base_scene_node_remap.size();
  917. base_scene_node_remap[rkey] = idx;
  918. }
  919. return rkey;
  920. }
  921. }
  922. return -1;
  923. }
  924. int nid = node_path_cache[p_node];
  925. if (get_base_scene_state().is_valid() && !base_scene_node_remap.has(nid)) {
  926. //for nodes that _do_ exist in current scene, still try to look for
  927. //the node in the instantiated scene, as a property may be missing
  928. //from the local one
  929. int idx = get_base_scene_state()->find_node_by_path(p_node);
  930. if (idx != -1) {
  931. base_scene_node_remap[nid] = idx;
  932. }
  933. }
  934. return nid;
  935. }
  936. int SceneState::_find_base_scene_node_remap_key(int p_idx) const {
  937. for (const KeyValue<int, int> &E : base_scene_node_remap) {
  938. if (E.value == p_idx) {
  939. return E.key;
  940. }
  941. }
  942. return -1;
  943. }
  944. Variant SceneState::get_property_value(int p_node, const StringName &p_property, bool &found) const {
  945. found = false;
  946. ERR_FAIL_COND_V(p_node < 0, Variant());
  947. if (p_node < nodes.size()) {
  948. //find in built-in nodes
  949. int pc = nodes[p_node].properties.size();
  950. const StringName *namep = names.ptr();
  951. const NodeData::Property *p = nodes[p_node].properties.ptr();
  952. for (int i = 0; i < pc; i++) {
  953. if (p_property == namep[p[i].name & FLAG_PROP_NAME_MASK]) {
  954. found = true;
  955. return variants[p[i].value];
  956. }
  957. }
  958. }
  959. //property not found, try on instance
  960. if (base_scene_node_remap.has(p_node)) {
  961. return get_base_scene_state()->get_property_value(base_scene_node_remap[p_node], p_property, found);
  962. }
  963. return Variant();
  964. }
  965. bool SceneState::is_node_in_group(int p_node, const StringName &p_group) const {
  966. ERR_FAIL_COND_V(p_node < 0, false);
  967. if (p_node < nodes.size()) {
  968. const StringName *namep = names.ptr();
  969. for (int i = 0; i < nodes[p_node].groups.size(); i++) {
  970. if (namep[nodes[p_node].groups[i]] == p_group) {
  971. return true;
  972. }
  973. }
  974. }
  975. if (base_scene_node_remap.has(p_node)) {
  976. return get_base_scene_state()->is_node_in_group(base_scene_node_remap[p_node], p_group);
  977. }
  978. return false;
  979. }
  980. bool SceneState::disable_placeholders = false;
  981. void SceneState::set_disable_placeholders(bool p_disable) {
  982. disable_placeholders = p_disable;
  983. }
  984. bool SceneState::is_connection(int p_node, const StringName &p_signal, int p_to_node, const StringName &p_to_method) const {
  985. ERR_FAIL_COND_V(p_node < 0, false);
  986. ERR_FAIL_COND_V(p_to_node < 0, false);
  987. if (p_node < nodes.size() && p_to_node < nodes.size()) {
  988. int signal_idx = -1;
  989. int method_idx = -1;
  990. for (int i = 0; i < names.size(); i++) {
  991. if (names[i] == p_signal) {
  992. signal_idx = i;
  993. } else if (names[i] == p_to_method) {
  994. method_idx = i;
  995. }
  996. }
  997. if (signal_idx >= 0 && method_idx >= 0) {
  998. //signal and method strings are stored..
  999. for (int i = 0; i < connections.size(); i++) {
  1000. if (connections[i].from == p_node && connections[i].to == p_to_node && connections[i].signal == signal_idx && connections[i].method == method_idx) {
  1001. return true;
  1002. }
  1003. }
  1004. }
  1005. }
  1006. if (base_scene_node_remap.has(p_node) && base_scene_node_remap.has(p_to_node)) {
  1007. return get_base_scene_state()->is_connection(base_scene_node_remap[p_node], p_signal, base_scene_node_remap[p_to_node], p_to_method);
  1008. }
  1009. return false;
  1010. }
  1011. void SceneState::set_bundled_scene(const Dictionary &p_dictionary) {
  1012. ERR_FAIL_COND(!p_dictionary.has("names"));
  1013. ERR_FAIL_COND(!p_dictionary.has("variants"));
  1014. ERR_FAIL_COND(!p_dictionary.has("node_count"));
  1015. ERR_FAIL_COND(!p_dictionary.has("nodes"));
  1016. ERR_FAIL_COND(!p_dictionary.has("conn_count"));
  1017. ERR_FAIL_COND(!p_dictionary.has("conns"));
  1018. //ERR_FAIL_COND( !p_dictionary.has("path"));
  1019. int version = 1;
  1020. if (p_dictionary.has("version")) {
  1021. version = p_dictionary["version"];
  1022. }
  1023. ERR_FAIL_COND_MSG(version > PACKED_SCENE_VERSION, "Save format version too new.");
  1024. const int node_count = p_dictionary["node_count"];
  1025. const Vector<int> snodes = p_dictionary["nodes"];
  1026. ERR_FAIL_COND(snodes.size() < node_count);
  1027. const int conn_count = p_dictionary["conn_count"];
  1028. const Vector<int> sconns = p_dictionary["conns"];
  1029. ERR_FAIL_COND(sconns.size() < conn_count);
  1030. Vector<String> snames = p_dictionary["names"];
  1031. if (snames.size()) {
  1032. int namecount = snames.size();
  1033. names.resize(namecount);
  1034. const String *r = snames.ptr();
  1035. for (int i = 0; i < names.size(); i++) {
  1036. names.write[i] = r[i];
  1037. }
  1038. }
  1039. Array svariants = p_dictionary["variants"];
  1040. if (svariants.size()) {
  1041. int varcount = svariants.size();
  1042. variants.resize(varcount);
  1043. for (int i = 0; i < varcount; i++) {
  1044. variants.write[i] = svariants[i];
  1045. }
  1046. } else {
  1047. variants.clear();
  1048. }
  1049. nodes.resize(node_count);
  1050. if (node_count) {
  1051. const int *r = snodes.ptr();
  1052. int idx = 0;
  1053. for (int i = 0; i < node_count; i++) {
  1054. NodeData &nd = nodes.write[i];
  1055. nd.parent = r[idx++];
  1056. nd.owner = r[idx++];
  1057. nd.type = r[idx++];
  1058. uint32_t name_index = r[idx++];
  1059. nd.name = name_index & ((1 << NAME_INDEX_BITS) - 1);
  1060. nd.index = (name_index >> NAME_INDEX_BITS);
  1061. nd.index--; //0 is invalid, stored as 1
  1062. nd.instance = r[idx++];
  1063. nd.properties.resize(r[idx++]);
  1064. for (int j = 0; j < nd.properties.size(); j++) {
  1065. nd.properties.write[j].name = r[idx++];
  1066. nd.properties.write[j].value = r[idx++];
  1067. }
  1068. nd.groups.resize(r[idx++]);
  1069. for (int j = 0; j < nd.groups.size(); j++) {
  1070. nd.groups.write[j] = r[idx++];
  1071. }
  1072. }
  1073. }
  1074. connections.resize(conn_count);
  1075. if (conn_count) {
  1076. const int *r = sconns.ptr();
  1077. int idx = 0;
  1078. for (int i = 0; i < conn_count; i++) {
  1079. ConnectionData &cd = connections.write[i];
  1080. cd.from = r[idx++];
  1081. cd.to = r[idx++];
  1082. cd.signal = r[idx++];
  1083. cd.method = r[idx++];
  1084. cd.flags = r[idx++];
  1085. cd.binds.resize(r[idx++]);
  1086. for (int j = 0; j < cd.binds.size(); j++) {
  1087. cd.binds.write[j] = r[idx++];
  1088. }
  1089. if (version >= 3) {
  1090. cd.unbinds = r[idx++];
  1091. }
  1092. }
  1093. }
  1094. Array np;
  1095. if (p_dictionary.has("node_paths")) {
  1096. np = p_dictionary["node_paths"];
  1097. }
  1098. node_paths.resize(np.size());
  1099. for (int i = 0; i < np.size(); i++) {
  1100. node_paths.write[i] = np[i];
  1101. }
  1102. Array ei;
  1103. if (p_dictionary.has("editable_instances")) {
  1104. ei = p_dictionary["editable_instances"];
  1105. }
  1106. if (p_dictionary.has("base_scene")) {
  1107. base_scene_idx = p_dictionary["base_scene"];
  1108. }
  1109. editable_instances.resize(ei.size());
  1110. for (int i = 0; i < editable_instances.size(); i++) {
  1111. editable_instances.write[i] = ei[i];
  1112. }
  1113. //path=p_dictionary["path"];
  1114. }
  1115. Dictionary SceneState::get_bundled_scene() const {
  1116. Vector<String> rnames;
  1117. rnames.resize(names.size());
  1118. if (names.size()) {
  1119. String *r = rnames.ptrw();
  1120. for (int i = 0; i < names.size(); i++) {
  1121. r[i] = names[i];
  1122. }
  1123. }
  1124. Dictionary d;
  1125. d["names"] = rnames;
  1126. d["variants"] = variants;
  1127. Vector<int> rnodes;
  1128. d["node_count"] = nodes.size();
  1129. for (int i = 0; i < nodes.size(); i++) {
  1130. const NodeData &nd = nodes[i];
  1131. rnodes.push_back(nd.parent);
  1132. rnodes.push_back(nd.owner);
  1133. rnodes.push_back(nd.type);
  1134. uint32_t name_index = nd.name;
  1135. if (nd.index < (1 << (32 - NAME_INDEX_BITS)) - 1) { //save if less than 16k children
  1136. name_index |= uint32_t(nd.index + 1) << NAME_INDEX_BITS; //for backwards compatibility, index 0 is no index
  1137. }
  1138. rnodes.push_back(name_index);
  1139. rnodes.push_back(nd.instance);
  1140. rnodes.push_back(nd.properties.size());
  1141. for (int j = 0; j < nd.properties.size(); j++) {
  1142. rnodes.push_back(nd.properties[j].name);
  1143. rnodes.push_back(nd.properties[j].value);
  1144. }
  1145. rnodes.push_back(nd.groups.size());
  1146. for (int j = 0; j < nd.groups.size(); j++) {
  1147. rnodes.push_back(nd.groups[j]);
  1148. }
  1149. }
  1150. d["nodes"] = rnodes;
  1151. Vector<int> rconns;
  1152. d["conn_count"] = connections.size();
  1153. for (int i = 0; i < connections.size(); i++) {
  1154. const ConnectionData &cd = connections[i];
  1155. rconns.push_back(cd.from);
  1156. rconns.push_back(cd.to);
  1157. rconns.push_back(cd.signal);
  1158. rconns.push_back(cd.method);
  1159. rconns.push_back(cd.flags);
  1160. rconns.push_back(cd.binds.size());
  1161. for (int j = 0; j < cd.binds.size(); j++) {
  1162. rconns.push_back(cd.binds[j]);
  1163. }
  1164. rconns.push_back(cd.unbinds);
  1165. }
  1166. d["conns"] = rconns;
  1167. Array rnode_paths;
  1168. rnode_paths.resize(node_paths.size());
  1169. for (int i = 0; i < node_paths.size(); i++) {
  1170. rnode_paths[i] = node_paths[i];
  1171. }
  1172. d["node_paths"] = rnode_paths;
  1173. Array reditable_instances;
  1174. reditable_instances.resize(editable_instances.size());
  1175. for (int i = 0; i < editable_instances.size(); i++) {
  1176. reditable_instances[i] = editable_instances[i];
  1177. }
  1178. d["editable_instances"] = reditable_instances;
  1179. if (base_scene_idx >= 0) {
  1180. d["base_scene"] = base_scene_idx;
  1181. }
  1182. d["version"] = PACKED_SCENE_VERSION;
  1183. return d;
  1184. }
  1185. int SceneState::get_node_count() const {
  1186. return nodes.size();
  1187. }
  1188. StringName SceneState::get_node_type(int p_idx) const {
  1189. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1190. if (nodes[p_idx].type == TYPE_INSTANTIATED) {
  1191. return StringName();
  1192. }
  1193. return names[nodes[p_idx].type];
  1194. }
  1195. StringName SceneState::get_node_name(int p_idx) const {
  1196. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1197. return names[nodes[p_idx].name];
  1198. }
  1199. int SceneState::get_node_index(int p_idx) const {
  1200. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1201. return nodes[p_idx].index;
  1202. }
  1203. bool SceneState::is_node_instance_placeholder(int p_idx) const {
  1204. ERR_FAIL_INDEX_V(p_idx, nodes.size(), false);
  1205. return nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER);
  1206. }
  1207. Ref<PackedScene> SceneState::get_node_instance(int p_idx) const {
  1208. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Ref<PackedScene>());
  1209. if (nodes[p_idx].instance >= 0) {
  1210. if (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER) {
  1211. return Ref<PackedScene>();
  1212. } else {
  1213. return variants[nodes[p_idx].instance & FLAG_MASK];
  1214. }
  1215. } else if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1216. if (base_scene_idx >= 0) {
  1217. return variants[base_scene_idx];
  1218. }
  1219. }
  1220. return Ref<PackedScene>();
  1221. }
  1222. String SceneState::get_node_instance_placeholder(int p_idx) const {
  1223. ERR_FAIL_INDEX_V(p_idx, nodes.size(), String());
  1224. if (nodes[p_idx].instance >= 0 && (nodes[p_idx].instance & FLAG_INSTANCE_IS_PLACEHOLDER)) {
  1225. return variants[nodes[p_idx].instance & FLAG_MASK];
  1226. }
  1227. return String();
  1228. }
  1229. Vector<StringName> SceneState::get_node_groups(int p_idx) const {
  1230. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Vector<StringName>());
  1231. Vector<StringName> groups;
  1232. for (int i = 0; i < nodes[p_idx].groups.size(); i++) {
  1233. groups.push_back(names[nodes[p_idx].groups[i]]);
  1234. }
  1235. return groups;
  1236. }
  1237. NodePath SceneState::get_node_path(int p_idx, bool p_for_parent) const {
  1238. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1239. if (nodes[p_idx].parent < 0 || nodes[p_idx].parent == NO_PARENT_SAVED) {
  1240. if (p_for_parent) {
  1241. return NodePath();
  1242. } else {
  1243. return NodePath(".");
  1244. }
  1245. }
  1246. Vector<StringName> sub_path;
  1247. NodePath base_path;
  1248. int nidx = p_idx;
  1249. while (true) {
  1250. if (nodes[nidx].parent == NO_PARENT_SAVED || nodes[nidx].parent < 0) {
  1251. sub_path.insert(0, ".");
  1252. break;
  1253. }
  1254. if (!p_for_parent || p_idx != nidx) {
  1255. sub_path.insert(0, names[nodes[nidx].name]);
  1256. }
  1257. if (nodes[nidx].parent & FLAG_ID_IS_PATH) {
  1258. base_path = node_paths[nodes[nidx].parent & FLAG_MASK];
  1259. break;
  1260. } else {
  1261. nidx = nodes[nidx].parent & FLAG_MASK;
  1262. }
  1263. }
  1264. for (int i = base_path.get_name_count() - 1; i >= 0; i--) {
  1265. sub_path.insert(0, base_path.get_name(i));
  1266. }
  1267. if (sub_path.is_empty()) {
  1268. return NodePath(".");
  1269. }
  1270. return NodePath(sub_path, false);
  1271. }
  1272. int SceneState::get_node_property_count(int p_idx) const {
  1273. ERR_FAIL_INDEX_V(p_idx, nodes.size(), -1);
  1274. return nodes[p_idx].properties.size();
  1275. }
  1276. StringName SceneState::get_node_property_name(int p_idx, int p_prop) const {
  1277. ERR_FAIL_INDEX_V(p_idx, nodes.size(), StringName());
  1278. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), StringName());
  1279. return names[nodes[p_idx].properties[p_prop].name & FLAG_PROP_NAME_MASK];
  1280. }
  1281. Vector<String> SceneState::get_node_deferred_nodepath_properties(int p_idx) const {
  1282. Vector<String> ret;
  1283. ERR_FAIL_INDEX_V(p_idx, nodes.size(), ret);
  1284. for (int i = 0; i < nodes[p_idx].properties.size(); i++) {
  1285. uint32_t idx = nodes[p_idx].properties[i].name;
  1286. if (idx & FLAG_PATH_PROPERTY_IS_NODE) {
  1287. ret.push_back(names[idx & FLAG_PROP_NAME_MASK]);
  1288. }
  1289. }
  1290. return ret;
  1291. }
  1292. Variant SceneState::get_node_property_value(int p_idx, int p_prop) const {
  1293. ERR_FAIL_INDEX_V(p_idx, nodes.size(), Variant());
  1294. ERR_FAIL_INDEX_V(p_prop, nodes[p_idx].properties.size(), Variant());
  1295. return variants[nodes[p_idx].properties[p_prop].value];
  1296. }
  1297. NodePath SceneState::get_node_owner_path(int p_idx) const {
  1298. ERR_FAIL_INDEX_V(p_idx, nodes.size(), NodePath());
  1299. if (nodes[p_idx].owner < 0 || nodes[p_idx].owner == NO_PARENT_SAVED) {
  1300. return NodePath(); //root likely
  1301. }
  1302. if (nodes[p_idx].owner & FLAG_ID_IS_PATH) {
  1303. return node_paths[nodes[p_idx].owner & FLAG_MASK];
  1304. } else {
  1305. return get_node_path(nodes[p_idx].owner & FLAG_MASK);
  1306. }
  1307. }
  1308. int SceneState::get_connection_count() const {
  1309. return connections.size();
  1310. }
  1311. NodePath SceneState::get_connection_source(int p_idx) const {
  1312. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1313. if (connections[p_idx].from & FLAG_ID_IS_PATH) {
  1314. return node_paths[connections[p_idx].from & FLAG_MASK];
  1315. } else {
  1316. return get_node_path(connections[p_idx].from & FLAG_MASK);
  1317. }
  1318. }
  1319. StringName SceneState::get_connection_signal(int p_idx) const {
  1320. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1321. return names[connections[p_idx].signal];
  1322. }
  1323. NodePath SceneState::get_connection_target(int p_idx) const {
  1324. ERR_FAIL_INDEX_V(p_idx, connections.size(), NodePath());
  1325. if (connections[p_idx].to & FLAG_ID_IS_PATH) {
  1326. return node_paths[connections[p_idx].to & FLAG_MASK];
  1327. } else {
  1328. return get_node_path(connections[p_idx].to & FLAG_MASK);
  1329. }
  1330. }
  1331. StringName SceneState::get_connection_method(int p_idx) const {
  1332. ERR_FAIL_INDEX_V(p_idx, connections.size(), StringName());
  1333. return names[connections[p_idx].method];
  1334. }
  1335. int SceneState::get_connection_flags(int p_idx) const {
  1336. ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
  1337. return connections[p_idx].flags;
  1338. }
  1339. int SceneState::get_connection_unbinds(int p_idx) const {
  1340. ERR_FAIL_INDEX_V(p_idx, connections.size(), -1);
  1341. return connections[p_idx].unbinds;
  1342. }
  1343. Array SceneState::get_connection_binds(int p_idx) const {
  1344. ERR_FAIL_INDEX_V(p_idx, connections.size(), Array());
  1345. Array binds;
  1346. for (int i = 0; i < connections[p_idx].binds.size(); i++) {
  1347. binds.push_back(variants[connections[p_idx].binds[i]]);
  1348. }
  1349. return binds;
  1350. }
  1351. bool SceneState::has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance) {
  1352. // this method cannot be const because of this
  1353. Ref<SceneState> ss = this;
  1354. do {
  1355. for (int i = 0; i < ss->connections.size(); i++) {
  1356. const ConnectionData &c = ss->connections[i];
  1357. NodePath np_from;
  1358. if (c.from & FLAG_ID_IS_PATH) {
  1359. np_from = ss->node_paths[c.from & FLAG_MASK];
  1360. } else {
  1361. np_from = ss->get_node_path(c.from);
  1362. }
  1363. NodePath np_to;
  1364. if (c.to & FLAG_ID_IS_PATH) {
  1365. np_to = ss->node_paths[c.to & FLAG_MASK];
  1366. } else {
  1367. np_to = ss->get_node_path(c.to);
  1368. }
  1369. StringName sn_signal = ss->names[c.signal];
  1370. StringName sn_method = ss->names[c.method];
  1371. if (np_from == p_node_from && sn_signal == p_signal && np_to == p_node_to && sn_method == p_method) {
  1372. return true;
  1373. }
  1374. }
  1375. if (p_no_inheritance) {
  1376. break;
  1377. }
  1378. ss = ss->get_base_scene_state();
  1379. } while (ss.is_valid());
  1380. return false;
  1381. }
  1382. Vector<NodePath> SceneState::get_editable_instances() const {
  1383. return editable_instances;
  1384. }
  1385. //add
  1386. int SceneState::add_name(const StringName &p_name) {
  1387. names.push_back(p_name);
  1388. return names.size() - 1;
  1389. }
  1390. int SceneState::add_value(const Variant &p_value) {
  1391. variants.push_back(p_value);
  1392. return variants.size() - 1;
  1393. }
  1394. int SceneState::add_node_path(const NodePath &p_path) {
  1395. node_paths.push_back(p_path);
  1396. return (node_paths.size() - 1) | FLAG_ID_IS_PATH;
  1397. }
  1398. int SceneState::add_node(int p_parent, int p_owner, int p_type, int p_name, int p_instance, int p_index) {
  1399. NodeData nd;
  1400. nd.parent = p_parent;
  1401. nd.owner = p_owner;
  1402. nd.type = p_type;
  1403. nd.name = p_name;
  1404. nd.instance = p_instance;
  1405. nd.index = p_index;
  1406. nodes.push_back(nd);
  1407. return nodes.size() - 1;
  1408. }
  1409. void SceneState::add_node_property(int p_node, int p_name, int p_value, bool p_deferred_node_path) {
  1410. ERR_FAIL_INDEX(p_node, nodes.size());
  1411. ERR_FAIL_INDEX(p_name, names.size());
  1412. ERR_FAIL_INDEX(p_value, variants.size());
  1413. NodeData::Property prop;
  1414. prop.name = p_name;
  1415. if (p_deferred_node_path) {
  1416. prop.name |= FLAG_PATH_PROPERTY_IS_NODE;
  1417. }
  1418. prop.value = p_value;
  1419. nodes.write[p_node].properties.push_back(prop);
  1420. }
  1421. void SceneState::add_node_group(int p_node, int p_group) {
  1422. ERR_FAIL_INDEX(p_node, nodes.size());
  1423. ERR_FAIL_INDEX(p_group, names.size());
  1424. nodes.write[p_node].groups.push_back(p_group);
  1425. }
  1426. void SceneState::set_base_scene(int p_idx) {
  1427. ERR_FAIL_INDEX(p_idx, variants.size());
  1428. base_scene_idx = p_idx;
  1429. }
  1430. void SceneState::add_connection(int p_from, int p_to, int p_signal, int p_method, int p_flags, int p_unbinds, const Vector<int> &p_binds) {
  1431. ERR_FAIL_INDEX(p_signal, names.size());
  1432. ERR_FAIL_INDEX(p_method, names.size());
  1433. for (int i = 0; i < p_binds.size(); i++) {
  1434. ERR_FAIL_INDEX(p_binds[i], variants.size());
  1435. }
  1436. ConnectionData c;
  1437. c.from = p_from;
  1438. c.to = p_to;
  1439. c.signal = p_signal;
  1440. c.method = p_method;
  1441. c.flags = p_flags;
  1442. c.unbinds = p_unbinds;
  1443. c.binds = p_binds;
  1444. connections.push_back(c);
  1445. }
  1446. void SceneState::add_editable_instance(const NodePath &p_path) {
  1447. editable_instances.push_back(p_path);
  1448. }
  1449. String SceneState::get_meta_pointer_property(const String &p_property) {
  1450. return META_POINTER_PROPERTY_BASE + p_property;
  1451. }
  1452. Vector<String> SceneState::_get_node_groups(int p_idx) const {
  1453. Vector<StringName> groups = get_node_groups(p_idx);
  1454. Vector<String> ret;
  1455. for (int i = 0; i < groups.size(); i++) {
  1456. ret.push_back(groups[i]);
  1457. }
  1458. return ret;
  1459. }
  1460. void SceneState::_bind_methods() {
  1461. //unbuild API
  1462. ClassDB::bind_method(D_METHOD("get_node_count"), &SceneState::get_node_count);
  1463. ClassDB::bind_method(D_METHOD("get_node_type", "idx"), &SceneState::get_node_type);
  1464. ClassDB::bind_method(D_METHOD("get_node_name", "idx"), &SceneState::get_node_name);
  1465. ClassDB::bind_method(D_METHOD("get_node_path", "idx", "for_parent"), &SceneState::get_node_path, DEFVAL(false));
  1466. ClassDB::bind_method(D_METHOD("get_node_owner_path", "idx"), &SceneState::get_node_owner_path);
  1467. ClassDB::bind_method(D_METHOD("is_node_instance_placeholder", "idx"), &SceneState::is_node_instance_placeholder);
  1468. ClassDB::bind_method(D_METHOD("get_node_instance_placeholder", "idx"), &SceneState::get_node_instance_placeholder);
  1469. ClassDB::bind_method(D_METHOD("get_node_instance", "idx"), &SceneState::get_node_instance);
  1470. ClassDB::bind_method(D_METHOD("get_node_groups", "idx"), &SceneState::_get_node_groups);
  1471. ClassDB::bind_method(D_METHOD("get_node_index", "idx"), &SceneState::get_node_index);
  1472. ClassDB::bind_method(D_METHOD("get_node_property_count", "idx"), &SceneState::get_node_property_count);
  1473. ClassDB::bind_method(D_METHOD("get_node_property_name", "idx", "prop_idx"), &SceneState::get_node_property_name);
  1474. ClassDB::bind_method(D_METHOD("get_node_property_value", "idx", "prop_idx"), &SceneState::get_node_property_value);
  1475. ClassDB::bind_method(D_METHOD("get_connection_count"), &SceneState::get_connection_count);
  1476. ClassDB::bind_method(D_METHOD("get_connection_source", "idx"), &SceneState::get_connection_source);
  1477. ClassDB::bind_method(D_METHOD("get_connection_signal", "idx"), &SceneState::get_connection_signal);
  1478. ClassDB::bind_method(D_METHOD("get_connection_target", "idx"), &SceneState::get_connection_target);
  1479. ClassDB::bind_method(D_METHOD("get_connection_method", "idx"), &SceneState::get_connection_method);
  1480. ClassDB::bind_method(D_METHOD("get_connection_flags", "idx"), &SceneState::get_connection_flags);
  1481. ClassDB::bind_method(D_METHOD("get_connection_binds", "idx"), &SceneState::get_connection_binds);
  1482. ClassDB::bind_method(D_METHOD("get_connection_unbinds", "idx"), &SceneState::get_connection_unbinds);
  1483. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1484. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1485. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1486. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1487. }
  1488. SceneState::SceneState() {
  1489. }
  1490. ////////////////
  1491. void PackedScene::_set_bundled_scene(const Dictionary &p_scene) {
  1492. state->set_bundled_scene(p_scene);
  1493. }
  1494. Dictionary PackedScene::_get_bundled_scene() const {
  1495. return state->get_bundled_scene();
  1496. }
  1497. Error PackedScene::pack(Node *p_scene) {
  1498. return state->pack(p_scene);
  1499. }
  1500. void PackedScene::clear() {
  1501. state->clear();
  1502. }
  1503. void PackedScene::reload_from_file() {
  1504. String path = get_path();
  1505. if (!path.is_resource_file()) {
  1506. return;
  1507. }
  1508. Ref<PackedScene> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  1509. if (!s.is_valid()) {
  1510. return;
  1511. }
  1512. // Backup the loaded_state
  1513. Ref<SceneState> loaded_state = s->get_state();
  1514. // This assigns a new state to s->state
  1515. // We do this because of the next step
  1516. s->recreate_state();
  1517. // This has a side-effect to clear s->state
  1518. copy_from(s);
  1519. // Then, we copy the backed-up loaded_state to state
  1520. state->copy_from(loaded_state);
  1521. }
  1522. bool PackedScene::can_instantiate() const {
  1523. return state->can_instantiate();
  1524. }
  1525. Node *PackedScene::instantiate(GenEditState p_edit_state) const {
  1526. #ifndef TOOLS_ENABLED
  1527. ERR_FAIL_COND_V_MSG(p_edit_state != GEN_EDIT_STATE_DISABLED, nullptr, "Edit state is only for editors, does not work without tools compiled.");
  1528. #endif
  1529. Node *s = state->instantiate((SceneState::GenEditState)p_edit_state);
  1530. if (!s) {
  1531. return nullptr;
  1532. }
  1533. if (p_edit_state != GEN_EDIT_STATE_DISABLED) {
  1534. s->set_scene_instance_state(state);
  1535. }
  1536. if (!is_built_in()) {
  1537. s->set_scene_file_path(get_path());
  1538. }
  1539. s->notification(Node::NOTIFICATION_SCENE_INSTANTIATED);
  1540. return s;
  1541. }
  1542. void PackedScene::replace_state(Ref<SceneState> p_by) {
  1543. state = p_by;
  1544. state->set_path(get_path());
  1545. #ifdef TOOLS_ENABLED
  1546. state->set_last_modified_time(get_last_modified_time());
  1547. #endif
  1548. }
  1549. void PackedScene::recreate_state() {
  1550. state = Ref<SceneState>(memnew(SceneState));
  1551. state->set_path(get_path());
  1552. #ifdef TOOLS_ENABLED
  1553. state->set_last_modified_time(get_last_modified_time());
  1554. #endif
  1555. }
  1556. Ref<SceneState> PackedScene::get_state() const {
  1557. return state;
  1558. }
  1559. void PackedScene::set_path(const String &p_path, bool p_take_over) {
  1560. state->set_path(p_path);
  1561. Resource::set_path(p_path, p_take_over);
  1562. }
  1563. void PackedScene::reset_state() {
  1564. clear();
  1565. }
  1566. void PackedScene::_bind_methods() {
  1567. ClassDB::bind_method(D_METHOD("pack", "path"), &PackedScene::pack);
  1568. ClassDB::bind_method(D_METHOD("instantiate", "edit_state"), &PackedScene::instantiate, DEFVAL(GEN_EDIT_STATE_DISABLED));
  1569. ClassDB::bind_method(D_METHOD("can_instantiate"), &PackedScene::can_instantiate);
  1570. ClassDB::bind_method(D_METHOD("_set_bundled_scene", "scene"), &PackedScene::_set_bundled_scene);
  1571. ClassDB::bind_method(D_METHOD("_get_bundled_scene"), &PackedScene::_get_bundled_scene);
  1572. ClassDB::bind_method(D_METHOD("get_state"), &PackedScene::get_state);
  1573. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled"), "_set_bundled_scene", "_get_bundled_scene");
  1574. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_DISABLED);
  1575. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_INSTANCE);
  1576. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN);
  1577. BIND_ENUM_CONSTANT(GEN_EDIT_STATE_MAIN_INHERITED);
  1578. }
  1579. PackedScene::PackedScene() {
  1580. state = Ref<SceneState>(memnew(SceneState));
  1581. }