Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GlobalBlackboard.h
Go to the documentation of this file.
1/**
2 * @file GlobalBlackboard.h
3 * @brief Singleton GlobalBlackboard for shared TaskValue variables across VS graphs.
4 * @author Olympe Engine
5 * @date 2026-03-08
6 *
7 * @details
8 * GlobalBlackboard provides a process-wide key/value store using TaskValue.
9 * It is accessible from any VS graph node via the "global:" scope prefix in
10 * LocalBlackboard::GetValueScoped / SetValueScoped.
11 *
12 * The singleton is implemented with the Meyers pattern (C++14 compliant).
13 * The header-only implementation avoids ODR issues when included in multiple
14 * translation units; the singleton local static is guaranteed to be initialized
15 * exactly once by the C++11/14 standard.
16 *
17 * JSON persistence is provided as a separate opt-in (SaveToJson / LoadFromJson)
18 * and is not required for basic runtime operation.
19 *
20 * C++14 compliant - no C++17/20 features.
21 */
22
23#pragma once
24
25#include <string>
26#include <unordered_map>
27
28#include "../TaskSystem/TaskGraphTypes.h"
29#include "../system/system_utils.h"
30
31namespace Olympe {
32
33/**
34 * @class GlobalBlackboard
35 * @brief Process-wide singleton blackboard storing TaskValue entries.
36 *
37 * @details
38 * All VS graph nodes sharing the same process can read and write
39 * GlobalBlackboard variables using the "global:" scope prefix.
40 *
41 * Example:
42 * @code
43 * // Write:
44 * GlobalBlackboard::Get().SetVar("GamePhase", TaskValue(std::string("Combat")));
45 *
46 * // Read:
47 * TaskValue v = GlobalBlackboard::Get().GetVar("GamePhase");
48 * @endcode
49 */
51public:
52
53 /**
54 * @brief Returns the singleton instance.
55 * @return Reference to the single GlobalBlackboard instance.
56 */
58 {
60 return instance;
61 }
62
63 /**
64 * @brief Reads a variable by key.
65 *
66 * @param key Variable name (without "global:" prefix).
67 * @return Stored TaskValue, or default TaskValue() (None) if not found.
68 */
69 TaskValue GetVar(const std::string& key) const
70 {
71 auto it = m_store.find(key);
72 if (it == m_store.end())
73 {
74 return TaskValue();
75 }
76 return it->second;
77 }
78
79 /**
80 * @brief Writes or creates a variable.
81 *
82 * @param key Variable name (without "global:" prefix).
83 * @param value Value to store.
84 */
85 void SetVar(const std::string& key, const TaskValue& value)
86 {
87 m_store[key] = value;
88 m_dirty = true;
89 }
90
91 /**
92 * @brief Returns true if any variable has been modified since the last ClearDirty().
93 */
94 bool IsDirty() const { return m_dirty; }
95
96 /**
97 * @brief Marks the blackboard as modified (called automatically by SetVar).
98 */
99 void MarkDirty() { m_dirty = true; }
100
101 /**
102 * @brief Clears the dirty flag (call after persisting).
103 */
104 void ClearDirty() { m_dirty = false; }
105
106 /**
107 * @brief Checks whether a variable exists.
108 * @param key Variable name (without "global:" prefix).
109 * @return true if the variable exists.
110 */
111 bool HasVar(const std::string& key) const
112 {
113 return m_store.find(key) != m_store.end();
114 }
115
116 /**
117 * @brief Removes all variables (useful in tests / level transitions).
118 */
119 void Clear()
120 {
121 m_store.clear();
122 m_dirty = false;
123 }
124
125private:
126 GlobalBlackboard() = default;
127 ~GlobalBlackboard() = default;
128
129 // Non-copyable, non-movable.
134
135 std::unordered_map<std::string, TaskValue> m_store;
136 bool m_dirty = false;
137};
138
139} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Process-wide singleton blackboard storing TaskValue entries.
void MarkDirty()
Marks the blackboard as modified (called automatically by SetVar).
std::unordered_map< std::string, TaskValue > m_store
void SetVar(const std::string &key, const TaskValue &value)
Writes or creates a variable.
GlobalBlackboard(GlobalBlackboard &&)=delete
static GlobalBlackboard & Get()
Returns the singleton instance.
GlobalBlackboard & operator=(GlobalBlackboard &&)=delete
GlobalBlackboard & operator=(const GlobalBlackboard &)=delete
bool IsDirty() const
Returns true if any variable has been modified since the last ClearDirty().
GlobalBlackboard(const GlobalBlackboard &)=delete
void ClearDirty()
Clears the dirty flag (call after persisting).
bool HasVar(const std::string &key) const
Checks whether a variable exists.
TaskValue GetVar(const std::string &key) const
Reads a variable by key.
void Clear()
Removes all variables (useful in tests / level transitions).
C++14-compliant type-safe value container for task parameters.
< Provides AssetID and INVALID_ASSET_ID