0

I'm trying to write my own game engine and I want the user to be able to use custom POD components. In order to save them in a scene file I need to find a way to (de)serialize them. Unity and Godot probably handle this with reflection, while I believe Unreal "simulates reflection" through code generation, which is probably too much for my project. Should I just stick to (de)serialize only map data (like static meshes and light sources), "hardcoding" every scene in a different way?

Edit: I need to be able to deserialize user-defined types; this means that my engine might be unaware of a Transform component. However, if it's serialized in a YAML or JSON format, I need a function that handles that string and populates the scene accordingly. I've thought about a map whose keys are component names (as strings) and values are pointers to deserialize functions, but how would the user intuitively register those functions?

prt_mhl
  • 13
  • 4
  • Perhaps you could use a third-party serialization library to help you solve this? – Some programmer dude Jul 06 '22 at 13:29
  • I would recommend using a library, serialization is NOT trivial. You can't just dump (POD) objects to disk and simply read it back to memory and expect things to work. For one Objects will not be in a valid state since their constructors have not been called, what do to with data saved on one machine and read on another? They can have different architectures/memory layouts, code can be compiled with different versions/settings of compilers. etc.. etc.. You might want to have a look at protobuf (allows for serialization to disk, network, and even other languages like Python) – Pepijn Kramer Jul 06 '22 at 13:34
  • @PepijnKramer I'm not really concerned about object construction since I'd like every component to be default-constructible (as a rule) and allow uninitialized fields in a C-like fashion. The deserialization process would be responsible of initializing the PODs anyway, fetching values from the file. I'm not concerned about architecture or compilers YET because (AS OF NOW) I'm assuming the user will be using little endian and a compiler setup similar to mine. Thank you so much for telling me about Protobuf, it looks very interesting. I'll definitely consider it as a possible solution! – prt_mhl Jul 06 '22 at 15:10

1 Answers1

-2

You can find a lot about this topic if you just google it. For example one of the top google finding is the duplicate topic here on stackoverflow: How do you serialize an object in C++?

I had the same problem and used the boost::serialization to solve it.

str0yd
  • 97
  • 11