Jump to content

Recording inputs for replays - problem with delay


Raicuparta
 Share

Recommended Posts

The game in question only uses one key, so the cheapest way to save a replay would be by storing the input times. What I'm doing is something like this:

totalTime = 0inputTimes = []replaying = falsefunction update () {  totalTime += game.time.physicsElapsedMS  if (replaying && totalTime >= inputTimes[0])    inputTimes.shift()    keyPresed()}// called on key pressfunction keyPressed () {  if (!replaying) inputTimes.push(totalTime)  // also player movement, etc...}function setReplay (times) {  inputTimes = times  replaying = true}

I try recording a session, the passing the resulting array to the game, but there's always a little delay. It seems to be always exactly the same delay though. When the replay is playing, it always plays exactly the same way, but always a little bit different (not even sure if it's late or early) than the moves I made. Is there a better approach to this? Am I doing something obviously wrong?

 

EDIT: after some testing it seems the delay is about 15ms (or exactly 15ms), not sure why. Adding 15 to totalTime when checking the inputTimes array seems to make it perfect, couldn't make it fail so far. I doubt it is actually perfect, though.

Link to comment
Share on other sites

I believe keyboard events are handled outside of the update/render calls, and the callbacks are called directly from the event handler functions Phaser registers... hence your totalTime variable reflects the last time that the update() function started to run, not the time that your keyPressed() function was called.

Might help to move the replay key simulating code outside of update() and into a postUpdate() function, so your simulated keypresses register after all the update code has ran.

Finally assuming you are using a fully fixed timestep (because I don't now how accurate your "recordings" would be otherwise*) why record times rather than frames? Just increment your time counter in update by one every time it runs...

*think about what happens if a frame is skipped during recording but not playback or vice-versa

Link to comment
Share on other sites

Finally assuming you are using a fully fixed timestep (because I don't now how accurate your "recordings" would be otherwise*

How do I know if I'm doing this?

 

*think about what happens if a frame is skipped during recording but not playback or vice-versa

I thought using time instead of frames would prevent that kind of problems.

Link to comment
Share on other sites

Usually your inputs are converted into actions (like changing the coordinates of a sprite) during the update loops, not at the exact moment you press them. So when you 'replay' the game, you have absolutely no guarantee that the update loops will execute at the exact same time. And also, even a slight FPS drop (that is, less update loops) would change everything.

 

So you're right not to use frames, but a 15ms delay seems unavoidable.

 

Maybe one thing you could try to do is to manually call the update() function right after an input is pressed (or replayed). Thus the code wouldn't have to wait for the update function to be called automatically every 15ms (or so), and you might gain some accuracy.

Link to comment
Share on other sites

Maybe one thing you could try to do is to manually call the update() function right after an input is pressed (or replayed). Thus the code wouldn't have to wait for the update function to be called automatically every 15ms (or so), and you might gain some accuracy.

This seems to have worked! Thank you! Now I don't have to add those 15ms manually, which could probably result in failures in the replays.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...