Subsystem synchronization problem

Started by
4 comments, last by Dae 15 years, 3 months ago
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. :)
Advertisement
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.
Sounds like an excellent solution. I'll take a look at that library. Thanks!
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.
Personally I'd recommend sigslot, as it's only a one header include that'll give you observer pattern functionality. Signals are definately the way to go imo to make your subsystems blindly communicate with each other.
Pointer-to-Pointer?

or events...
010001000110000101100101

This topic is closed to new replies.

Advertisement