1

i'm trying to record player input and play it back. i'm doing this by making a list in which i add the current frame every time the player clicks. when i play it back, i compare the list to the current frame. when the frame the player clicked is equal to the current playback frame, an action will happen.

public float recordingFrames;
public float playbackFrames;
public List<float> clickFrames;
public bool recording;
public bool playing;

if (recording)
{
    recordingFrames += Time.deltaTime;
    if (Input.GetMouseButton(0))
    {
        clickFrames.Add(recordingFrames);
    }
}

if (playback)
{
    playbackFrames += Time.deltaTime;
    for (int i = 0; i < clickFrames.Count; i++)
    {
        if (clickFrames[i] == playbackFrames)
        {
            Debug.Log("hello!")
        }
    }
}

the debug log is never received. how can i fix this? thank you!

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 2
    Possible duplicate of [Compare floats in Unity](https://stackoverflow.com/q/39898301/7111561) -> You can't use `==` for `float` values. Rather use `if(Mathf.Approximately(clickFrames[i], playbackFrames))` or use a threashold `if(Mathf.Abs(clickFrames[i] - playbackFrames) <= ThresholdValue)` – derHugo Sep 25 '19 at 17:09
  • 1
    Also elements of [Approach to solve a not accurate replay system](https://stackoverflow.com/q/54565734/1092820). You shouldn't check for approximate frame count anyway because there's no expectation for `deltaTime` being the same between recording episode and playback episode unless you're handling input in `FixedUpdate` (which you shouldn't do) – Ruzihm Sep 25 '19 at 17:21

1 Answers1

1

There's no reason to expect Time.deltaTime to be constant. You are probably never going to get the same cumulative amount of time this way. You are better off clicking as long as the next timestamp is less than or equal to the current timestamp, and keeping track of where you are in the list of clickFrames accordingly.

This also lets you avoid iterating through the while clickFrames list every frame:

public float recordingFrames;
public float playbackFrames;
public List<float> clickFrames;
public bool recording;
public bool playing;
public int nextClickFrameIndex;

public void BeginPlayback()
{
    nextClickFrameIndex = 0;
    playback = true;
}

public void InputRecordPlayback()
{
    if (recording)
    {
        recordingFrames += Time.deltaTime;
        if (Input.GetMouseButton(0))
        {
            clickFrames.Add(recordingFrames);
        }
    }

    if (playback)
    {
        playbackFrames += Time.deltaTime;

        // loop while you have clicks left in the replay
        // AND you have clicks to playback since the last frame
        while (
                nextClickFrameIndex < playBackFrames.Count 
                && clickFrames[nextClickFrameIndex] <= playbackFrames 
                )
        {
            nextClickFrameIndex++;
            Debug.Log("hello!");
        }
    }
}
Ruzihm
  • 19,749
  • 5
  • 36
  • 48