Merge pull request #35 from limbonaut/complain-if-variable-missing
Blackboard: Complain when variable is missing
This commit is contained in:
commit
69bcc763f7
|
@ -32,12 +32,15 @@ Ref<Blackboard> Blackboard::top() const {
|
||||||
return bb;
|
return bb;
|
||||||
}
|
}
|
||||||
|
|
||||||
Variant Blackboard::get_var(const String &p_name, const Variant &p_default) const {
|
Variant Blackboard::get_var(const String &p_name, const Variant &p_default, bool p_complain) const {
|
||||||
if (data.has(p_name)) {
|
if (data.has(p_name)) {
|
||||||
return data.get(p_name).get_value();
|
return data.get(p_name).get_value();
|
||||||
} else if (parent.is_valid()) {
|
} else if (parent.is_valid()) {
|
||||||
return parent->get_var(p_name, p_default);
|
return parent->get_var(p_name, p_default);
|
||||||
} else {
|
} else {
|
||||||
|
if (p_complain) {
|
||||||
|
ERR_PRINT(vformat("Blackboard: Variable \"%s\" not found.", p_name));
|
||||||
|
}
|
||||||
return p_default;
|
return p_default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +93,7 @@ void Blackboard::prefetch_nodepath_vars(Node *p_node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Blackboard::_bind_methods() {
|
void Blackboard::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default"), &Blackboard::get_var, Variant());
|
ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default", "p_complain"), &Blackboard::get_var, Variant(), true);
|
||||||
ClassDB::bind_method(D_METHOD("set_var", "p_name", "p_value"), &Blackboard::set_var);
|
ClassDB::bind_method(D_METHOD("set_var", "p_name", "p_value"), &Blackboard::set_var);
|
||||||
ClassDB::bind_method(D_METHOD("has_var", "p_name"), &Blackboard::has_var);
|
ClassDB::bind_method(D_METHOD("has_var", "p_name"), &Blackboard::has_var);
|
||||||
ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent);
|
ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent);
|
||||||
|
|
|
@ -46,7 +46,7 @@ public:
|
||||||
|
|
||||||
Ref<Blackboard> top() const;
|
Ref<Blackboard> top() const;
|
||||||
|
|
||||||
Variant get_var(const String &p_name, const Variant &p_default) const;
|
Variant get_var(const String &p_name, const Variant &p_default, bool p_complain = true) const;
|
||||||
void set_var(const String &p_name, const Variant &p_value);
|
void set_var(const String &p_name, const Variant &p_value);
|
||||||
bool has_var(const String &p_name) const;
|
bool has_var(const String &p_name) const;
|
||||||
void erase_var(const String &p_name);
|
void erase_var(const String &p_name);
|
||||||
|
|
|
@ -37,8 +37,9 @@
|
||||||
<return type="Variant" />
|
<return type="Variant" />
|
||||||
<param index="0" name="p_name" type="String" />
|
<param index="0" name="p_name" type="String" />
|
||||||
<param index="1" name="p_default" type="Variant" default="null" />
|
<param index="1" name="p_default" type="Variant" default="null" />
|
||||||
|
<param index="2" name="p_complain" type="bool" default="true" />
|
||||||
<description>
|
<description>
|
||||||
Returns variable value.
|
Returns variable value or [param p_default] if variable doesn't exist. If [param p_complain] is [code]true[/code], an error will be printed if variable doesn't exist.
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="has_var" qualifiers="const">
|
<method name="has_var" qualifiers="const">
|
||||||
|
|
|
@ -100,7 +100,9 @@ TEST_CASE("[Modules][LimboAI] BBNode") {
|
||||||
CHECK(param->get_value(dummy, bb) == Variant(other));
|
CHECK(param->get_value(dummy, bb) == Variant(other));
|
||||||
}
|
}
|
||||||
SUBCASE("When variable doesn't exist") {
|
SUBCASE("When variable doesn't exist") {
|
||||||
|
ERR_PRINT_OFF;
|
||||||
CHECK(param->get_value(dummy, bb, Variant()).is_null());
|
CHECK(param->get_value(dummy, bb, Variant()).is_null());
|
||||||
|
ERR_PRINT_ON;
|
||||||
}
|
}
|
||||||
SUBCASE("When variable has wrong type") {
|
SUBCASE("When variable has wrong type") {
|
||||||
bb->set_var("test_var", 123);
|
bb->set_var("test_var", 123);
|
||||||
|
|
|
@ -35,8 +35,10 @@ TEST_CASE("[Modules][LimboAI] BTCheckTrigger") {
|
||||||
|
|
||||||
ct->set_variable("trigger");
|
ct->set_variable("trigger");
|
||||||
|
|
||||||
SUBCASE("When variable is not set") {
|
SUBCASE("When variable is not found") {
|
||||||
|
ERR_PRINT_OFF;
|
||||||
CHECK(ct->execute(0.01666) == BTTask::FAILURE);
|
CHECK(ct->execute(0.01666) == BTTask::FAILURE);
|
||||||
|
ERR_PRINT_ON;
|
||||||
}
|
}
|
||||||
SUBCASE("When variable set to false") {
|
SUBCASE("When variable set to false") {
|
||||||
bb->set_var("trigger", false);
|
bb->set_var("trigger", false);
|
||||||
|
|
|
@ -73,14 +73,14 @@ TEST_CASE("[Modules][LimboAI] BTNewScope") {
|
||||||
ns->get_blackboard()->set_var("berry", "raspberry");
|
ns->get_blackboard()->set_var("berry", "raspberry");
|
||||||
CHECK(ns->get_blackboard()->get_var("berry", "wetgoop") == "raspberry");
|
CHECK(ns->get_blackboard()->get_var("berry", "wetgoop") == "raspberry");
|
||||||
CHECK(child->get_blackboard()->get_var("berry", "wetgoop") == "raspberry");
|
CHECK(child->get_blackboard()->get_var("berry", "wetgoop") == "raspberry");
|
||||||
CHECK(parent->get_blackboard()->get_var("berry", "wetgoop") == "wetgoop");
|
CHECK(parent->get_blackboard()->get_var("berry", "wetgoop", false) == "wetgoop");
|
||||||
CHECK_FALSE(parent->get_blackboard()->has_var("berry"));
|
CHECK_FALSE(parent->get_blackboard()->has_var("berry"));
|
||||||
|
|
||||||
// * Check if setting a variable doesn't propagate it up the scope (now with the child task)
|
// * Check if setting a variable doesn't propagate it up the scope (now with the child task)
|
||||||
child->get_blackboard()->set_var("seed", "sunflower");
|
child->get_blackboard()->set_var("seed", "sunflower");
|
||||||
CHECK(child->get_blackboard()->get_var("seed", "wetgoop") == "sunflower");
|
CHECK(child->get_blackboard()->get_var("seed", "wetgoop") == "sunflower");
|
||||||
CHECK(ns->get_blackboard()->get_var("seed", "wetgoop") == "sunflower");
|
CHECK(ns->get_blackboard()->get_var("seed", "wetgoop") == "sunflower");
|
||||||
CHECK(parent->get_blackboard()->get_var("seed", "wetgoop") == "wetgoop");
|
CHECK(parent->get_blackboard()->get_var("seed", "wetgoop", false) == "wetgoop");
|
||||||
CHECK_FALSE(parent->get_blackboard()->has_var("seed"));
|
CHECK_FALSE(parent->get_blackboard()->has_var("seed"));
|
||||||
|
|
||||||
// * Check return status
|
// * Check return status
|
||||||
|
|
Loading…
Reference in New Issue