Putting 3 floats together to make one big long number

Hello,

I am making a fastest time scoreboard system. I just need to work out how you add the 3 floats together. E.g. minutes = 1, seconds = 32 and milliseconds = 382 it would then give a big number of 132382.

Below is my code I made so far but I have an error of cannot implicitly convert float to float.

 void Update()
    {
        if (tutorialTimerOn && !story.StoryActive) //When the game has started and after the story has finished
        {
            TutorialTimeCalculation();
        }
        else if (tutorialTimerOn == false && !story.StoryActive && !pauseMenu.activeInHierarchy)
        {
            newTime = (minutes, seconds, milliseconds);
            Debug.Log(newTime);
            currentFastestTime = PlayerPrefs.GetFloat("tutorialFastestTime");
            if (newTime < currentFastestTime)
            {
                // New high score!
                currentFastestTime = newTime;
                PlayerPrefs.SetFloat("tutorialFastestTime", currentFastestTime);
                PlayerPrefs.SetString("tutorialFastestTimeText", timerText.text);
            }
            else
            {
                // High score hasn't changed
            }
        }
    }

The part of the code where I wrote: newTime = (minutes, seconds, milliseconds); is where I am trying to add these numbers together.

Thank you in advance!

My first thought would be to parse your floats out into strings, then pull out the decimal points, and the append them together, then cast into an int or float or whatever you needed.

Edit - I should say that I’m not very familiar with C#, and there are probably much faster and better ways to do this. :slight_smile:

that should work, or even saving as 3 separate values…
or could convert everything into milliseconds, so you can save it as a single value.
(and then can also convert back to min - sec - ms on load)

I personally would use a TimeSpan for something like this.

Allows you to define time based on those values, and get out specific values, as well as provides a range of lovely accessor methods

Its also really easy to link it to DateTime objects or convert to them if you wanted to associate time with a date.

EDIT: you can use it like so:

TimeSpan timeSpan = new TimeSpan(2, 14, 18); // hours, minutes, seconds
1 Like

See? That’s what I was talking about…

1 Like

Personally I like use UTC time format. Is easy to change time, and extract relevant time element, like years, hours, seconds etc.

Usually these would be combined like this to give a big int.

newTime = ((minutes * 60.0f) + seconds) * 1000.0f + milliseconds;

If you want the specific format you described, then change the 60.0 to 100.0

Really though, you should be storing the time as one value and only converting it into minutes, seconds etc for display rather than the other way around.

3 Likes

Hello everyone,

I’d like to firstly say thank you to all of you who took some time out of your day to help me with my problem. I am pleased to inform you have now got the fastest time system working, I’d like to give a special thanks to DominoM as that line of code you supplied has sorted out my problem. Nevertheless, a huge thank you to everyone for their input as it gives me a better idea of what to try in the future as I could make things more efficient!

I hope everyone has a great rest of their day!

Your welcome. For future reference, my fastest time code is something like this:

//start gane
float startTime = Time.time;

// in update loop
timerDisplay.text = GetTimeString(Time.time - startTime);

// finish game
float endTime = Time.time - startTime;

if (fastestTime == 0.0f || endTime < fastestTime)
{
    fastestTime = endTime;
}

string GetTimeString(float t)
{
    int minutes = Mathf.FloorToInt(t / 60.0f);
    int seconds = Mathf.FloorToInt(t - minutes * 60.0f);
    int milliseconds = Mathf.FloorToInt((t % 1.0f) * 1000.0f);

    return string.Format("{0:0}:{1:00}.{2:000}", minutes, seconds, milliseconds);
}

I set Time.timeScale to 0.0f going into the pause menu, and revert it to 1.0f on continuing game. If I needed something more complicated I’d probably accumulate an ignoreTime (during cut scenes etc) to subtract from the endTime.

Edit: bug fix for 1st run with no fastestTime :slight_smile: