Fix shadowing members in the module

This commit is contained in:
Serhii Snitsaruk 2023-07-20 20:10:02 +02:00
parent 6a35c25a94
commit d2eede3f76
5 changed files with 90 additions and 88 deletions

View File

@ -22,11 +22,10 @@ String BTTask::_generate_name() const {
ERR_FAIL_COND_V_MSG(!get_script_instance()->get_script()->is_tool(), "ERROR: not a tool script", "Task script should be a \"tool\" script!"); ERR_FAIL_COND_V_MSG(!get_script_instance()->get_script()->is_tool(), "ERROR: not a tool script", "Task script should be a \"tool\" script!");
return get_script_instance()->call(LimboStringNames::get_singleton()->_generate_name); return get_script_instance()->call(LimboStringNames::get_singleton()->_generate_name);
} }
String name = get_script_instance()->get_script()->get_path(); String script_path = get_script_instance()->get_script()->get_path();
if (!name.is_empty()) { if (!script_path.is_empty()) {
// Generate name based on script file // Generate name based on script file
name = name.get_basename().get_file().trim_prefix("BT").to_pascal_case(); return script_path.get_basename().get_file().trim_prefix("BT").to_pascal_case();
return name;
} }
} }
return get_class().trim_prefix("BT"); return get_class().trim_prefix("BT");
@ -44,35 +43,35 @@ Array BTTask::_get_children() const {
} }
void BTTask::_set_children(Array p_children) { void BTTask::_set_children(Array p_children) {
children.clear(); data.children.clear();
const int num_children = p_children.size(); const int num_children = p_children.size();
children.resize(num_children); data.children.resize(num_children);
for (int i = 0; i < num_children; i++) { for (int i = 0; i < num_children; i++) {
Variant task_var = p_children[i]; Variant task_var = p_children[i];
Ref<BTTask> task_ref = task_var; Ref<BTTask> task_ref = task_var;
task_ref->parent = this; task_ref->data.parent = this;
children.set(i, task_var); data.children.set(i, task_var);
} }
} }
String BTTask::get_task_name() const { String BTTask::get_task_name() const {
if (custom_name.is_empty()) { if (data.custom_name.is_empty()) {
return _generate_name(); return _generate_name();
} }
return custom_name; return data.custom_name;
} }
Ref<BTTask> BTTask::get_root() const { Ref<BTTask> BTTask::get_root() const {
const BTTask *task = this; const BTTask *task = this;
while (!task->is_root()) { while (!task->is_root()) {
task = task->parent; task = task->data.parent;
} }
return Ref<BTTask>(task); return Ref<BTTask>(task);
} }
void BTTask::set_custom_name(const String &p_name) { void BTTask::set_custom_name(const String &p_name) {
if (custom_name != p_name) { if (data.custom_name != p_name) {
custom_name = p_name; data.custom_name = p_name;
emit_changed(); emit_changed();
} }
}; };
@ -80,9 +79,9 @@ void BTTask::set_custom_name(const String &p_name) {
void BTTask::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) { void BTTask::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) {
ERR_FAIL_COND(p_agent == nullptr); ERR_FAIL_COND(p_agent == nullptr);
ERR_FAIL_COND(p_blackboard == nullptr); ERR_FAIL_COND(p_blackboard == nullptr);
agent = p_agent; data.agent = p_agent;
blackboard = p_blackboard; data.blackboard = p_blackboard;
for (int i = 0; i < children.size(); i++) { for (int i = 0; i < data.children.size(); i++) {
get_child(i)->initialize(p_agent, p_blackboard); get_child(i)->initialize(p_agent, p_blackboard);
} }
@ -93,13 +92,13 @@ void BTTask::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard) {
Ref<BTTask> BTTask::clone() const { Ref<BTTask> BTTask::clone() const {
Ref<BTTask> inst = duplicate(false); Ref<BTTask> inst = duplicate(false);
inst->parent = nullptr; inst->data.parent = nullptr;
inst->agent = nullptr; inst->data.agent = nullptr;
inst->blackboard.unref(); inst->data.blackboard.unref();
for (int i = 0; i < children.size(); i++) { for (int i = 0; i < data.children.size(); i++) {
Ref<BTTask> c = get_child(i)->clone(); Ref<BTTask> c = get_child(i)->clone();
c->parent = inst.ptr(); c->data.parent = inst.ptr();
inst->children.set(i, c); inst->data.children.set(i, c);
} }
// Make BBParam properties unique. // Make BBParam properties unique.
@ -132,96 +131,96 @@ Ref<BTTask> BTTask::clone() const {
} }
int BTTask::execute(double p_delta) { int BTTask::execute(double p_delta) {
if (status != RUNNING) { if (data.status != RUNNING) {
// Reset children status. // Reset children status.
if (status != FRESH) { if (data.status != FRESH) {
for (int i = 0; i < get_child_count(); i++) { for (int i = 0; i < get_child_count(); i++) {
children.get(i)->cancel(); data.children.get(i)->cancel();
} }
} }
if (!GDVIRTUAL_CALL(_enter)) { if (!GDVIRTUAL_CALL(_enter)) {
_enter(); _enter();
} }
} else { } else {
elapsed += p_delta; data.elapsed += p_delta;
} }
if (!GDVIRTUAL_CALL(_tick, p_delta, status)) { if (!GDVIRTUAL_CALL(_tick, p_delta, data.status)) {
status = _tick(p_delta); data.status = _tick(p_delta);
} }
if (status != RUNNING) { if (data.status != RUNNING) {
if (!GDVIRTUAL_CALL(_exit)) { if (!GDVIRTUAL_CALL(_exit)) {
_exit(); _exit();
} }
elapsed = 0.0; data.elapsed = 0.0;
} }
return status; return data.status;
} }
void BTTask::cancel() { void BTTask::cancel() {
for (int i = 0; i < children.size(); i++) { for (int i = 0; i < data.children.size(); i++) {
get_child(i)->cancel(); get_child(i)->cancel();
} }
if (status == RUNNING) { if (data.status == RUNNING) {
if (!GDVIRTUAL_CALL(_exit)) { if (!GDVIRTUAL_CALL(_exit)) {
_exit(); _exit();
} }
} }
status = FRESH; data.status = FRESH;
elapsed = 0.0; data.elapsed = 0.0;
} }
Ref<BTTask> BTTask::get_child(int p_idx) const { Ref<BTTask> BTTask::get_child(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, children.size(), nullptr); ERR_FAIL_INDEX_V(p_idx, data.children.size(), nullptr);
return children.get(p_idx); return data.children.get(p_idx);
} }
int BTTask::get_child_count() const { int BTTask::get_child_count() const {
return children.size(); return data.children.size();
} }
void BTTask::add_child(Ref<BTTask> p_child) { void BTTask::add_child(Ref<BTTask> p_child) {
ERR_FAIL_COND_MSG(p_child->get_parent().is_valid(), "p_child already has a parent!"); ERR_FAIL_COND_MSG(p_child->get_parent().is_valid(), "p_child already has a parent!");
p_child->parent = this; p_child->data.parent = this;
children.push_back(p_child); data.children.push_back(p_child);
emit_changed(); emit_changed();
} }
void BTTask::add_child_at_index(Ref<BTTask> p_child, int p_idx) { void BTTask::add_child_at_index(Ref<BTTask> p_child, int p_idx) {
ERR_FAIL_COND_MSG(p_child->get_parent().is_valid(), "p_child already has a parent!"); ERR_FAIL_COND_MSG(p_child->get_parent().is_valid(), "p_child already has a parent!");
if (p_idx < 0 || p_idx > children.size()) { if (p_idx < 0 || p_idx > data.children.size()) {
p_idx = children.size(); p_idx = data.children.size();
} }
children.insert(p_idx, p_child); data.children.insert(p_idx, p_child);
p_child->parent = this; p_child->data.parent = this;
emit_changed(); emit_changed();
} }
void BTTask::remove_child(Ref<BTTask> p_child) { void BTTask::remove_child(Ref<BTTask> p_child) {
int idx = children.find(p_child); int idx = data.children.find(p_child);
if (idx == -1) { if (idx == -1) {
ERR_FAIL_MSG("p_child not found!"); ERR_FAIL_MSG("p_child not found!");
} else { } else {
children.remove_at(idx); data.children.remove_at(idx);
p_child->parent = nullptr; p_child->data.parent = nullptr;
emit_changed(); emit_changed();
} }
} }
void BTTask::remove_child_at_index(int p_idx) { void BTTask::remove_child_at_index(int p_idx) {
ERR_FAIL_INDEX(p_idx, get_child_count()); ERR_FAIL_INDEX(p_idx, get_child_count());
children.remove_at(p_idx); data.children.remove_at(p_idx);
} }
bool BTTask::has_child(const Ref<BTTask> &p_child) const { bool BTTask::has_child(const Ref<BTTask> &p_child) const {
return children.find(p_child) != -1; return data.children.find(p_child) != -1;
} }
bool BTTask::is_descendant_of(const Ref<BTTask> &p_task) const { bool BTTask::is_descendant_of(const Ref<BTTask> &p_task) const {
const BTTask *task = this; const BTTask *task = this;
while (task != nullptr) { while (task != nullptr) {
task = task->parent; task = task->data.parent;
if (task == p_task.ptr()) { if (task == p_task.ptr()) {
return true; return true;
} }
@ -230,14 +229,14 @@ bool BTTask::is_descendant_of(const Ref<BTTask> &p_task) const {
} }
int BTTask::get_child_index(const Ref<BTTask> &p_child) const { int BTTask::get_child_index(const Ref<BTTask> &p_child) const {
return children.find(p_child); return data.children.find(p_child);
} }
Ref<BTTask> BTTask::next_sibling() const { Ref<BTTask> BTTask::next_sibling() const {
if (parent != nullptr) { if (data.parent != nullptr) {
int idx = parent->get_child_index(Ref<BTTask>(this)); int idx = data.parent->get_child_index(Ref<BTTask>(this));
if (idx != -1 && parent->get_child_count() > (idx + 1)) { if (idx != -1 && data.parent->get_child_count() > (idx + 1)) {
return parent->get_child(idx + 1); return data.parent->get_child(idx + 1);
} }
} }
return Ref<BTTask>(); return Ref<BTTask>();
@ -317,18 +316,18 @@ void BTTask::_bind_methods() {
} }
BTTask::BTTask() { BTTask::BTTask() {
custom_name = String(); data.custom_name = String();
agent = nullptr; data.agent = nullptr;
parent = nullptr; data.parent = nullptr;
children = Vector<Ref<BTTask>>(); data.children = Vector<Ref<BTTask>>();
status = FRESH; data.status = FRESH;
elapsed = 0.0; data.elapsed = 0.0;
} }
BTTask::~BTTask() { BTTask::~BTTask() {
for (int i = 0; i < get_child_count(); i++) { for (int i = 0; i < get_child_count(); i++) {
ERR_FAIL_COND(!get_child(i).is_valid()); ERR_FAIL_COND(!get_child(i).is_valid());
get_child(i)->parent = nullptr; get_child(i)->data.parent = nullptr;
get_child(i).unref(); get_child(i).unref();
} }
} }

View File

@ -28,13 +28,16 @@ public:
private: private:
friend class BehaviorTree; friend class BehaviorTree;
String custom_name; // Avoid namespace pollution in derived classes.
Node *agent; struct Data {
Ref<Blackboard> blackboard; String custom_name;
BTTask *parent; Node *agent;
Vector<Ref<BTTask>> children; Ref<Blackboard> blackboard;
int status; BTTask *parent;
double elapsed; Vector<Ref<BTTask>> children;
int status;
double elapsed;
} data;
Array _get_children() const; Array _get_children() const;
void _set_children(Array children); void _set_children(Array children);
@ -58,17 +61,17 @@ protected:
public: public:
virtual bool editor_can_reload_from_file() override { return false; } virtual bool editor_can_reload_from_file() override { return false; }
Node *get_agent() const { return agent; } Node *get_agent() const { return data.agent; }
void set_agent(Node *p_agent) { agent = p_agent; } void set_agent(Node *p_agent) { data.agent = p_agent; }
String get_custom_name() const { return custom_name; } String get_custom_name() const { return data.custom_name; }
void set_custom_name(const String &p_name); void set_custom_name(const String &p_name);
String get_task_name() const; String get_task_name() const;
Ref<Blackboard> get_blackboard() const { return blackboard; } Ref<Blackboard> get_blackboard() const { return data.blackboard; }
Ref<BTTask> get_parent() const { return Ref<BTTask>(parent); } Ref<BTTask> get_parent() const { return Ref<BTTask>(data.parent); }
Ref<BTTask> get_root() const; Ref<BTTask> get_root() const;
bool is_root() const { return parent == nullptr; } bool is_root() const { return data.parent == nullptr; }
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);
@ -76,8 +79,8 @@ public:
int execute(double p_delta); int execute(double p_delta);
void cancel(); void cancel();
int get_status() const { return status; } int get_status() const { return data.status; }
double get_elapsed_time() const { return elapsed; }; double get_elapsed_time() const { return data.elapsed; };
Ref<BTTask> get_child(int p_idx) const; Ref<BTTask> get_child(int p_idx) const;
int get_child_count() const; int get_child_count() const;

View File

@ -115,9 +115,9 @@ void LimboDebuggerTab::_bt_selected(int p_idx) {
info_message->set_text(TTR("Waiting for behavior tree update.")); info_message->set_text(TTR("Waiting for behavior tree update."));
info_message->show(); info_message->show();
NodePath path = bt_player_list->get_item_text(p_idx); NodePath path = bt_player_list->get_item_text(p_idx);
Array data; Array msg_data;
data.push_back(path); msg_data.push_back(path);
session->send_message("limboai:track_bt_player", data); session->send_message("limboai:track_bt_player", msg_data);
} }
void LimboDebuggerTab::_filter_changed(String p_text) { void LimboDebuggerTab::_filter_changed(String p_text) {

View File

@ -846,10 +846,10 @@ void LimboAIEditor::_on_panel_task_selected(String p_task) {
Ref<BTTask> task; Ref<BTTask> task;
if (p_task.begins_with("res:")) { if (p_task.begins_with("res:")) {
Ref<Script> script = ResourceLoader::load(p_task, "Script"); Ref<Script> s = ResourceLoader::load(p_task, "Script");
ERR_FAIL_COND_MSG(script.is_null() || !script->is_valid(), vformat("LimboAI: Failed to instance task. Bad script: %s", p_task)); ERR_FAIL_COND_MSG(s.is_null() || !s->is_valid(), vformat("LimboAI: Failed to instance task. Bad script: %s", p_task));
Variant inst = ClassDB::instantiate(script->get_instance_base_type()); Variant inst = ClassDB::instantiate(s->get_instance_base_type());
ERR_FAIL_COND_MSG(inst.is_zero(), vformat("LimboAI: Failed to instance base type \"%s\".", script->get_instance_base_type())); ERR_FAIL_COND_MSG(inst.is_zero(), vformat("LimboAI: Failed to instance base type \"%s\".", s->get_instance_base_type()));
if (unlikely(!((Object *)inst)->is_class("BTTask"))) { if (unlikely(!((Object *)inst)->is_class("BTTask"))) {
if (!inst.is_ref_counted()) { if (!inst.is_ref_counted()) {
@ -859,8 +859,8 @@ void LimboAIEditor::_on_panel_task_selected(String p_task) {
return; return;
} }
if (inst && script.is_valid()) { if (inst && s.is_valid()) {
((Object *)inst)->set_script(script); ((Object *)inst)->set_script(s);
task = inst; task = inst;
} }
} else { } else {

View File

@ -46,8 +46,8 @@ Ref<Texture2D> LimboUtility::get_task_icon(String p_class_or_script_path) const
ERR_FAIL_COND_V_MSG(p_class_or_script_path.is_empty(), Variant(), "BTTask: script path or class cannot be empty."); ERR_FAIL_COND_V_MSG(p_class_or_script_path.is_empty(), Variant(), "BTTask: script path or class cannot be empty.");
if (p_class_or_script_path.begins_with("res:")) { if (p_class_or_script_path.begins_with("res:")) {
Ref<Script> script = ResourceLoader::load(p_class_or_script_path, "Script"); Ref<Script> s = ResourceLoader::load(p_class_or_script_path, "Script");
return EditorNode::get_singleton()->get_object_icon(script.ptr(), "BTTask"); return EditorNode::get_singleton()->get_object_icon(s.ptr(), "BTTask");
} }
// TODO: Walk inheritance tree until icon is found. // TODO: Walk inheritance tree until icon is found.
return EditorNode::get_singleton()->get_class_icon(p_class_or_script_path, "BTTask"); return EditorNode::get_singleton()->get_class_icon(p_class_or_script_path, "BTTask");