Fix editor crash related to bt resource reloading

This commit is contained in:
Serhii Snitsaruk 2022-12-19 10:29:46 +01:00
parent d5088a9f20
commit ef7480b054
5 changed files with 22 additions and 19 deletions

View File

@ -19,6 +19,8 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
virtual bool editor_can_reload_from_file() override { return false; }
void set_description(String p_value) { void set_description(String p_value) {
description = p_value; description = p_value;
emit_changed(); emit_changed();

View File

@ -287,7 +287,7 @@ void BTTask::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_name"), "set_custom_name", "get_custom_name"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_name"), "set_custom_name", "get_custom_name");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_agent", "get_agent"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_agent", "get_agent");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_RESOURCE_TYPE, "Blackboard", 0), "", "get_blackboard"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_RESOURCE_TYPE, "Blackboard", 0), "", "get_blackboard");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "parent", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", 0), "", "get_parent"); // ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "parent", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", 0), "", "get_parent");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "children", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_children", "_get_children"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "children", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_children", "_get_children");
ADD_PROPERTY(PropertyInfo(Variant::INT, "status", PROPERTY_HINT_NONE, "", 0), "", "get_status"); ADD_PROPERTY(PropertyInfo(Variant::INT, "status", PROPERTY_HINT_NONE, "", 0), "", "get_status");

View File

@ -54,6 +54,8 @@ protected:
GDVIRTUAL0RC(String, _get_configuration_warning); GDVIRTUAL0RC(String, _get_configuration_warning);
public: public:
virtual bool editor_can_reload_from_file() override { return false; }
Node *get_agent() const { return agent; } Node *get_agent() const { return agent; }
void set_agent(Node *p_agent) { agent = p_agent; } void set_agent(Node *p_agent) { agent = p_agent; }
@ -68,6 +70,7 @@ public:
virtual Ref<BTTask> clone() const; virtual Ref<BTTask> clone() const;
virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard); virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard);
virtual String get_configuration_warning() const;
int execute(float p_delta); int execute(float p_delta);
void cancel(); void cancel();
@ -84,7 +87,6 @@ public:
int get_child_index(const Ref<BTTask> &p_child) const; int get_child_index(const Ref<BTTask> &p_child) const;
Ref<BTTask> next_sibling() const; Ref<BTTask> next_sibling() const;
virtual String get_configuration_warning() const;
void print_tree(int p_initial_tabs = 0) const; void print_tree(int p_initial_tabs = 0) const;
BTTask(); BTTask();

View File

@ -171,6 +171,11 @@ void TaskTree::load_bt(const Ref<BehaviorTree> &p_behavior_tree) {
} }
} }
void TaskTree::unload() {
bt->unreference();
tree->clear();
}
void TaskTree::update_task(const Ref<BTTask> &p_task) { void TaskTree::update_task(const Ref<BTTask> &p_task) {
ERR_FAIL_COND(p_task.is_null()); ERR_FAIL_COND(p_task.is_null());
TreeItem *item = _find_item(p_task); TreeItem *item = _find_item(p_task);
@ -853,18 +858,14 @@ void LimboAIEditor::_on_task_dragged(Ref<BTTask> p_task, Ref<BTTask> p_to_task,
} }
void LimboAIEditor::_on_resources_reload(const Vector<String> &p_resources) { void LimboAIEditor::_on_resources_reload(const Vector<String> &p_resources) {
for (int i = 0; i < p_resources.size(); i++) { for (const String &res : p_resources) {
if (!ResourceCache::has(p_resources[i])) { if (!ResourceCache::has(res)) {
continue; continue;
} }
String res_type = ResourceLoader::get_resource_type(p_resources[i]); String res_type = ResourceLoader::get_resource_type(res);
if (res_type == "BehaviorTree") { if (res_type == "BehaviorTree") {
for (int j = 0; j < history.size(); j++) { disk_changed_files.insert(res);
if (history.get(j)->get_path() == p_resources[i]) {
disk_changed_files.insert(p_resources[i]);
}
}
} }
} }
@ -886,14 +887,12 @@ void LimboAIEditor::_on_resources_reload(const Vector<String> &p_resources) {
void LimboAIEditor::_reload_modified() { void LimboAIEditor::_reload_modified() {
for (const String &fn : disk_changed_files) { for (const String &fn : disk_changed_files) {
for (int j = 0; j < history.size(); j++) { Ref<Resource> res = ResourceCache::get_ref(fn);
if (history.get(j)->get_path() == fn) { if (res.is_valid()) {
dirty.erase(history.get(j)); ERR_FAIL_COND(!res->is_class("BehaviorTree"));
history.get(j)->get_root_task()->clear_internal_resource_paths(); res->reload_from_file();
history.get(j)->reload_from_file(); if (idx_history >= 0 && history.get(idx_history) == res) {
if (j == idx_history) { edit_bt(res, true);
edit_bt(history.get(j), true);
}
} }
} }
} }
@ -1220,7 +1219,6 @@ void LimboAIEditorPlugin::apply_changes() {
} }
void LimboAIEditorPlugin::_notification(int p_notification) { void LimboAIEditorPlugin::_notification(int p_notification) {
// print_line(vformat("NOTIFICATION: %d", p_notification));
} }
void LimboAIEditorPlugin::make_visible(bool p_visible) { void LimboAIEditorPlugin::make_visible(bool p_visible) {

View File

@ -50,6 +50,7 @@ protected:
public: public:
void load_bt(const Ref<BehaviorTree> &p_behavior_tree); void load_bt(const Ref<BehaviorTree> &p_behavior_tree);
void unload();
Ref<BehaviorTree> get_bt() const { return bt; } Ref<BehaviorTree> get_bt() const { return bt; }
void update_tree() { _update_tree(); } void update_tree() { _update_tree(); }
void update_task(const Ref<BTTask> &p_task); void update_task(const Ref<BTTask> &p_task);