Rewind and Forward the Game

Hi guys,

How can i rewind and forward my game?not a animation,whole of a the footbal games that have rewind and forward buttons.

so thanks

We have to understand first that is not the easiest thing in the world, and it's really depends on what kind the game it is. It's easier to implement it for a chess game and a little bit harder for a football game.

A general solution that could cover any kind of game would be the most obvious one. We should keep a record in a list of at least these things:

  • `Frame Count` - If you want to be able to skip frames.
  • `Delta Time` - You need to know for how long you should stay on that frame.
  • `Time Since Level Load` - To be aware of the exact time the actual game was on that frame.
  • Current state of your game - Easy to describe, but hard to implement. Let's see it in details.

So for each of these records we need to save the current game state which will include, all the `GameObject` that you have in the game and for each one you'll need to know:

  • Position
  • Rotation
  • Scale
  • Animations
  • Sound effects
  • Particle Systems
  • ...
  • Things that we can't even imagine
  • ...
  • Lots more

If we managed to store somehow the above information, then the next step would be start implementing our player and examine some of the implications that we might have here.

The simplest thing that the user should be able to do is to replay the game in `x1` speed. But we have to decide if we want to replay each frame that happened in the original game or we want to synchronize it with the real time.

  • In the first case, the total replay time in the speed `x1` would be different than the original game time, because the new frame rate will not be the same.
  • In the second case it will be much harder, because we might need to synchronize the original time with the current, so we need to be able to skip frames or waiting longer in some of them.
  • In any case we need to apply the recorded information to our current game and we have to do that very fast, because we are dealing with it in per frame basis.

To be able to seek to specific time of the game or playing it with different speeds wouldn't be that hard, because all we have to do is to find the correct record, apply the information and continue from there if not paused. We should also probably will be able to include in our current scripts the information if we are in a normal game or during a replay, because we have to disable the normal flow of the game.

Another completely different approach would to save only the information from the Input of the Player with the exact time-stamp. In order to replay the game afterwards would be starting the game normally, disabling the Input and sending the correct Input to the game at the correct time.

As a conclusion I would like to add that we have to trust completely the `Unity Engine/Physics` as it should react exactly the same way on each run. But even if we had, because of some time synchronizations problems we most likely will have a completely different outcome of the game (based on true facts).

It is possible, just start by trying to re-play one game object for a minute and if manage to do that, then you are good to go for trying to do it with two game objects for 2 minutes, and if you manage to do even that, then I guess you know how to continue... :)

Good luck and have fun..

There's no built-in way to do this in Unity as it would be up to you as the programmer to figure out what and how to record what you need.

Basically the idea would be to keep a record of everything that happens to an object in the game as it progresses, and then when you need to fast forward or rewind you would just feed that recorded info back into the system.

There may be a script or something out there that would help out for the initial setup, I'm not sure. Since every game is different and would need different things recorded, again it's up to you as the programmer to figure out the most efficient way to do it.

Lipis did an excellent job layout out the basics of the approach you'd want to use. I would add that in most games it's best to do both snapshots of game sates as well as player inputs driving additional changes. When I worked with PhysX, which was admittedly several years ago, it was not deterministic (meaning that given the same inputs in two simulations they were not guaranteed to produce the same result), and I'd wager that has not improved enough to be reliable (just as likely it's gotten worse). While you can in theory rely on recording only player input (or any 'unpredictable' states in your simulation) you will pull your hair out trying to create a deterministic game in most cases, and that's with a 100% deterministic engine, which you likely don't have. But player input takes virtually no data, and would generally dramatically reduce recording file sizes.

Also, if a physics heavy game, consider saving a cumulation of passing time to trigger saving a snapshot, and adding to it when objects collide based on their mass. The biggest variations are generally due to object interaction variation, so trying to do a snapshot after a collision is good. This will also give you more snapshots as more is happening within your scene.

One additional question I have related to this is that if you have a timeline a player can 'scrub' on, how would you go about applying that play time in a desired zero 'real' time. IOW, if you have a snapshot at frame 100, but want to scrub to 150, you jump to 100, restore, then how do you pass 50 frames instantly? Would you set time scale to something crazy high then try to set it back in the appropriate fixed update, or would that cause other issues?