From a74138a0dc5862d2c26eb78a141ba3c3f9d01c6d Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sat, 23 Jan 2016 21:42:15 -0300 Subject: [PATCH] -Some changes to how scenes and scripts are overriden in scene instance and inheritance -Fixes #3127 and also properly fixes #2958 --- core/script_language.cpp | 16 ++++++++++++++ core/script_language.h | 2 ++ scene/resources/packed_scene.cpp | 36 +++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/core/script_language.cpp b/core/script_language.cpp index 2ce3844ba38..0eac39e7d13 100644 --- a/core/script_language.cpp +++ b/core/script_language.cpp @@ -92,6 +92,22 @@ void ScriptServer::init_languages() { } } +void ScriptInstance::get_property_state(List > &state) { + + List pinfo; + get_property_list(&pinfo); + for (List::Element *E=pinfo.front();E;E=E->next()) { + + if (E->get().usage&PROPERTY_USAGE_STORAGE) { + Pair p; + p.first=E->get().name; + if (get(p.first,p.second)) + state.push_back(p); + } + } +} + + Variant ScriptInstance::call(const StringName& p_method,VARIANT_ARG_DECLARE) { VARIANT_ARGPTRS; diff --git a/core/script_language.h b/core/script_language.h index 3138c88e8e4..7e278b85557 100644 --- a/core/script_language.h +++ b/core/script_language.h @@ -113,6 +113,8 @@ public: virtual void get_property_list(List *p_properties) const=0; virtual Variant::Type get_property_type(const StringName& p_name,bool *r_is_valid=NULL) const=0; + virtual void get_property_state(List > &state); + virtual void get_method_list(List *p_list) const=0; virtual bool has_method(const StringName& p_method) const=0; virtual Variant call(const StringName& p_method,VARIANT_ARG_LIST); diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp index 57d2a8d890d..43196a43d47 100644 --- a/scene/resources/packed_scene.cpp +++ b/scene/resources/packed_scene.cpp @@ -33,7 +33,7 @@ #include "scene/gui/control.h" #include "scene/2d/node_2d.h" #include "scene/main/instance_placeholder.h" - +#include "core/core_string_names.h" #define PACK_VERSION 2 bool SceneState::can_instance() const { @@ -99,6 +99,7 @@ Node *SceneState::instance(bool p_gen_edit_state) const { Node *node=NULL; + if (i==0 && base_scene_idx>=0) { //scene inheritance on root node //print_line("scene inherit"); @@ -193,7 +194,26 @@ Node *SceneState::instance(bool p_gen_edit_state) const { ERR_FAIL_INDEX_V( nprops[j].name, sname_count, NULL ); ERR_FAIL_INDEX_V( nprops[j].value, prop_count, NULL ); - node->set(snames[ nprops[j].name ],props[ nprops[j].value ],&valid); + if (snames[ nprops[j].name ]==CoreStringNames::get_singleton()->_script) { + //work around to avoid old script variables from disappearing, should be the proper fix to: + //https://github.com/godotengine/godot/issues/2958 + + //store old state + List > old_state; + if (node->get_script_instance()) { + node->get_script_instance()->get_property_state(old_state); + } + + node->set(snames[ nprops[j].name ],props[ nprops[j].value ],&valid); + + //restore old state for new script, if exists + for (List >::Element *E=old_state.front();E;E=E->next()) { + node->set(E->get().first,E->get().second); + } + } else { + + node->set(snames[ nprops[j].name ],props[ nprops[j].value ],&valid); + } } } @@ -460,6 +480,7 @@ Error SceneState::_parse_node(Node *p_owner,Node *p_node,int p_parent_idx, Map plist; p_node->get_property_list(&plist); + bool saved_script=false; for (List::Element *E=plist.front();E;E=E->next()) { @@ -508,8 +529,10 @@ Error SceneState::_parse_node(Node *p_owner,Node *p_node,int p_parent_idx, Mapget_script_instance()) { +#if 0 +// this workaround ended up causing problems: +https://github.com/godotengine/godot/issues/3127 + if (saved_script && exists && p_node->get_script_instance()) { //if this is an overriden value by another script, save it anyway //as the script change will erase it //https://github.com/godotengine/godot/issues/2958 @@ -522,7 +545,7 @@ Error SceneState::_parse_node(Node *p_owner,Node *p_node,int p_parent_idx, Map