Handle NodePath vars
This commit is contained in:
parent
4f06bb5a3e
commit
c43fac596c
|
@ -1,6 +1,10 @@
|
||||||
/* blackboard.cpp */
|
/* blackboard.cpp */
|
||||||
|
|
||||||
#include "blackboard.h"
|
#include "blackboard.h"
|
||||||
|
#include "core/error_macros.h"
|
||||||
|
#include "core/variant.h"
|
||||||
|
#include "scene/main/node.h"
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
Variant Blackboard::get_var(const Variant &p_key, const Variant &p_default) const {
|
Variant Blackboard::get_var(const Variant &p_key, const Variant &p_default) const {
|
||||||
if (data.has(p_key)) {
|
if (data.has(p_key)) {
|
||||||
|
@ -20,6 +24,19 @@ bool Blackboard::has_var(const Variant &p_key) const {
|
||||||
return data.has(p_key) || (parent.is_valid() && parent->has_var(p_key));
|
return data.has(p_key) || (parent.is_valid() && parent->has_var(p_key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Blackboard::prefetch_nodepath_vars(Node *p_node) {
|
||||||
|
ERR_FAIL_COND(p_node == nullptr);
|
||||||
|
for (int i = 0; i < data.size(); i++) {
|
||||||
|
Variant value = data.get_value_at_index(i);
|
||||||
|
if (value.get_type() == Variant::NODE_PATH) {
|
||||||
|
Node *fetched_node = p_node->get_node_or_null(value);
|
||||||
|
if (fetched_node != nullptr) {
|
||||||
|
data[data.get_key_at_index(i)] = fetched_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void Blackboard::_bind_methods() {
|
void Blackboard::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("get_data"), &Blackboard::get_data);
|
ClassDB::bind_method(D_METHOD("get_data"), &Blackboard::get_data);
|
||||||
ClassDB::bind_method(D_METHOD("set_data", "p_data"), &Blackboard::set_data);
|
ClassDB::bind_method(D_METHOD("set_data", "p_data"), &Blackboard::set_data);
|
||||||
|
|
|
@ -28,6 +28,7 @@ public:
|
||||||
Variant get_var(const Variant &p_key, const Variant &p_default) const;
|
Variant get_var(const Variant &p_key, const Variant &p_default) const;
|
||||||
void set_var(const Variant &p_key, const Variant &p_value);
|
void set_var(const Variant &p_key, const Variant &p_value);
|
||||||
bool has_var(const Variant &p_key) const;
|
bool has_var(const Variant &p_key) const;
|
||||||
|
void prefetch_nodepath_vars(Node *p_node);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BLACKBOARD_H
|
#endif // BLACKBOARD_H
|
|
@ -21,6 +21,10 @@ void BTPlayer::_load_tree() {
|
||||||
ERR_FAIL_COND_MSG(!behavior_tree.is_valid(), "BTPlayer needs a valid behavior tree.");
|
ERR_FAIL_COND_MSG(!behavior_tree.is_valid(), "BTPlayer needs a valid behavior tree.");
|
||||||
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "Behavior tree has no valid root task.");
|
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "Behavior tree has no valid root task.");
|
||||||
_loaded_tree = behavior_tree;
|
_loaded_tree = behavior_tree;
|
||||||
|
if (prefetch_nodepath_vars == true) {
|
||||||
|
// blackboard->prefetch_nodepath_vars(get_owner());
|
||||||
|
blackboard->prefetch_nodepath_vars(this);
|
||||||
|
}
|
||||||
_root_task = _loaded_tree->instance(get_owner(), blackboard);
|
_root_task = _loaded_tree->instance(get_owner(), blackboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +104,8 @@ void BTPlayer::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("get_auto_restart"), &BTPlayer::get_auto_restart);
|
ClassDB::bind_method(D_METHOD("get_auto_restart"), &BTPlayer::get_auto_restart);
|
||||||
ClassDB::bind_method(D_METHOD("set_blackboard", "p_blackboard"), &BTPlayer::set_blackboard);
|
ClassDB::bind_method(D_METHOD("set_blackboard", "p_blackboard"), &BTPlayer::set_blackboard);
|
||||||
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTPlayer::get_blackboard);
|
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTPlayer::get_blackboard);
|
||||||
|
ClassDB::bind_method(D_METHOD("set_prefetch_nodepath_vars", "p_value"), &BTPlayer::set_prefetch_nodepath_vars);
|
||||||
|
ClassDB::bind_method(D_METHOD("get_prefetch_nodepath_vars"), &BTPlayer::get_prefetch_nodepath_vars);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_blackboard"), &BTPlayer::_set_blackboard_data);
|
ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_blackboard"), &BTPlayer::_set_blackboard_data);
|
||||||
ClassDB::bind_method(D_METHOD("_get_blackboard_data"), &BTPlayer::_get_blackboard_data);
|
ClassDB::bind_method(D_METHOD("_get_blackboard_data"), &BTPlayer::_get_blackboard_data);
|
||||||
|
@ -113,6 +119,7 @@ void BTPlayer::_bind_methods() {
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_restart"), "set_auto_restart", "get_auto_restart");
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_restart"), "set_auto_restart", "get_auto_restart");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "", "get_blackboard");
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "", "get_blackboard");
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_blackboard_data"), "_set_blackboard_data", "_get_blackboard_data");
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_blackboard_data"), "_set_blackboard_data", "_get_blackboard_data");
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefetch_nodepath_vars"), "set_prefetch_nodepath_vars", "get_prefetch_nodepath_vars");
|
||||||
|
|
||||||
BIND_ENUM_CONSTANT(IDLE);
|
BIND_ENUM_CONSTANT(IDLE);
|
||||||
BIND_ENUM_CONSTANT(PHYSICS);
|
BIND_ENUM_CONSTANT(PHYSICS);
|
||||||
|
|
|
@ -25,6 +25,7 @@ private:
|
||||||
bool active = false;
|
bool active = false;
|
||||||
bool auto_restart = false;
|
bool auto_restart = false;
|
||||||
Ref<Blackboard> blackboard;
|
Ref<Blackboard> blackboard;
|
||||||
|
bool prefetch_nodepath_vars = true;
|
||||||
|
|
||||||
Ref<BehaviorTree> _loaded_tree;
|
Ref<BehaviorTree> _loaded_tree;
|
||||||
Ref<BTTask> _root_task;
|
Ref<BTTask> _root_task;
|
||||||
|
@ -55,6 +56,9 @@ public:
|
||||||
Ref<Blackboard> get_blackboard() const { return blackboard; }
|
Ref<Blackboard> get_blackboard() const { return blackboard; }
|
||||||
void set_blackboard(const Ref<Blackboard> &p_blackboard) { blackboard = p_blackboard; }
|
void set_blackboard(const Ref<Blackboard> &p_blackboard) { blackboard = p_blackboard; }
|
||||||
|
|
||||||
|
void set_prefetch_nodepath_vars(bool p_value) { prefetch_nodepath_vars = p_value; }
|
||||||
|
bool get_prefetch_nodepath_vars() const { return prefetch_nodepath_vars; }
|
||||||
|
|
||||||
void update(float p_delta);
|
void update(float p_delta);
|
||||||
void restart();
|
void restart();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue