You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Fix properties being lost when reloading placeholder GDScript instance
During reloading in `GDScriptLanguage::reload_all_scripts` a placeholder instance that must remain so is replaced with a new placeholder instance. The state is then restored by calling `ScriptInstance::set` for each property. This does not work if the script is missing the properties due to build/parse failing. The fix for such cases is to call `placeholder_set_fallback` instead of `set` on the script instance. I took this chance to move the `build_failed` flag from `PlaceHolderScriptInstance` to `Script`. That improves the code a lot. I also renamed it to `placeholder_fallback_enabled` which is a much better name (`build_failed` could lead to misunderstandings).
This commit is contained in:
@@ -475,20 +475,15 @@ bool GDScript::_update_exports() {
|
||||
_signals[c->_signals[i].name] = c->_signals[i].arguments;
|
||||
}
|
||||
} else {
|
||||
for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
|
||||
E->get()->set_build_failed(true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!valid) {
|
||||
for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
|
||||
E->get()->set_build_failed(true);
|
||||
}
|
||||
placeholder_fallback_enabled = true;
|
||||
return false;
|
||||
}
|
||||
} else if (!valid || placeholder_fallback_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
placeholder_fallback_enabled = false;
|
||||
|
||||
if (base_cache.is_valid()) {
|
||||
if (base_cache->_update_exports()) {
|
||||
changed = true;
|
||||
@@ -503,7 +498,6 @@ bool GDScript::_update_exports() {
|
||||
_update_exports_values(values, propnames);
|
||||
|
||||
for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
|
||||
E->get()->set_build_failed(false);
|
||||
E->get()->update(propnames, values);
|
||||
}
|
||||
}
|
||||
@@ -907,6 +901,7 @@ GDScript::GDScript() :
|
||||
tool = false;
|
||||
#ifdef TOOLS_ENABLED
|
||||
source_changed_cache = false;
|
||||
placeholder_fallback_enabled = false;
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
@@ -1675,6 +1670,8 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so
|
||||
//restore state if saved
|
||||
for (Map<ObjectID, List<Pair<StringName, Variant> > >::Element *F = E->get().front(); F; F = F->next()) {
|
||||
|
||||
List<Pair<StringName, Variant> > &saved_state = F->get();
|
||||
|
||||
Object *obj = ObjectDB::get_instance(F->key());
|
||||
if (!obj)
|
||||
continue;
|
||||
@@ -1684,16 +1681,26 @@ void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_so
|
||||
obj->set_script(RefPtr());
|
||||
}
|
||||
obj->set_script(scr.get_ref_ptr());
|
||||
if (!obj->get_script_instance()) {
|
||||
|
||||
ScriptInstance *script_instance = obj->get_script_instance();
|
||||
|
||||
if (!script_instance) {
|
||||
//failed, save reload state for next time if not saved
|
||||
if (!scr->pending_reload_state.has(obj->get_instance_id())) {
|
||||
scr->pending_reload_state[obj->get_instance_id()] = F->get();
|
||||
scr->pending_reload_state[obj->get_instance_id()] = saved_state;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
for (List<Pair<StringName, Variant> >::Element *G = F->get().front(); G; G = G->next()) {
|
||||
obj->get_script_instance()->set(G->get().first, G->get().second);
|
||||
if (script_instance->is_placeholder() && scr->is_placeholder_fallback_enabled()) {
|
||||
PlaceHolderScriptInstance *placeholder = static_cast<PlaceHolderScriptInstance *>(script_instance);
|
||||
for (List<Pair<StringName, Variant> >::Element *G = saved_state.front(); G; G = G->next()) {
|
||||
placeholder->property_set_fallback(G->get().first, G->get().second);
|
||||
}
|
||||
} else {
|
||||
for (List<Pair<StringName, Variant> >::Element *G = saved_state.front(); G; G = G->next()) {
|
||||
script_instance->set(G->get().first, G->get().second);
|
||||
}
|
||||
}
|
||||
|
||||
scr->pending_reload_state.erase(obj->get_instance_id()); //as it reloaded, remove pending state
|
||||
|
||||
Reference in New Issue
Block a user