Search Unity

BestTime_System implementation problems...

Discussion in 'Scripting' started by JCDigital, Feb 19, 2020.

  1. JCDigital

    JCDigital

    Joined:
    Jan 20, 2016
    Posts:
    6
    I am working in a new game that is almost finished, but i cant get my script to work correctly to give me a bestTime rather than the currentTime I am saving result in PlayerPrefs and i have various if()s to verify all cases...

    for example:
    float _bestTime_F starts with no value, meaning 0 at the first run of the game.
    I have a float _currentTime_F that is constantly updating every game loop.

    - I need that if my _currentTime_F is > than my _bestTime_F,
    meaning your best time still same because you get a higher time than you Best.
    - Also verify if my _currentTime_F is < than my _besTime_F,
    meaning that you have made a new Best Score...
    - And if my _bestTime_F is <= _currentTime_F...
    player Best Time maintain the _bestTime_F value...
    meaning player have finished the level and no new Best Time has been made.

    I will appreciate any advice. ::JCD

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class _Timer : MonoBehaviour
    7. {
    8.     // VARIABLES____________________...
    9.  
    10.     public Text timer_TXT;
    11.     private string _currentTime_S;
    12.     private float _currentTime_F;
    13.  
    14.  
    15.     public Text bestTime_TXT;
    16.     public string bestTime_S;
    17.     private float _bestTime_F;
    18.      
    19.  
    20.  
    21.     // FUNCTIONS_____________________...
    22.  
    23.     void Start()
    24.     {
    25.         //_bestTime_F = PlayerPrefs.GetFloat("BestTime", Mathf.Infinity);
    26.        
    27.         _bestTime_F = PlayerPrefs.GetFloat("BestTime");
    28.  
    29.         Debug.Log(_bestTime_F);
    30.        
    31.     } //start.
    32.  
    33.    
    34.     private void Update()
    35.     {
    36.         Timer();
    37.  
    38.     } //update.
    39.  
    40.  
    41.     private void Timer()
    42.     {
    43.         timer_TXT.text = System.TimeSpan.
    44.             FromSeconds((int)Time.timeSinceLevelLoad).
    45.             ToString();
    46.  
    47.         _currentTime_S = timer_TXT.text;    
    48.        
    49.  
    50.  
    51.  
    52.         _currentTime_F += Time.deltaTime;
    53.         //Debug.Log(_currentTime_F);
    54.  
    55.         if (_bestTime_F < _currentTime_F)
    56.         {
    57.             _bestTime_F = _currentTime_F;
    58.         }
    59.  
    60.      
    61.  
    62.  
    63.         if (_Spikes.s_destroyed_B) /* testing when player dies but it will be called when level clear */
    64.         {
    65.             if (_bestTime_F < _currentTime_F)
    66.             {
    67.                 _bestTime_F = _currentTime_F;
    68.  
    69.                 PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    70.                 //PlayerPrefs.Save();
    71.  
    72.                 Debug.Log("Your BestTime starts being less than your CurrentTime...");
    73.             }              
    74.  
    75.             if (_currentTime_F >= _bestTime_F)
    76.             {
    77.                 //_bestTime_F = _currentTime_F;
    78.  
    79.                 //_bestTime_F = PlayerPrefs.GetFloat("BestTime");
    80.  
    81.                 PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    82.                 //PlayerPrefs.Save();
    83.  
    84.                 Debug.Log("Your BestTime is greater or equal than your CurrentTime...");
    85.             }
    86.  
    87.             if (_currentTime_F < _bestTime_F)
    88.             {
    89.                 //_bestTime_F = _currentTime_F;
    90.  
    91.                 PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    92.  
    93.                 Debug.Log("Congratulations, you set a new BestTime!");
    94.             }
    95.         }          
    96.  
    97.     } //timer.
    98.  
    99.    
    100. } // END TIMER.
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You failed to mentioned what is actually not working. Here's the problems I noticed at a quick glance.

    * Line 55, whenever _bestTime_F is less than _currentTime_F, you are setting _bestTime_F to _currentTime_F. This seems backwards assuming lower times are better.
    * Line 57, you are setting _bestTime_F to _currentTime_F, but the rest of the code in this method depends on comparisons between these two variables. Why set them equal to each other here, but do any of the "if" statements with them later? Makes no sense
    * Line 65, this "if" statement can never be true because if it were true you would have set the two variables equal to each other back on line 57.
    * Line 91, if _currentTime_F is better than _bestTime_F, why aren't you saving _currentTime_F as the new BestTime?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    One pointer to making this stuff cleaner: when you want to implement something persistent like a high score saver, put it all in its own static class and then access it ONLY through that class. Here's a SUPER-simple example:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public static class BestScore
    4. {
    5.     private const string s_BestScore = "BestScore";
    6.     public static int Score
    7.     {
    8.         get
    9.         {
    10.             return PlayerPrefs.GetInt(s_BestScore, 0);
    11.         }
    12.         set
    13.         {
    14.             PlayerPrefs.SetInt(s_BestScore, value);
    15.         }
    16.     }
    17. }
    Once you have that, NEVER go inside that class again.

    To check if your current score is higher and set it,

    Code (csharp):
    1. if (CurrentScore > BestScore.Score)
    2. {
    3.   Debug.Log( "You got the best score!");
    4.   BestScore.Score = CurrentScore;
    5. }
    Anywhere you want to use it, just use it:

    Code (csharp):
    1. Debug.Log( "Your best score is " + BestScore.Score);
     
    Joe-Censored likes this.
  4. JCDigital

    JCDigital

    Joined:
    Jan 20, 2016
    Posts:
    6
    @Joe_Censored

    Joe, I re-write the script and i come with a way to get what i was looking for. If you want to copy code and run it by yourself and see results and you have a better way to achieve this just let me know. Take a look below...



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Timer : MonoBehaviour
    {
    // VARIABLES____________________...

    private float _currentTime_F = 0f;
    private float _bestTime_F;

    // FUNCIONES____________________...


    private void Awake()
    {
    //unncomment line below, run the game and exit...
    // if you need to clear all saved values to the PlayerPrefs.
    //PlayerPrefs.DeleteAll();

    } //awake.

    private void Start()
    {
    _bestTime_F = PlayerPrefs.GetFloat("BestTime"); //second time you run the game again...
    // in console the value of PlayerPrefs get int "BestTime"
    // will be printing the value it holds as you BestTime.


    Debug.Log("Recent Best Time: " + _bestTime_F); //i explicitly need this value to be 0 the
    // first run after the game being installed...

    } //start.


    private void Update()
    {
    runningTimer();

    } //update.


    private void runningTimer()
    {
    _currentTime_F += Time.deltaTime;

    Debug.Log("Current Time: " + _currentTime_F);

    } //timer.


    private void OnApplicationQuit()
    {
    PlayerPrefs.SetFloat("CurrentTime", _currentTime_F);

    //FIRST TIME PLAYER PLAY THE GAME!
    if (_bestTime_F == 0) //the game will start _bestTime_F with a value of 0...
    //this will only be true ONLY the very first time you run the game...
    // meaning this is the very first time you have play the game!
    {


    //in this line i assign the _currentTime_F value to the var _BestTime_F,
    // meaning at this moment i need that var _bestTime_F holds the same value
    // to be saved into PlayerPrefs becuase you have made your very
    // first BestTime;
    _bestTime_F = _currentTime_F;

    //saving...
    PlayerPrefs.SetFloat("BestTime", _bestTime_F);



    //Debugs.
    Debug.Log("BestTime_F_var: " + _bestTime_F); //i explicitly need this value to be 0 the
    // first run after the game being installed...

    Debug.Log("CurrentTime_F_var: " + _currentTime_F); //current time or simply time obtained this run.

    } //can NEVER be true again, meaning never will be 0...



    //NOT THE FIRST... BUT ALL SUBSECUENT PLAYS!
    if (_currentTime_F < _bestTime_F) //at this point _bestTime_F will never be 0 because you already made a new BestTime...
    // now if the time you finished the level is LESS than your Time stored in _bestTime_F var,
    // your _currentTime_F var will pass it value as your new BestTime.
    {
    PlayerPrefs.SetFloat("BestTime", _currentTime_F); //assigning the new lower value (_currentTime_F) to PlayerPrefs as your
    // NEW BestTime.

    Debug.Log("Congratulation!, you have made a new Best Time: " + PlayerPrefs.GetFloat("BestTime"));
    }


    //ONLY IF THE CURRENT TIME IS EQUAL OR HIGHER THAN THE BEST SCORE YOU HAVE ALREADY MADE,
    // MEANING BEST TIME WILL REMAIN THE SAME.
    if (_currentTime_F >= _bestTime_F)
    {
    PlayerPrefs.SetFloat("BestTime", _bestTime_F);

    Debug.Log("Your BestTime stays the same becouse you have not make a new BestTime than... Your BestTime: " +
    _bestTime_F);
    }


    } //onapplicationquit.


    } // END TIMER.
     

    Attached Files:

  5. JCDigital

    JCDigital

    Joined:
    Jan 20, 2016
    Posts:
    6


    Joe, i re-write my script only for console debug and i come with a way to achieve what i was looking to get.
    Please take a look and if you have a better or more efficient way to do it, just let me know.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Timer : MonoBehaviour
    6. {
    7.     // VARIABLES____________________...
    8.  
    9.     private float _currentTime_F = 0f;
    10.     private float _bestTime_F;
    11.  
    12.     // FUNCIONES____________________...
    13.  
    14.  
    15.     private void Awake()
    16.     {
    17.         //unncomment line below, run the game and exit...
    18.         // if you need to clear all saved values to the PlayerPrefs.
    19.         //PlayerPrefs.DeleteAll();
    20.  
    21.     } //awake.
    22.  
    23.     private void Start()
    24.     {
    25.         _bestTime_F = PlayerPrefs.GetFloat("BestTime"); //second time you run the game again...
    26.                                                         // in console the value of PlayerPrefs get int "BestTime"
    27.                                                         //  will be printing the value it holds as you BestTime.
    28.  
    29.  
    30.         Debug.Log("Recent Best Time: " + _bestTime_F); //i explicitly need this value to be 0 the
    31.                                                           // first run after the game being installed...
    32.  
    33.     } //start.
    34.  
    35.    
    36.     private void Update()
    37.     {
    38.         runningTimer();
    39.  
    40.     } //update.
    41.  
    42.  
    43.     private void runningTimer()
    44.     {
    45.         _currentTime_F += Time.deltaTime;
    46.  
    47.         Debug.Log("Current Time: " + _currentTime_F);
    48.  
    49.     } //timer.
    50.  
    51.  
    52.     private void OnApplicationQuit()
    53.     {
    54.         PlayerPrefs.SetFloat("CurrentTime", _currentTime_F);
    55.  
    56.         //FIRST TIME PLAYER PLAY THE GAME!
    57.         if (_bestTime_F == 0) //the game will start _bestTime_F with a value of 0...
    58.                                 //this will only be true ONLY the very first time you run the game...
    59.                                 // meaning this is the very first time you have play the game!
    60.         {
    61.          
    62.            
    63.             //in this line i assign the _currentTime_F value to the var _BestTime_F,
    64.                 // meaning at this moment i need that var _bestTime_F holds the same value
    65.                 // to be saved into PlayerPrefs becuase you have made your very
    66.                 // first BestTime;
    67.             _bestTime_F = _currentTime_F;
    68.  
    69.             //saving...
    70.             PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    71.  
    72.  
    73.  
    74.             //Debugs.
    75.             Debug.Log("BestTime_F_var: " + _bestTime_F); //i explicitly need this value to be 0 the
    76.                                                          // first run after the game being installed...
    77.  
    78.             Debug.Log("CurrentTime_F_var: " + _currentTime_F); //current time or simply time obtained this run.
    79.  
    80.         } //can NEVER be true again, meaning never will be 0...
    81.  
    82.  
    83.        
    84.         //NOT THE FIRST... BUT ALL SUBSECUENT PLAYS!
    85.         if (_currentTime_F < _bestTime_F) //at this point _bestTime_F will never be 0 because you already made a new BestTime...
    86.                                           // now if the time you finished the level is LESS than your Time stored in _bestTime_F var,
    87.                                           // your _currentTime_F var will pass it value as your new BestTime.
    88.         {
    89.             PlayerPrefs.SetFloat("BestTime", _currentTime_F); //assigning the new lower value (_currentTime_F) to PlayerPrefs as your
    90.                                                               // NEW BestTime.
    91.  
    92.             Debug.Log("Congratulation!, you have made a new Best Time: " + PlayerPrefs.GetFloat("BestTime"));
    93.         }
    94.  
    95.  
    96.         //ONLY IF THE CURRENT TIME IS EQUAL OR HIGHER THAN THE BEST SCORE YOU HAVE ALREADY MADE,
    97.         // MEANING BEST TIME WILL REMAIN THE SAME.
    98.         if (_currentTime_F >= _bestTime_F)
    99.         {
    100.             PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    101.  
    102.             Debug.Log("Your BestTime stays the same becouse you have not make a new BestTime than... Your BestTime: " +
    103.                 _bestTime_F);
    104.         }
    105.  
    106.  
    107.     } //onapplicationquit.
    108.  
    109.  
    110. } // END TIMER.
    111.  
     
  6. JCDigital

    JCDigital

    Joined:
    Jan 20, 2016
    Posts:
    6
    Thanks @kurt, but not what is was trying to achieve.
    Take a look at my re-written script and take a look what i was trying to achieve.
    If you have any advice just let me know. Thanks again.
     
  7. JCDigital

    JCDigital

    Joined:
    Jan 20, 2016
    Posts:
    6
    Here is the script re-written again for anybody who wants to contribute to write this in a efficient way.

    EYE: it will not be implemented when application Quit but when Player Completes the
    current Level! so this is just an example for anybody who wants to debug it over console
    to see results of statements.


    Thanks JCD::

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Timer : MonoBehaviour
    6. {
    7.     // VARIABLES____________________...
    8.  
    9.     private float _currentTime_F = 0f;
    10.     private float _bestTime_F;
    11.  
    12.     // FUNCIONES____________________...
    13.  
    14.  
    15.     private void Awake()
    16.     {
    17.         //unncomment line below, run the game and exit...
    18.         // if you need to clear all saved values to the PlayerPrefs.
    19.         //PlayerPrefs.DeleteAll();
    20.  
    21.     } //awake.
    22.  
    23.     private void Start()
    24.     {
    25.         _bestTime_F = PlayerPrefs.GetFloat("BestTime"); //second time you run the game again...
    26.                                                         // in console the value of PlayerPrefs get int "BestTime"
    27.                                                         //  will be printing the value it holds as you BestTime.
    28.  
    29.  
    30.         Debug.Log("Recent Best Time: " + _bestTime_F); //i explicitly need this value to be 0 the
    31.                                                           // first run after the game being installed...
    32.  
    33.     } //start.
    34.  
    35.  
    36.     private void Update()
    37.     {
    38.         runningTimer();
    39.  
    40.     } //update.
    41.  
    42.  
    43.     private void runningTimer()
    44.     {
    45.         _currentTime_F += Time.deltaTime;
    46.  
    47.         Debug.Log("Current Time: " + _currentTime_F);
    48.  
    49.     } //timer.
    50.  
    51.  
    52.     private void OnApplicationQuit()
    53.     {
    54.         PlayerPrefs.SetFloat("CurrentTime", _currentTime_F);
    55.  
    56.         //FIRST TIME PLAYER PLAY THE GAME!
    57.         if (_bestTime_F == 0) //the game will start _bestTime_F with a value of 0...
    58.                                 //this will only be true ONLY the very first time you run the game...
    59.                                 // meaning this is the very first time you have play the game!
    60.         {
    61.          
    62.          
    63.             //in this line i assign the _currentTime_F value to the var _BestTime_F,
    64.                 // meaning at this moment i need that var _bestTime_F holds the same value
    65.                 // to be saved into PlayerPrefs becuase you have made your very
    66.                 // first BestTime;
    67.             _bestTime_F = _currentTime_F;
    68.  
    69.             //saving...
    70.             PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    71.  
    72.  
    73.  
    74.             //Debugs.
    75.             Debug.Log("BestTime_F_var: " + _bestTime_F); //i explicitly need this value to be 0 the
    76.                                                          // first run after the game being installed...
    77.  
    78.             Debug.Log("CurrentTime_F_var: " + _currentTime_F); //current time or simply time obtained this run.
    79.  
    80.         } //can NEVER be true again, meaning never will be 0...
    81.  
    82.  
    83.      
    84.         //NOT THE FIRST... BUT ALL SUBSECUENT PLAYS!
    85.         if (_currentTime_F < _bestTime_F) //at this point _bestTime_F will never be 0 because you already made a new BestTime...
    86.                                           // now if the time you finished the level is LESS than your Time stored in _bestTime_F var,
    87.                                           // your _currentTime_F var will pass it value as your new BestTime.
    88.         {
    89.             PlayerPrefs.SetFloat("BestTime", _currentTime_F); //assigning the new lower value (_currentTime_F) to PlayerPrefs as your
    90.                                                               // NEW BestTime.
    91.  
    92.  
    93.             Debug.Log("Congratulation!, you have made a new Best Time: " + PlayerPrefs.GetFloat("BestTime"));
    94.         }
    95.  
    96.  
    97.         //ONLY IF THE CURRENT TIME IS EQUAL OR HIGHER THAN THE BEST SCORE YOU HAVE ALREADY MADE,
    98.         // MEANING BEST TIME WILL REMAIN THE SAME.
    99.         if (_currentTime_F >= _bestTime_F)
    100.         {
    101.             PlayerPrefs.SetFloat("BestTime", _bestTime_F);
    102.  
    103.             Debug.Log("Your BestTime stays the same becouse you have not make a new BestTime than... Your Time was: " +
    104.                 _bestTime_F);
    105.         }
    106.  
    107.  
    108.     } //onapplicationquit.
    109.  
    110.  
    111. } // END TIMER.
    112.  
     
    Last edited: Feb 22, 2020