Allow live reloading of built-in scripts

This commit is contained in:
kobewi 2024-07-06 23:40:01 +02:00
parent b97110cd30
commit 5636518358
4 changed files with 32 additions and 4 deletions

View file

@ -1079,10 +1079,14 @@ void ScriptEditor::_mark_built_in_scripts_as_saved(const String &p_parent_path)
} }
Ref<Script> scr = edited_res; Ref<Script> scr = edited_res;
if (scr.is_valid() && scr->is_tool()) { if (scr.is_valid()) {
trigger_live_script_reload(scr->get_path());
if (scr->is_tool()) {
scr->reload(true); scr->reload(true);
} }
} }
}
} }
void ScriptEditor::trigger_live_script_reload(const String &p_script_path) { void ScriptEditor::trigger_live_script_reload(const String &p_script_path) {

View file

@ -53,6 +53,7 @@
#include "core/io/file_access_encrypted.h" #include "core/io/file_access_encrypted.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "scene/resources/packed_scene.h"
#include "scene/scene_string_names.h" #include "scene/scene_string_names.h"
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
@ -2492,7 +2493,7 @@ void GDScriptLanguage::reload_scripts(const Array &p_scripts, bool p_soft_reload
SelfList<GDScript> *elem = script_list.first(); SelfList<GDScript> *elem = script_list.first();
while (elem) { while (elem) {
// Scripts will reload all subclasses, so only reload root scripts. // Scripts will reload all subclasses, so only reload root scripts.
if (elem->self()->is_root_script() && elem->self()->get_path().is_resource_file()) { if (elem->self()->is_root_script() && !elem->self()->get_path().is_empty()) {
scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident scripts.push_back(Ref<GDScript>(elem->self())); //cast to gdscript to avoid being erased by accident
} }
elem = elem->next(); elem = elem->next();
@ -2560,7 +2561,19 @@ void GDScriptLanguage::reload_scripts(const Array &p_scripts, bool p_soft_reload
for (KeyValue<Ref<GDScript>, HashMap<ObjectID, List<Pair<StringName, Variant>>>> &E : to_reload) { for (KeyValue<Ref<GDScript>, HashMap<ObjectID, List<Pair<StringName, Variant>>>> &E : to_reload) {
Ref<GDScript> scr = E.key; Ref<GDScript> scr = E.key;
print_verbose("GDScript: Reloading: " + scr->get_path()); print_verbose("GDScript: Reloading: " + scr->get_path());
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);
ERR_CONTINUE(scene.is_null());
Ref<SceneState> state = scene->get_state();
Ref<GDScript> fresh = state->get_sub_resource(scr->get_path());
ERR_CONTINUE(fresh.is_null());
scr->set_source_code(fresh->get_source_code());
} else {
scr->load_source_code(scr->get_path()); scr->load_source_code(scr->get_path());
}
scr->reload(p_soft_reload); scr->reload(p_soft_reload);
//restore state if saved //restore state if saved

View file

@ -1880,6 +1880,16 @@ Vector<NodePath> SceneState::get_editable_instances() const {
return editable_instances; return editable_instances;
} }
Ref<Resource> SceneState::get_sub_resource(const String &p_path) {
for (const Variant &v : variants) {
const Ref<Resource> &res = v;
if (res.is_valid() && res->get_path() == p_path) {
return res;
}
}
return Ref<Resource>();
}
//add //add
int SceneState::add_name(const StringName &p_name) { int SceneState::add_name(const StringName &p_name) {

View file

@ -195,6 +195,7 @@ public:
bool has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance = false); bool has_connection(const NodePath &p_node_from, const StringName &p_signal, const NodePath &p_node_to, const StringName &p_method, bool p_no_inheritance = false);
Vector<NodePath> get_editable_instances() const; Vector<NodePath> get_editable_instances() const;
Ref<Resource> get_sub_resource(const String &p_path);
//build API //build API