Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BBVariableRegistry.cpp
Go to the documentation of this file.
1/**
2 * @file BBVariableRegistry.cpp
3 * @brief Implementation of BBVariableRegistry.
4 * @author Olympe Engine
5 * @date 2026-03-14
6 *
7 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
8 */
9
10#include "BBVariableRegistry.h"
11#include "../NodeGraphCore/GlobalTemplateBlackboard.h"
12
13#include <algorithm>
14
15namespace Olympe {
16
17// ---------------------------------------------------------------------------
18// Helpers
19// ---------------------------------------------------------------------------
20
21static const char* VariableTypeName(VariableType t)
22{
23 switch (t)
24 {
25 case VariableType::Bool: return "Bool";
26 case VariableType::Int: return "Int";
27 case VariableType::Float: return "Float";
28 case VariableType::Vector: return "Vector";
29 case VariableType::EntityID: return "EntityID";
30 case VariableType::String: return "String";
31 case VariableType::List: return "List";
32 case VariableType::GlobalRef: return "GlobalRef";
33 default: return "None";
34 }
35}
36
37// ---------------------------------------------------------------------------
38// BBVariableRegistry
39// ---------------------------------------------------------------------------
40
42{
43 m_vars.clear();
44 m_vars.reserve(tmpl.Blackboard.size());
45
46 // Load local and global variables from the template
47 for (size_t i = 0; i < tmpl.Blackboard.size(); ++i)
48 {
49 const BlackboardEntry& entry = tmpl.Blackboard[i];
50 if (entry.Key.empty())
51 continue;
52
54 spec.name = entry.Key;
55 spec.type = entry.Type;
56 spec.isGlobal = entry.IsGlobal;
57 spec.displayLabel = FormatDisplayLabel(entry.Key, entry.Type, entry.IsGlobal);
58 m_vars.push_back(spec);
59 }
60
61 // Phase 24: Also load global variables from GlobalTemplateBlackboard registry
63 const std::vector<GlobalEntryDefinition>& globalVars = gtb.GetAllVariables();
64
65 for (const auto& globalEntry : globalVars)
66 {
67 // Check if this global variable is already in the template (avoid duplicates)
68 bool alreadyExists = false;
69 for (const auto& existing : m_vars)
70 {
71 if (existing.name == globalEntry.Key && existing.isGlobal)
72 {
73 alreadyExists = true;
74 break;
75 }
76 }
77
78 if (!alreadyExists)
79 {
81 spec.name = globalEntry.Key;
82 spec.type = globalEntry.Type;
83 spec.isGlobal = true;
84 spec.displayLabel = FormatDisplayLabel(globalEntry.Key, globalEntry.Type, true);
85 m_vars.push_back(spec);
86 }
87 }
88
89 // Sort by name
90 std::sort(m_vars.begin(), m_vars.end(),
91 [](const VarSpec& a, const VarSpec& b) {
92 return a.name < b.name;
93 });
94}
95
96const std::vector<VarSpec>& BBVariableRegistry::GetAllVariables() const
97{
98 return m_vars;
99}
100
102{
103 std::vector<VarSpec> result;
104 for (size_t i = 0; i < m_vars.size(); ++i)
105 {
106 if (m_vars[i].type == type)
107 result.push_back(m_vars[i]);
108 }
109 return result;
110}
111
112std::vector<VarSpec> BBVariableRegistry::GetLocalVariables() const
113{
114 std::vector<VarSpec> result;
115 for (size_t i = 0; i < m_vars.size(); ++i)
116 {
117 if (!m_vars[i].isGlobal)
118 result.push_back(m_vars[i]);
119 }
120 return result;
121}
122
123std::vector<VarSpec> BBVariableRegistry::GetGlobalVariables() const
124{
125 std::vector<VarSpec> result;
126 for (size_t i = 0; i < m_vars.size(); ++i)
127 {
128 if (m_vars[i].isGlobal)
129 result.push_back(m_vars[i]);
130 }
131 return result;
132}
133
134/*static*/
135std::string BBVariableRegistry::FormatDisplayLabel(const std::string& name,
136 VariableType type,
137 bool isGlobal)
138{
139 return name + " (" + VariableTypeName(type) + ", " +
140 (isGlobal ? "global" : "local") + ")";
141}
142
143bool BBVariableRegistry::HasVariable(const std::string& name) const
144{
145 for (size_t i = 0; i < m_vars.size(); ++i)
146 {
147 if (m_vars[i].name == name)
148 return true;
149 }
150 return false;
151}
152
154{
155 return m_vars.size();
156}
157
158} // namespace Olympe
Wrapper around the graph blackboard entries for dropdown editors.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::vector< VarSpec > GetVariablesByType(VariableType type) const
Returns variables whose type matches type, sorted by name.
std::vector< VarSpec > m_vars
std::vector< VarSpec > GetGlobalVariables() const
Returns only global-scope variables.
void LoadFromTemplate(const TaskGraphTemplate &tmpl)
Rebuilds the registry from the blackboard entries of a template.
static std::string FormatDisplayLabel(const std::string &name, VariableType type, bool isGlobal)
Formats a display label for a variable.
const std::vector< VarSpec > & GetAllVariables() const
Returns all variables (local and global), sorted by name.
size_t GetCount() const
Returns the number of registered variables.
bool HasVariable(const std::string &name) const
Returns true if a variable with the given name is registered.
std::vector< VarSpec > GetLocalVariables() const
Returns only local-scope variables.
static GlobalTemplateBlackboard & Get()
Immutable, shareable task graph asset.
< Provides AssetID and INVALID_ASSET_ID
VariableType
Type tags used by TaskValue to identify stored data.
@ Int
32-bit signed integer
@ Float
Single-precision float.
@ String
std::string
@ GlobalRef
Reference to a global blackboard key (scope "global:")
@ List
std::vector<TaskValue> (used by ForEach node)
@ Vector
3-component vector (Vector from vector.h)
@ EntityID
Entity identifier (uint64_t)
static const char * VariableTypeName(VariableType t)
Single entry in the graph's declared blackboard schema (local or global).
Metadata for a single blackboard variable entry.
std::string name
Variable key (e.g. "health", "target")