Browse Source

Merge pull request #89261 from paulloz/core/fix-script-reloading-outside-script-editor

Fix how scripts reload outside of ScriptEditor
Rémi Verschelde 1 year ago
parent
commit
d5f944ff10
3 changed files with 22 additions and 0 deletions
  1. 1 0
      core/io/resource.cpp
  2. 19 0
      core/object/script_language.cpp
  3. 2 0
      core/object/script_language.h

+ 1 - 0
core/io/resource.cpp

@@ -214,6 +214,7 @@ Error Resource::copy_from(const Ref<Resource> &p_resource) {
 	}
 	return OK;
 }
+
 void Resource::reload_from_file() {
 	String path = get_path();
 	if (!path.is_resource_file()) {

+ 19 - 0
core/object/script_language.cpp

@@ -34,6 +34,7 @@
 #include "core/core_string_names.h"
 #include "core/debugger/engine_debugger.h"
 #include "core/debugger/script_debugger.h"
+#include "core/io/resource_loader.h"
 
 #include <stdint.h>
 
@@ -170,6 +171,24 @@ void Script::_bind_methods() {
 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_source_code", "get_source_code");
 }
 
+void Script::reload_from_file() {
+#ifdef TOOLS_ENABLED
+	// Replicates how the ScriptEditor reloads script resources, which generally handles it.
+	// However, when scripts are to be reloaded but aren't open in the internal editor, we go through here instead.
+	const Ref<Script> rel = ResourceLoader::load(ResourceLoader::path_remap(get_path()), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
+	if (rel.is_null()) {
+		return;
+	}
+
+	set_source_code(rel->get_source_code());
+	set_last_modified_time(rel->get_last_modified_time());
+
+	reload();
+#else
+	Resource::reload_from_file();
+#endif
+}
+
 void ScriptServer::set_scripting_enabled(bool p_enabled) {
 	scripting_enabled = p_enabled;
 }

+ 2 - 0
core/object/script_language.h

@@ -125,6 +125,8 @@ protected:
 	Dictionary _get_script_constant_map();
 
 public:
+	virtual void reload_from_file() override;
+
 	virtual bool can_instantiate() const = 0;
 
 	virtual Ref<Script> get_base_script() const = 0; //for script inheritance