Hey! My name is Joe Lubertazzi; I am the technical director on Dischord. I manage the overall structure and health of our code-base, our build systems, our editor and tools, and am responsible for our underlying game engine, named Ocean. Over these next few weeks, I’ll be detailing the custom reflection engine utilized by Ocean and the various benefits it offers us. But first, a primer!
Reflection is the ability of a computer program to examine and modify the structure and behavior of a program at runtime. With reflection, you can look at a generic object and ask it “What type are you?” or “Do you have a member called ’Health’?” You can also invoke functions on that object by name, or construct a new instance of that object, all at runtime.
Reflection is incredibly valuable when it comes to serialization and managing your data. Normally, for every type in your game that you want to save out to a file or display in your editor, you would have to write a custom Serialize function:
Reflection is the ability of a computer program to examine and modify the structure and behavior of a program at runtime. With reflection, you can look at a generic object and ask it “What type are you?” or “Do you have a member called ’Health’?” You can also invoke functions on that object by name, or construct a new instance of that object, all at runtime.
Reflection is incredibly valuable when it comes to serialization and managing your data. Normally, for every type in your game that you want to save out to a file or display in your editor, you would have to write a custom Serialize function:
Given the above example you can easily imagine that the Serialize function for most other types (such as the Weapon type) would be very similar. Furthermore, when adding a new member to a type, you must remember to go back and add it to that type’s Serialize function. Wouldn’t it be nice to have serialization just ‘work’ for any type we throw at it? Using reflection, we can do just that:
In this way objects can be serialized generically by their reflected properties with a single function, effectively removing the boilerplate in the previous example and the possibility of error. Ensuring a new type is serialized is now trivial: it needs simply be reflected.
Some programming languages, such as C# or Java, have reflection built-in. Unfortunately for us as game programmers we use C++, and C++ is not one of those languages – meaning that we must write a custom implementation of reflection for the C++ language ourselves if we want all of the glorious benefits.
This is precisely what we have done for Ocean. Ocean uses our reflection engine for everything from serialization to object creation to type-checking to function binding.
In my next (lengthier) post I'll be covering how reflection is utilized in Ocean’s object and resource management systems, our editor, and other miscellaneous tools.
Some programming languages, such as C# or Java, have reflection built-in. Unfortunately for us as game programmers we use C++, and C++ is not one of those languages – meaning that we must write a custom implementation of reflection for the C++ language ourselves if we want all of the glorious benefits.
This is precisely what we have done for Ocean. Ocean uses our reflection engine for everything from serialization to object creation to type-checking to function binding.
In my next (lengthier) post I'll be covering how reflection is utilized in Ocean’s object and resource management systems, our editor, and other miscellaneous tools.