Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
LocalBlackboard.h
Go to the documentation of this file.
1/**
2 * @file LocalBlackboard.h
3 * @brief Runtime key-value store for task graph variables
4 * @author Olympe Engine
5 * @date 2026-02-20
6 *
7 * @details
8 * LocalBlackboard provides a simple unordered_map-based storage for TaskValue
9 * variables. Each TaskRunner instance owns one LocalBlackboard that is
10 * initialised from a TaskGraphTemplate (which provides variable names, types
11 * and default values) and can be reset to those defaults at any time.
12 *
13 * C++14 compliant - no std::variant, std::optional, or C++17/20 features.
14 */
15
16#pragma once
17
18#include <string>
19#include <unordered_map>
20#include <vector>
21#include <stdexcept>
22
23#include "TaskGraphTypes.h"
24#include "TaskGraphTemplate.h"
25
26namespace Olympe {
27
28/**
29 * @class LocalBlackboard
30 * @brief Simple map-based blackboard for task graph runtime state.
31 *
32 * @details
33 * Variables must be registered via Initialize() before use. SetValue()
34 * enforces type compatibility: the new value must match the declared type of
35 * the variable. Throws std::runtime_error on unknown variable or type mismatch.
36 */
38public:
39
40 /**
41 * @brief Default constructor. Blackboard is empty until Initialize() is called.
42 */
44
45 // -----------------------------------------------------------------------
46 // Lifecycle
47 // -----------------------------------------------------------------------
48
49 /**
50 * @brief Initialises the blackboard from a template.
51 *
52 * Registers all local variables defined in @p tmpl, copying their types
53 * and default values. Any previous state is discarded.
54 *
55 * @param tmpl The TaskGraphTemplate that owns this blackboard's schema.
56 */
58
59 /**
60 * @brief Resets all variables to their default values.
61 *
62 * Does not change the set of registered variables.
63 */
64 void Reset();
65
66 // -----------------------------------------------------------------------
67 // Variable access
68 // -----------------------------------------------------------------------
69
70 /**
71 * @brief Returns the current value of a variable.
72 * @param varName Name of the variable.
73 * @throws std::runtime_error if the variable is not registered.
74 */
75 TaskValue GetValue(const std::string& varName) const;
76
77 /**
78 * @brief Sets the value of a variable.
79 *
80 * The type of @p value must match the declared type of the variable.
81 *
82 * @param varName Name of the variable.
83 * @param value New value.
84 * @throws std::runtime_error if the variable is not registered.
85 * @throws std::runtime_error if the type of @p value does not match the
86 * declared type of the variable.
87 */
88 void SetValue(const std::string& varName, const TaskValue& value);
89
90 // -----------------------------------------------------------------------
91 // Queries
92 // -----------------------------------------------------------------------
93
94 /**
95 * @brief Returns true if a variable with the given name is registered.
96 * @param varName Name of the variable.
97 */
98 bool HasVariable(const std::string& varName) const;
99
100 /**
101 * @brief Returns all registered variable names (useful for debugging / editor).
102 */
103 std::vector<std::string> GetVariableNames() const;
104
105 // -----------------------------------------------------------------------
106 // Persistence
107 // -----------------------------------------------------------------------
108
109 /**
110 * @brief Serializes all variable names and typed values into a byte buffer.
111 *
112 * Binary format (little-endian):
113 * uint32_t count - number of variables
114 * for each variable:
115 * uint32_t nameLen - length of name in bytes
116 * <nameLen bytes> - variable name (UTF-8)
117 * uint8_t type - VariableType tag
118 * <value bytes>:
119 * Bool -> uint8_t (0 = false, 1 = true)
120 * Int -> int32_t
121 * Float -> float
122 * Vector -> float x, float y, float z
123 * EntityID-> uint64_t
124 * String -> uint32_t len, <len bytes>
125 *
126 * @param outBytes Output byte buffer (cleared before writing).
127 */
128 void Serialize(std::vector<uint8_t>& outBytes) const;
129
130 /**
131 * @brief Restores variable values from a byte buffer produced by Serialize().
132 *
133 * The blackboard schema (m_types) must already be initialised via
134 * Initialize() before calling this method. Unknown variable names and
135 * type-mismatched entries are skipped with a warning log. The buffer
136 * is consumed in one pass; parsing stops on any truncation error.
137 *
138 * @param inBytes Byte buffer previously produced by Serialize().
139 */
140 void Deserialize(const std::vector<uint8_t>& inBytes);
141
142 // -----------------------------------------------------------------------
143 // ATS Visual Scripting extensions (Phase 2)
144 // -----------------------------------------------------------------------
145
146 /**
147 * @brief Initializes the blackboard from a vector of BlackboardEntry (ATS VS schema v4).
148 *
149 * Companion to Initialize(const TaskGraphTemplate&) for VS graphs that use
150 * the BlackboardEntry schema instead of VariableDefinition.
151 *
152 * @param entries Blackboard entries from TaskGraphTemplate::Blackboard.
153 */
154 void InitializeFromEntries(const std::vector<BlackboardEntry>& entries);
155
156 /**
157 * @brief Sets a value using a scoped key (prefix "local:" is stripped).
158 * Forwards "global:" scope to GlobalBlackboard singleton.
159 * @param scopedKey Key with optional "local:" or "global:" prefix.
160 * @param value Value to set.
161 */
162 void SetValueScoped(const std::string& scopedKey, const TaskValue& value);
163
164 /**
165 * @brief Gets a value using a scoped key (prefix "local:" is stripped).
166 * Forwards "global:" scope to GlobalBlackboard singleton.
167 * @param scopedKey Key with optional prefix.
168 * @return Stored value or default TaskValue() if not found.
169 */
170 TaskValue GetValueScoped(const std::string& scopedKey) const;
171
172private:
173
174 /// Current values for each registered variable.
175 std::unordered_map<std::string, TaskValue> m_variables;
176
177 /// Default (initial) values used by Reset().
178 std::unordered_map<std::string, TaskValue> m_defaults;
179
180 /// Declared type of each variable (used for type validation in SetValue).
181 std::unordered_map<std::string, VariableType> m_types;
182};
183
184} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Immutable asset structure shared by all task graph runners.
Core enumerations and TaskValue type-safe variant for the Atomic Task System.
Simple map-based blackboard for task graph runtime state.
void Serialize(std::vector< uint8_t > &outBytes) const
Serializes all variable names and typed values into a byte buffer.
std::unordered_map< std::string, VariableType > m_types
Declared type of each variable (used for type validation in SetValue).
TaskValue GetValueScoped(const std::string &scopedKey) const
Gets a value using a scoped key (prefix "local:" is stripped).
void Deserialize(const std::vector< uint8_t > &inBytes)
Restores variable values from a byte buffer produced by Serialize().
void Reset()
Resets all variables to their default values.
void Initialize(const TaskGraphTemplate &tmpl)
Initialises the blackboard from a template.
std::unordered_map< std::string, TaskValue > m_defaults
Default (initial) values used by Reset().
bool HasVariable(const std::string &varName) const
Returns true if a variable with the given name is registered.
void SetValue(const std::string &varName, const TaskValue &value)
Sets the value of a variable.
TaskValue GetValue(const std::string &varName) const
Returns the current value of a variable.
void SetValueScoped(const std::string &scopedKey, const TaskValue &value)
Sets a value using a scoped key (prefix "local:" is stripped).
std::unordered_map< std::string, TaskValue > m_variables
Current values for each registered variable.
std::vector< std::string > GetVariableNames() const
Returns all registered variable names (useful for debugging / editor).
LocalBlackboard()
Default constructor.
void InitializeFromEntries(const std::vector< BlackboardEntry > &entries)
Initializes the blackboard from a vector of BlackboardEntry (ATS VS schema v4).
Immutable, shareable task graph asset.
C++14-compliant type-safe value container for task parameters.
< Provides AssetID and INVALID_ASSET_ID