Sign in to follow this  
formalproof

Subsystem synchronization problem

Recommended Posts

I have the following problem: in my game, different subsystems have a pointer to the current game level. When the level changes, a subsystem called LevelHandler destroys the old map and allocates memory for a new one, storing the address in the same pointer. Of course, now all the other subsystems' level pointers have gone bad. My question is, how can I best synchronize all the subsystems to use the new level? This would be easy if every subsystem would know about each other, but it has been my design goal to isolate them from each other as much as possible. This problem is probably a well-known one, so there has to be some great professional-looking solution I'm not aware of. :)

Share this post


Link to post
Share on other sites
You can keep your systems pretty separate from each other by using a signal/slot mechanism. Boost.Signals is a decent implementation of the concept. You can see it used extensively in GUI libraries like gtkmm and Qt.

A signal is basically a container of callbacks. For instance, you can create two such signals, one called pre_level_destroyed and one called post_level_built. I am not sure where those objects should live, but one possibility is making them members of the Level. Each subsystem will register there what it needs to have called whenever the level is about to be destroyed, or right after it is built. Just before LevelHandler destroys the old map, it will "issue" the signal pre_level_destroyed (that means call all the callbacks in the container), and after it builds a new one it will isse the signal post_level_built.

That's how I would do it.

Share this post


Link to post
Share on other sites
While I don't personally use boost (I don't really do much programming at home anymore), this problem is a perfect candidate for the observer design pattern, which boost.signals seems to be an implementation of anyways.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this