Search Unity

How to check quest clear time?

Discussion in 'Scripting' started by leegod, Jul 15, 2019.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    1,798
    For example, if one user took 3 minutes, 14 seconds, 68 milliseconds to beat one quest(or stage, whatever), how to calculate it, how to store it as variable?

    And how to show & calculate the total playing time since user firstly started the game?

    I want to show some codes.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    771
    Use Time.time to compare a Quest's start and complete times.
    Since Time.time counts in seconds, just divide by 60 for minutes and multiply by 1000 for milliseconds.
    Rough example:
    Code (CSharp):
    1. public class Quest {
    2.    public bool isComplete = false;
    3.    public string completionTimeString;
    4.  
    5.    private float startTime;
    6.  
    7.    public void StartQuest() {
    8.       if(!isComplete) {
    9.          startTime = Time.time;
    10.       }
    11.    }
    12.  
    13.    public void CompleteQuest() {
    14.       if(!isComplete) {
    15.          isComplete = true;
    16.          int endTime = Mathf.FloorToInt(Time.time - startTime);
    17.          completionTimeString = GetCompletionTimeString(endTime);
    18.       }
    19.    }
    20.  
    21.    private string GetCompletionTimeString(int endTime) {
    22.       int mm = endTime / 60;
    23.       int ss = endTime;
    24.       int ms = endTime * 1000;
    25.  
    26.       return mm + " minutes, " + ss + " seconds, " + ms + " milliseconds.";
    27.    }
    28. }
    29.  
    Time.realTimeSinceStartup.
     
    Last edited: Jul 15, 2019
    leegod likes this.
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    1,798
    I am not sure my revision is best efficient code, but I get result I wanted with some tweak from above code. Thx.

    Code (CSharp):
    1. public void CompleteQuest()
    2.     {
    3.         float endTime = Time.time - startTime;
    4.         Debug.Log("end time is " + endTime);
    5.         completionTimeString = GetCompletionTimeString(endTime);
    6.     }
    7.     private string GetCompletionTimeString(float endTime)
    8.     {
    9.         int mm = (int)(endTime / 60);
    10.         float ss = endTime - (mm * 60);
    11.         float ms = (ss - Mathf.FloorToInt(ss)) * 100;
    12.         return mm + "'  " + Mathf.FloorToInt(ss) + "''  " + ms.ToString("f0");
    13.     }