greenoldman
greenoldman

Reputation: 21072

Pattern for caching resources

I have a game with images -- I would like to cache them silently before the games begins. Thus I can have two scenarios:

  1. the code was loaded
  2. caching started
  3. caching finished
  4. user started the game
  5. run the game

OR

  1. the code was loaded
  2. caching started
  3. user started the game --> progress is displayed
  4. caching finished
  5. run the game

For the second case, I could pass to caching function a callback to run the game. But since first case can happen I cannot do this, because I have to wait for the user as well.

So now I am thinking about having global (ouch) variable which would be a callback onGameStart. By default it would be displaying progress, but caching callback would do two things -- reset onGameStart to actually starting the game, and checking if the game was started, if it was, it would continue immediately.

Did I miss something? Is it the right pattern?

Upvotes: 0

Views: 74

Answers (1)

David Ehrmann
David Ehrmann

Reputation: 7576

What about a ResourceObserver that gets a notification when the ResourceManager has fetched all the resources? Then your game keeps state, and if the user starts a game before the notification, use the progress notifications to update the screen, otherwise, just skip to the game.

TL;DR: Observer+State Machine.

Upvotes: 1

Related Questions