title: How could you recreate UE4 serialization of user-defined types?
body: <h1>The problem</h1>
  <p>The Unreal Engine 4 Editor allows you to add objects of your own types to the scene.<br />
  Doing so requires minimal work from the user - to make a class visible in the editor you only need to add some macros, like UCLASS()</p>
  <pre><code>UCLASS()
  class MyInputComponent: public UInputComponent //you can instantiate it in the editor!
  {
       UPROPERTY(EditAnywhere)
       bool IsSomethingEnabled;
  };
  </code></pre>
  <p>This is enough to allow the editor to serialize the created-in-editor object's data (remember: the class is user-defined but the user doesn't have to hardcode loading specific fields. Also note that the UPROPERTY variable can be of user-defined type as well). It is then deserialized while loading the actual game. So how is it handled so painlessly?</p>
  <h2>My attempt - hardcoded loading for every new class</h2>
  <pre><code>class Component //abstract class
  {
  public:
      virtual void LoadFromStream(std::stringstream&amp; str) = 0;
      //virtual void SaveIntoStream(std::stringstream&amp; str) = 0;
  };
  class UserCreatedComponent: public Component
  {
      std::string Name;
      int SomeInteger;
      vec3 SomeVector; //example of user-defined type
  public:
      virtual void LoadFromStream(std::stringstream&amp; str) override //you have to write a function like this every time you create a new class
      {
          str &gt;&gt; Name &gt;&gt; SomeInteger &gt;&gt; SomeVector.x &gt;&gt; SomeVector.y &gt;&gt; SomeVector.z;
      }
  };
  
  std::vector&lt;Component*&gt; ComponentsFromStream(std::stringstream&amp; str)
  {
      std::vector&lt;Component*&gt; components;
      std::string type;
      while (str &gt;&gt; type)
      {
          if (type == &quot;UserCreatedComponent&quot;) //do this for every user-defined type...
              components.push_back(new UserCreatedComponent);
          else
              continue;
  
          components.back()-&gt;LoadFromStream(str);
      }
      
      return components;
  }
  </code></pre>
  <p>Example of an UserCreatedComponent object stream representation:</p>
  <pre><code>UserCreatedComponent MyComponent 5 0.707 0.707 0.707
  </code></pre>
  <p>The engine user has to do these things every time he creates a new class:<br />
  <strong>1. Modify ComponentsFromStream by adding another if<br />
  2. Add two methods, one which loads from stream and another which saves to stream.<br />
  We want to simplify it so the user only has to use a macro like UPROPERTY.</strong></p>
  <p>Our goal is to free the user from all this work and create a more extensible solution, like UE4's (described above).</p>
  <h3>Attempt at simplifying 1: Using type-int mapping</h3>
  <p><em>This section is based on the following:</em> <a href="https://stackoverflow.com/a/17409442/12703830">https://stackoverflow.com/a/17409442/12703830</a><br />
  The idea is that for every new class we map an integer, so when we create an object we can just pass the integer given in the stream to the factory.<br />
  Example of an UserCreatedComponent object stream representation:</p>
  <pre><code>1 MyComponent 5 0.707 0.707 0.707  
  </code></pre>
  <p>This solves the problem of working out the type of created object but also seems to create two new problems:</p>
  <ul>
  <li>How should we map classes to integers? What would happen if we include two libraries containing classes that map themselves to the same number?</li>
  <li>What will initializing e.g. components that need vectors for construction look like? We don't always use <em>string</em>s and <em>int</em>s for object construction (and streams give us pretty much only that).</li>
  </ul>