PlayerPrefs and multiple instances of game in webplayer

I’ve got a turn based game that can be played head to head online. I’d like to be able to save the game state in playerprefs so it can be resumed after a disconnect. So I save the state after each turn.

The problem I’m worrying about is, if the player has two instances of the game running in their browser, they will save over each others game state as they complete turns, or I might even end up with a saved game that is a mix of two games. Am I correct in thinking this is what will happen?

How to prevent this?

The quick solution seems to be to not allow more than one instance to run, perhaps saving a time value (heartbeat) in playerprefs, that gets saved every 5 seconds, if a new instance tries to run and it’s within 10 seconds of the value in playerprefs it won’t run. Do you think that would be workable?

Thanks,
Stephan

Your idea sounds like it could work out ok.

Another thought would be to give each game a unique ID (Guid.NewGuid().ToString is a good way) and then just list out the games when a player starts the web site - using your heartbeat you could disable the buttons for games that are in progress. Obviously the ID wouldn’t want to be what appeared on the button - so you could just list the opponent and the playing time perhaps.

When a game started you would add it to a list stored in PlayerPrefs and when it finished you would remove it.