Hi,
One feature of the engine for the game I'm developing is it will record the previous 32 (or so) full states for dynamic objects in a looping buffer. That way, systems like the renderer and quicksave can function without blocking the simulation thread (assuming they finish their job before it comes around the loop again).
This seems like a great idea (which has been successfully implemented in many games), but I'm having trouble figuring out how to implement it myself. Since my game engine is written in C++, performing a straight memcopy would be very dangerous. Some elements need more intelligent behavior when performing the copy as well, outside of the standard copy-assignment operator (ex, pointers to other objects in the scene need to be adjusted to point to the new version, even if it's just a simple +1 increment).
The first solution that comes to mind would just have a virtual function on the "GameObject" and "Component" classes, so that the author of each class can just manually copy over the relevant data (and exclude data that doesn't need to be copied), performing any adjustments themselves. While from an implementation standpoint this is probably the safest, simplest, and most efficient solution, it leaves a lot of room for user error and will quickly become very tedious. To solve that I could have the default implementation operate via the reflection system.
Complicating things, there are cases where a field simply is not copyable due to its type. I could either ignore copying that field entirely, or perform an intelligent deep-copy for that type (ex, a UniquePtr would have its pointed value copied, while a standard pointer would not).
Another solution to that problem would be to simply forbid the use of non-copyable types on GameObjects or Components. This would shift a lot of memory-management responsibility onto the scene manager itself (one thing that would have to change is how Components are currently owned by the GameObjects they are attached to. While conceptually this makes sense, requiring GameObjects to be fully copyable would entail moving ownership to the scene, which would complicate the question of when to detach/destroy components).
There are a lot of facets to this problem and there's a very good chance that I'm overthinking it. Ultimately, I'd like the solution I settle on to be safe and efficient, while also not making too many assumptions about how the user-defined Component/GameObject operates. There would of course have to be a few compromises on the part of the author of such types, but if I can leave things as open as possible that would be great. Whatever the solution is, I just can't think of it right now so I'd love to hear your ideas.
Edit:
What I've decided on is making this a special case of serialization (serializing to a working portion of memory, as opposed to a file or something). With a little modification, I could add support for this kind of thing to my existing serialization system. I'm still open to suggestions though.