Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BlackboardSystem.cpp
Go to the documentation of this file.
1/**
2 * @file BlackboardSystem.cpp
3 * @brief Implementation of BlackboardSystem (Phase 2.1)
4 * @author Olympe Engine
5 * @date 2026-02-19
6 */
7
8#include "BlackboardSystem.h"
9#include "../system/system_utils.h"
10
11namespace Olympe {
12namespace NodeGraph {
13
14// ============================================================================
15// Query
16// ============================================================================
17
18bool BlackboardSystem::HasEntry(const std::string& name) const
19{
20 return m_entries.find(name) != m_entries.end();
21}
22
23const BlackboardValue* BlackboardSystem::GetEntry(const std::string& name) const
24{
25 auto it = m_entries.find(name);
26 if (it != m_entries.end())
27 {
28 return &it->second;
29 }
30 return nullptr;
31}
32
33// ============================================================================
34// CRUD
35// ============================================================================
36
37bool BlackboardSystem::CreateEntry(const std::string& name, BlackboardType type,
39{
40 if (name.empty())
41 {
42 SYSTEM_LOG << "[BlackboardSystem] CreateEntry failed: empty name" << std::endl;
43 return false;
44 }
45
46 if (HasEntry(name))
47 {
48 SYSTEM_LOG << "[BlackboardSystem] CreateEntry failed: duplicate name '"
49 << name << "'" << std::endl;
50 return false;
51 }
52
54 val.type = type;
55 m_entries[name] = val;
56 SYSTEM_LOG << "[BlackboardSystem] Created entry '" << name
57 << "' type=" << TypeToString(type) << std::endl;
58 return true;
59}
60
61bool BlackboardSystem::RemoveEntry(const std::string& name)
62{
63 auto it = m_entries.find(name);
64 if (it == m_entries.end())
65 {
66 SYSTEM_LOG << "[BlackboardSystem] RemoveEntry: not found '" << name << "'" << std::endl;
67 return false;
68 }
69 m_entries.erase(it);
70 SYSTEM_LOG << "[BlackboardSystem] Removed entry '" << name << "'" << std::endl;
71 return true;
72}
73
74bool BlackboardSystem::RenameEntry(const std::string& oldName, const std::string& newName)
75{
76 if (newName.empty())
77 {
78 SYSTEM_LOG << "[BlackboardSystem] RenameEntry failed: empty new name" << std::endl;
79 return false;
80 }
81
82 auto it = m_entries.find(oldName);
83 if (it == m_entries.end())
84 {
85 SYSTEM_LOG << "[BlackboardSystem] RenameEntry: source not found '" << oldName << "'" << std::endl;
86 return false;
87 }
88
89 if (HasEntry(newName))
90 {
91 SYSTEM_LOG << "[BlackboardSystem] RenameEntry failed: target name already exists '"
92 << newName << "'" << std::endl;
93 return false;
94 }
95
96 BlackboardValue val = it->second;
97 m_entries.erase(it);
99 SYSTEM_LOG << "[BlackboardSystem] Renamed '" << oldName << "' -> '" << newName << "'" << std::endl;
100 return true;
101}
102
103bool BlackboardSystem::SetValue(const std::string& name, const BlackboardValue& value)
104{
105 auto it = m_entries.find(name);
106 if (it == m_entries.end())
107 {
108 SYSTEM_LOG << "[BlackboardSystem] SetValue: not found '" << name << "'" << std::endl;
109 return false;
110 }
111
112 if (it->second.type != value.type)
113 {
114 SYSTEM_LOG << "[BlackboardSystem] SetValue: type mismatch for '" << name << "'" << std::endl;
115 return false;
116 }
117
118 it->second = value;
119 return true;
120}
121
122// ============================================================================
123// Iteration
124// ============================================================================
125
126const std::map<std::string, BlackboardValue>& BlackboardSystem::GetAll() const
127{
128 return m_entries;
129}
130
131// ============================================================================
132// Serialization
133// ============================================================================
134
136{
137 json arr = json::array();
138 for (auto it = m_entries.begin(); it != m_entries.end(); ++it)
139 {
140 const std::string& name = it->first;
141 const BlackboardValue& val = it->second;
142
143 json entry = json::object();
144 entry["name"] = name;
145 entry["type"] = TypeToString(val.type);
146
147 json valObj = json::object();
148 switch (val.type)
149 {
150 case BlackboardType::Int:
151 valObj["int"] = val.intValue;
152 break;
153 case BlackboardType::Float:
154 valObj["float"] = val.floatValue;
155 break;
156 case BlackboardType::Bool:
157 valObj["bool"] = val.boolValue;
158 break;
159 case BlackboardType::String:
160 valObj["string"] = val.stringValue;
161 break;
162 case BlackboardType::Vector3:
163 valObj["x"] = val.vec3X;
164 valObj["y"] = val.vec3Y;
165 valObj["z"] = val.vec3Z;
166 break;
167 default:
168 break;
169 }
170 entry["value"] = valObj;
171 arr.push_back(entry);
172 }
173 return arr;
174}
175
177{
178 m_entries.clear();
179
180 if (!j.is_array())
181 {
182 return;
183 }
184
185 for (size_t i = 0; i < j.size(); ++i)
186 {
187 const json& entry = j[i];
188 if (!entry.is_object())
189 {
190 continue;
191 }
192
193 std::string name = JsonHelper::GetString(entry, "name", "");
194 if (name.empty())
195 {
196 continue;
197 }
198
199 std::string typeStr = JsonHelper::GetString(entry, "type", "Int");
200 BlackboardType type = StringToType(typeStr);
201
203 val.type = type;
204
205 if (entry.contains("value") && entry["value"].is_object())
206 {
207 const json& v = entry["value"];
208 switch (type)
209 {
210 case BlackboardType::Int:
211 val.intValue = JsonHelper::GetInt(v, "int", 0);
212 break;
213 case BlackboardType::Float:
214 val.floatValue = JsonHelper::GetFloat(v, "float", 0.0f);
215 break;
216 case BlackboardType::Bool:
217 val.boolValue = JsonHelper::GetBool(v, "bool", false);
218 break;
219 case BlackboardType::String:
220 val.stringValue = JsonHelper::GetString(v, "string", "");
221 break;
222 case BlackboardType::Vector3:
223 val.vec3X = JsonHelper::GetFloat(v, "x", 0.0f);
224 val.vec3Y = JsonHelper::GetFloat(v, "y", 0.0f);
225 val.vec3Z = JsonHelper::GetFloat(v, "z", 0.0f);
226 break;
227 default:
228 break;
229 }
230 }
231
232 m_entries[name] = val;
233 }
234}
235
236// ============================================================================
237// Private helpers
238// ============================================================================
239
240std::string BlackboardSystem::TypeToString(BlackboardType t)
241{
242 switch (t)
243 {
244 case BlackboardType::Int: return "Int";
245 case BlackboardType::Float: return "Float";
246 case BlackboardType::Bool: return "Bool";
247 case BlackboardType::String: return "String";
248 case BlackboardType::Vector3: return "Vector3";
249 default: return "Int";
250 }
251}
252
253BlackboardType BlackboardSystem::StringToType(const std::string& s)
254{
255 if (s == "Float") return BlackboardType::Float;
256 if (s == "Bool") return BlackboardType::Bool;
257 if (s == "String") return BlackboardType::String;
258 if (s == "Vector3") return BlackboardType::Vector3;
259 return BlackboardType::Int;
260}
261
262} // namespace NodeGraph
263} // namespace Olympe
Blackboard system for shared graph variables (Phase 2.1)
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
bool CreateEntry(const std::string &name, BlackboardType type, const BlackboardValue &initialValue)
Create a new entry.
bool RenameEntry(const std::string &oldName, const std::string &newName)
Rename an entry.
bool RemoveEntry(const std::string &name)
Remove an entry.
const std::map< std::string, BlackboardValue > & GetAll() const
Get all entries (for UI / serialization)
bool SetValue(const std::string &name, const BlackboardValue &value)
Update the value of an existing entry.
std::map< std::string, BlackboardValue > m_entries
const BlackboardValue * GetEntry(const std::string &name) const
Get a const pointer to an entry.
static BlackboardType StringToType(const std::string &s)
Convert a string to BlackboardType (returns Int on unknown)
json ToJson() const
Serialize blackboard to JSON.
void FromJson(const json &j)
Deserialize blackboard from JSON.
bool HasEntry(const std::string &name) const
Check if an entry exists.
static std::string TypeToString(BlackboardType t)
Convert a BlackboardType to its string representation.
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
int GetInt(const json &j, const std::string &key, int defaultValue=0)
Safely get an integer value from JSON.
float GetFloat(const json &j, const std::string &key, float defaultValue=0.0f)
Safely get a float value from JSON.
bool GetBool(const json &j, const std::string &key, bool defaultValue=false)
Safely get a boolean value from JSON.
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
Stores a typed value for a blackboard entry.
#define SYSTEM_LOG