Skip to content

Commit

Permalink
Improve variable synchronization between base and derived blackboard …
Browse files Browse the repository at this point in the history
…plans

When variable is altered in the base plan, values should properly sync to the derived plans if those vars are not altered in the derived plan. Editor QoL improvement.
  • Loading branch information
limbonaut committed Feb 9, 2024
1 parent 98a685b commit 95c2496
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 6 additions & 2 deletions blackboard/bb_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void BBVariable::unref() {

void BBVariable::set_value(const Variant &p_value) {
data->value = p_value; // Setting value even when bound as a fallback in case the binding fails.
data->value_changed = true;

if (is_bound()) {
Object *obj = ObjectDB::get_instance(ObjectID(data->bound_object));
Expand Down Expand Up @@ -83,6 +84,9 @@ BBVariable BBVariable::duplicate() const {
var.data->hint_string = data->hint_string;
var.data->type = data->type;
var.data->value = data->value;
var.data->binding_path = data->binding_path;
var.data->bound_object = data->bound_object;
var.data->bound_property = data->bound_property;
return var;
}

Expand Down Expand Up @@ -135,11 +139,11 @@ bool BBVariable::operator==(const BBVariable &p_var) const {
return false;
}

if (data->value != p_var.data->value) {
if (data->hint_string != p_var.data->hint_string) {
return false;
}

if (data->hint_string != p_var.data->hint_string) {
if (get_value() != p_var.get_value()) {
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion blackboard/bb_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ using namespace godot;
class BBVariable {
private:
struct Data {
// Is used to decide if the value needs to be synced in a derived plan.
bool value_changed = false;

SafeRefCount refcount;
Variant value;
Variant::Type type = Variant::NIL;
Expand Down Expand Up @@ -53,6 +56,9 @@ class BBVariable {

BBVariable duplicate() const;

_FORCE_INLINE_ bool is_value_changed() const { return data->value_changed; }
_FORCE_INLINE_ void reset_value_changed() { data->value_changed = false; }

bool is_same_prop_info(const BBVariable &p_other) const;
void copy_prop_info(const BBVariable &p_other);

Expand All @@ -62,7 +68,7 @@ class BBVariable {
bool has_binding() { return data->binding_path.is_empty(); }

// * Runtime binding methods
bool is_bound() const { return data->bound_object != 0; }
_FORCE_INLINE_ bool is_bound() const { return data->bound_object != 0; }
void bind(Object *p_object, const StringName &p_property);
void unbind();

Expand Down
9 changes: 8 additions & 1 deletion blackboard/blackboard_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ bool BlackboardPlan::_set(const StringName &p_name, const Variant &p_value) {
// * Editor
if (var_map.has(prop_name)) {
var_map[prop_name].set_value(p_value);
if (base.is_valid() && p_value == base->get_var(prop_name).get_value()) {
// When user pressed reset property button in inspector...
var_map[prop_name].reset_value_changed();
}
return true;
}

Expand Down Expand Up @@ -234,8 +238,11 @@ void BlackboardPlan::sync_with_base_plan() {
var.copy_prop_info(base_var);
changed = true;
}
if (var.get_value().get_type() != base_var.get_type()) {
if ((!var.is_value_changed() && var.get_value() != base_var.get_value()) ||
(var.get_value().get_type() != base_var.get_type())) {
// Reset value according to base plan.
var.set_value(base_var.get_value());
var.reset_value_changed();
changed = true;
}
}
Expand Down

0 comments on commit 95c2496

Please sign in to comment.