Issues with Observer Pattern (something to be mindful of)

Firstly, I think it’s pretty obvious that the “Observer Pattern” is a very standard and very useful methodology that one must use for any number of reasons.

Added: Perhaps a better way of putting it would have been “Something to be mindful of”.

My concern, and it’s probably a small one that’s easily addressed, is that I’ve seen bugs in games and even OS’s that seem to be a result of using the idea “wait until you’ve been told that something changed before doing something” (i.e. the Observer Pattern). A possible (I’m not certain about this) example I’ve seen has to do ironically enough with cursor affordance. I’ve seen games where if one alt-tabs away, the cursor is changed by the OS to the standard pointer but when one alt-tabs back to the game, the game doesn’t change the cursor back because the conditions in the program haven’t been met to send the notification out that, for example, the layer hit has changed so the cursor needs to be changed.

Hopefully it doesn’t seem like I’m rambling here. :slight_smile:

1 Like

Hi Scimajor,
I too have seen that sort of bug and it is definitely something to be mindful of. You’re correct that the problem with the Observer pattern is it tends to fall over when the user does something that you didn’t plan for.

With the example of alt-tabbing away from the window and back, a simple fix would be to make the cursor affordance script run a check whenever the game loses or regains focus.
Now, this doesn’t necessarily have to be within the cursor affordance script itself. If you were developing a mobile game for example, there would be a whole bunch of stuff you’d need to do when the screen loses focus (like pausing the game, turning off the music etc), so you could stuff the logic in a script specifically for that purpose.

Overall though, it’s a pretty solid pattern and it makes life a lot easier than some of the alternatives :smiley:

1 Like

Privacy & Terms