Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Fix for Tool Scripts requiring Restart

Hilderina6179107346a
diff --git a/editor/script/script_editor_plugin.cpp b/editor/script/script_editor_plugin.cpp
index 413c38f0b9a..2600e3199d1 100644
--- a/editor/script/script_editor_plugin.cpp
+++ b/editor/script/script_editor_plugin.cpp
@@ -1352,7 +1352,11 @@ void ScriptEditor::_mark_built_in_scripts_as_saved(const String &p_parent_path)
if (scr.is_valid()) {
trigger_live_script_reload(scr->get_path());
clear_docs_from_script(scr);
- scr->reload(true);
+ if (scr->is_tool()) {
+ scr->get_language()->reload_tool_script(edited_res);
+ } else {
+ scr->get_language()->reload_scripts({ scr });
+ }
update_docs_from_script(scr);
}
}
diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp
index 03949a330d4..6ac9b5ba5eb 100644
--- a/modules/gdscript/gdscript.cpp
+++ b/modules/gdscript/gdscript.cpp
@@ -2550,6 +2550,9 @@ void GDScriptLanguage::reload_scripts(const Array &p_scripts) {
for (KeyValue<Ref<GDScript>, HashMap<ObjectID, List<Pair<StringName, Variant>>>> &E : to_reload) {
Ref<GDScript> scr = E.key;
print_verbose("GDScript: Reloading: " + scr->get_path());
+#ifdef TOOLS_ENABLED
+ bool was_tool = scr->is_tool();
+#endif
if (scr->is_built_in()) {
// TODO: It would be nice to do it more efficiently than loading the whole scene again.
Ref<PackedScene> scene = ResourceLoader::load(scr->get_path().get_slice("::", 0), "", ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP);
@@ -2565,6 +2568,14 @@ void GDScriptLanguage::reload_scripts(const Array &p_scripts) {
}
scr->reload(true);
+#ifdef TOOLS_ENABLED
+ // If @tool is added/removed, we reconstruct the instance
+ // This changes a placeholder instance to an actual instance or vice versa.
+ if (was_tool != scr->is_tool()) {
+ _prepare_script_for_reload(scr, to_reload[scr]);
+ }
+#endif
+
//restore state if saved
for (KeyValue<ObjectID, List<Pair<StringName, Variant>>> &F : E.value) {
List<Pair<StringName, Variant>> &saved_state = F.value;
@@ -2606,6 +2617,40 @@ void GDScriptLanguage::reload_scripts(const Array &p_scripts) {
#endif // DEBUG_ENABLED
}
+#ifdef TOOLS_ENABLED
+void GDScriptLanguage::_prepare_script_for_reload(const Ref<GDScript> &p_script, HashMap<ObjectID, List<Pair<StringName, Variant>>> &p_map) {
+ MutexLock lock(mutex);
+ while (p_script->instances.first()) {
+ GDScriptInstance *instance = p_script->instances.first()->self();
+ // Save instance info.
+ List<Pair<StringName, Variant>> state;
+ instance->get_property_state(state);
+ p_map[instance->get_owner()->get_instance_id()] = state;
+ instance->get_owner()->set_script(Variant());
+ }
+
+ // Same thing for placeholders.
+ while (p_script->placeholders.size()) {
+ Object *obj = (*p_script->placeholders.begin())->get_owner();
+
+ // Save instance info.
+ if (obj->get_script_instance()) {
+ p_map.insert(obj->get_instance_id(), List<Pair<StringName, Variant>>());
+ List<Pair<StringName, Variant>> &state = p_map[obj->get_instance_id()];
+ obj->get_script_instance()->get_property_state(state);
+ obj->set_script(Variant());
+ } else {
+ // No instance found. Let's remove it so we don't loop forever.
+ p_script->placeholders.erase(*p_script->placeholders.begin());
+ }
+ }
+
+ for (const KeyValue<ObjectID, List<Pair<StringName, Variant>>> &F : p_script->pending_reload_state) {
+ p_map[F.key] = F.value; // Pending to reload, use this one instead.
+ }
+}
+#endif
+
void GDScriptLanguage::reload_tool_script(const Ref<Script> &p_script) {
Array scripts = { p_script };
reload_scripts(scripts);
diff --git a/modules/gdscript/gdscript.h b/modules/gdscript/gdscript.h
index 5a68fb1167d..0d230b64b87 100644
--- a/modules/gdscript/gdscript.h
+++ b/modules/gdscript/gdscript.h
@@ -455,6 +455,7 @@ class GDScriptLanguage : public ScriptLanguage {
bool profiling;
bool profile_native_calls;
uint64_t script_frame_time;
+ void _prepare_script_for_reload(const Ref<GDScript> &p_script, HashMap<ObjectID, List<Pair<StringName, Variant>>> &p_map);
#endif
HashMap<String, ObjectID> orphan_subclasses;