Skip to content

Commit a952009

Browse files
authored
Add Blackboard.print_state() (#264)
Method that prints values of all variables in each scope.
1 parent 0982804 commit a952009

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

blackboard/blackboard.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include "blackboard.h"
13+
#include "../util/limbo_compat.h"
1314

1415
#ifdef LIMBOAI_MODULE
1516
#include "core/variant/variant.h"
@@ -75,6 +76,26 @@ TypedArray<StringName> Blackboard::list_vars() const {
7576
return var_names;
7677
}
7778

79+
void Blackboard::print_state() const {
80+
Ref<Blackboard> bb{ this };
81+
int scope_idx = 0;
82+
while (bb.is_valid()) {
83+
int i = 0;
84+
String line = "Scope " + itos(scope_idx) + ": { ";
85+
for (const KeyValue<StringName, BBVariable> &kv : bb->data) {
86+
if (i > 0) {
87+
line += ", ";
88+
}
89+
line += String(kv.key) + ": " + String(kv.value.get_value());
90+
i++;
91+
}
92+
line += " }";
93+
PRINT_LINE(line);
94+
bb = bb->get_parent();
95+
scope_idx++;
96+
}
97+
}
98+
7899
Dictionary Blackboard::get_vars_as_dict() const {
79100
Dictionary dict;
80101
for (const KeyValue<StringName, BBVariable> &kv : data) {
@@ -136,6 +157,7 @@ void Blackboard::_bind_methods() {
136157
ClassDB::bind_method(D_METHOD("erase_var", "var_name"), &Blackboard::erase_var);
137158
ClassDB::bind_method(D_METHOD("clear"), &Blackboard::clear);
138159
ClassDB::bind_method(D_METHOD("list_vars"), &Blackboard::list_vars);
160+
ClassDB::bind_method(D_METHOD("print_state"), &Blackboard::print_state);
139161
ClassDB::bind_method(D_METHOD("get_vars_as_dict"), &Blackboard::get_vars_as_dict);
140162
ClassDB::bind_method(D_METHOD("populate_from_dict", "dictionary"), &Blackboard::populate_from_dict);
141163
ClassDB::bind_method(D_METHOD("top"), &Blackboard::top);

blackboard/blackboard.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Blackboard : public RefCounted {
5757
void erase_var(const StringName &p_name);
5858
void clear() { data.clear(); }
5959
TypedArray<StringName> list_vars() const;
60+
void print_state() const;
6061

6162
Dictionary get_vars_as_dict() const;
6263
void populate_from_dict(const Dictionary &p_dictionary);

doc_classes/Blackboard.xml

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
Fills the Blackboard with multiple variables from a dictionary. The dictionary keys must be variable names and the dictionary values must be variable values. Keys must be StringName or String.
8787
</description>
8888
</method>
89+
<method name="print_state" qualifiers="const">
90+
<return type="void" />
91+
<description>
92+
Prints the values of all variables in each scope.
93+
</description>
94+
</method>
8995
<method name="set_parent">
9096
<return type="void" />
9197
<param index="0" name="blackboard" type="Blackboard" />

0 commit comments

Comments
 (0)