title: Creating data driven structs in c++?
body: <p>I'm making a Entity Component System for a game.
  I implemented my components as structs with member variables:</p>
  
  <pre><code>struct Position
  {
     float x;
     float y;
  }
  
  struct Render
  {
     unsigned int texture;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>But here's a problem:
  I want to enable players to create mods that add new component types after the game is already compiled?</p>
  
  <p>For example, let's say that the modder supplies a file which defines a component, the file contains something like this:</p>
  
  <pre><code>position
  float x
  float y
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>struct Component
  {
     string type;
     map&lt;string, int&gt; ints;
     map&lt;string, float&gt; floats;
  }
  </code></pre>
  
  <p>Then the game would read a file that defines a component and add the variables to the maps.
  Now my questions are:</p>
  
  <ul>
  <li>Is this a good idea?</li>
  <li>Since I access these variables every frame, would the access get any slower because of the maps?</li>
  <li>What would be the best solution to this problem?</li>
  </ul>
  
  <p>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:</p>
  
  <pre><code>struct Health
  {
     float max_hp;
     float hp;
  }
  </code></pre>
  
  <p>I would write a file that contains:</p>
  
  <pre><code>health
  float max_hp
  float hp
  </code></pre>
  
  <p>and have the game read the class definition from that.</p>