I'm making a Entity Component System for a game. I implemented my components as structs with member variables:
struct Position
{
float x;
float y;
}
struct Render
{
unsigned int texture;
}
That's all fine, and as simple as it can get, and if I want to add a new component type I would have to create a new struct and add it to the code.
But here's a problem: I want to enable players to create mods that add new component types after the game is already compiled?
For example, let's say that the modder supplies a file which defines a component, the file contains something like this:
position
float x
float y
One way I could do that is to make every component have a map that maps a string (variable name) with its value. I would have one map for ints, one map for floats, and so on. Then the component would look something like this:
struct Component
{
string type;
map<string, int> ints;
map<string, float> floats;
}
Then the game would read a file that defines a component and add the variables to the maps. Now my questions are:
- Is this a good idea?
- Since I access these variables every frame, would the access get any slower because of the maps?
- What would be the best solution to this problem?
EDIT: I should point out that the files define entirely new type of struct, so instead of adding manually something like the following in the code:
struct Health
{
float max_hp;
float hp;
}
I would write a file that contains:
health
float max_hp
float hp
and have the game read the class definition from that.