Skip to content

Commit

Permalink
Fix BlackboardPlan arrays shouldn't be shared between instanced agents
Browse files Browse the repository at this point in the history
Variable values are deep copied now using Variant.duplicate(true). Note that currently it doesn't duplicate objects.
  • Loading branch information
limbonaut committed Jul 6, 2024
1 parent 6432dae commit 3ceedbe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions blackboard/bb_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ String BBVariable::get_hint_string() const {
return data->hint_string;
}

BBVariable BBVariable::duplicate() const {
BBVariable BBVariable::duplicate(bool p_deep) const {
BBVariable var;
var.data->hint = data->hint;
var.data->hint_string = data->hint_string;
var.data->type = data->type;
var.data->value = data->value;
if (p_deep) {
var.data->value = data->value.duplicate(p_deep);
} else {
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;
Expand Down
2 changes: 1 addition & 1 deletion blackboard/bb_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BBVariable {
void set_hint_string(const String &p_hint_string);
String get_hint_string() const;

BBVariable duplicate() const;
BBVariable duplicate(bool p_deep = false) const;

_FORCE_INLINE_ bool is_value_changed() const { return data->value_changed; }
_FORCE_INLINE_ void reset_value_changed() { data->value_changed = false; }
Expand Down
4 changes: 2 additions & 2 deletions blackboard/blackboard_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void BlackboardPlan::sync_with_base_plan() {
inline void bb_add_var_dup_with_prefetch(const Ref<Blackboard> &p_blackboard, const StringName &p_name, const BBVariable &p_var, bool p_prefetch, Node *p_node) {
if (unlikely(p_prefetch && p_var.get_type() == Variant::NODE_PATH)) {
Node *n = p_node->get_node_or_null(p_var.get_value());
BBVariable var = p_var.duplicate();
BBVariable var = p_var.duplicate(true);
if (n != nullptr) {
var.set_value(n);
} else {
Expand All @@ -404,7 +404,7 @@ inline void bb_add_var_dup_with_prefetch(const Ref<Blackboard> &p_blackboard, co
}
p_blackboard->assign_var(p_name, var);
} else {
p_blackboard->assign_var(p_name, p_var.duplicate());
p_blackboard->assign_var(p_name, p_var.duplicate(true));
}
}

Expand Down

0 comments on commit 3ceedbe

Please sign in to comment.