VEGO-Engine  0.1
Loading...
Searching...
No Matches
DataComponent.h
1#pragma once
2
3#include <map>
4#include <any>
5#include <string>
6#include <optional>
7#include "Component.h"
8
10class DataComponent : public Component
11{
12public:
15 ~DataComponent() {};
22 template <typename T>
23 void setEntry(const std::string& key, const T& value) { dataMap.insert_or_assign(key, value); }
24
32 template<typename T>
33 std::optional<T> getEntry(std::string key) const {
34 if (!this->dataMap.contains(key)) return std::nullopt;
35 const std::any& value = this->dataMap.at(key);
36 if (value.type() != typeid(T)) { return std::nullopt; }
37 return std::any_cast<T>(value);
38 }
39private:
40 std::map<std::string, std::any> dataMap;
41};
DataComponent class to centrally store data about an entity such as stats.
Definition DataComponent.h:11
DataComponent()
The data component only has a default constructor.
Definition DataComponent.h:14
void setEntry(const std::string &key, const T &value)
Set a key-value pair of any type in the data map.
Definition DataComponent.h:23
std::optional< T > getEntry(std::string key) const
Get a value of type T from the data map.
Definition DataComponent.h:33