I've been diving into some game production code lately and noticed that many games are scriptable and can pull level data from files. To achieve this, a reflection system is necessary to identify class names, their inheritance, and similar properties during runtime. This lets you link XML tags to in-game objects and expose the game world to scripting environments.
However, after looking into various reflection systems, they all seem like a chaotic jumble of macros, templates, and preprocessors. As someone who considers themselves somewhat experienced in C++, I find it overwhelming to understand how to put something like this together. The templates often have a confusing number of parameters, making it hard for me to grasp how to create a class that's suitable for reflection. I'm eager to learn more about this topic to advance my project. Can anyone recommend useful resources or methods to simplify this process?
2 Answers
C++ doesn’t natively support reflection, but you can emulate it. The complexity comes from trying to use SFINAE and other techniques, which can easily lead to frustration. If you’re feeling overwhelmed, don’t worry; it’s a common issue. There are ways to achieve reflection-like behavior without getting lost in template hell.
A practical approach is to use code generation. You can define your structures in a specific format, like IDL Schema, and then use tools to generate the corresponding C++ code and IO functionalities. Looking into Protocol Buffers, FlatBuffers, or Cap’n Proto can really help. They handle a lot of the heavy lifting for you!
That sounds promising! But I’ve also heard that using templates can help—though it can get really complicated, too. Have you had experience with that?
So are you saying there's no direct way to do it? Would I be better off looking into languages that support this more naturally, or is there hope for us C++ devs?