Monday, 10 October 2011

Developer Journal 121

I'm currently writing some code to make the world bigger over time. In particular, I'm thinking about how to save the state of the world over time. I'm wary of using more of Boost Serialization. The world is pretty simple with four walls and a floor. I can serialize the world object or the wall and ground objects.

I remember reading that I should avoid file reading in constructors. I'll need to do some file reading to get parameters to control how the world changes over time. I need to do this in the constructor if I serialize the world object or else I will need to call an initialisation function after each time I load the world object.

I don't know whether to serialize the world object or just the ground and wall objects. When in doubt and analysis paralysis, my strategy is usually to do the obvious, easy, albeit dumb thing. Save the 4 walls, the ground, and the last update time. Do everything manually for now.

I'm finding I'm spending more time trying to do things the smart way than is worthwhile.

Even simpler, save the current world size and the last world update time.

Hmm, that means the currentWorldSize and the lastWorldUpdate is inside class SimData and outside class World. When I load class World, it should be good to go. Albeit, I have to manually do stuff for physics and graphics. I'll have to copy over currentWorldSize and lastWorldUpdate from class SimData to class World. Then I'll have variables doing the same thing in different places. Grr, I'll serialize the world.

Dang! I just realized my fancy memory registry class is limited by the serialization process. My idea was to wrap new in a function that creates a note in the memory registry. When an object goes out of scope it notifies the memory registry. When the memory registry goes out of scope it should have no notes. If it has notes then it means some objects were not released and there is a memory leak.

I originally stumbled because I couldn't figure out how to get it to work with classes that were not my own. Then I realized that I didn't need to. The point of the memory register was to make sure that I avoided cyclic dependencies in my code. All good, then I realized that I didn't know how to make Boost Serialization use my New() function. I have to register objects created by Boost Serialization with the memory register manually. Not ideal, but it'll work for now.

No comments:

Post a Comment