#Help me pls =)

1 messages · Page 1 of 1 (latest)

wary meteor
#

Hello, I have a trouble while making my rhythm game. This is my code:

IEnumerator timer()
    {
        TimeSpan d = TimeSpan.FromSeconds(duration);
        string dur = string.Format("{0:D2}:{1:D2}", d.Minutes, d.Seconds);
        int index = 0;
        audio.loop = true;
        audio.Play();
        float waitAmount = 0.1f;
        while (true)
        {
            if (current >= duration) break;
            yield return new WaitForSeconds(waitAmount);
            DateTime fromdt = DateTime.Now;
            float mfrom = fromdt.Millisecond;
            current += 0.1f;
            TimeSpan c = TimeSpan.FromSeconds(current);
            ndur.text = string.Format("{0:D2}:{1:D2}", c.Minutes, c.Seconds) + " / " + dur;
            if (data.timestamp[index].time == string.Format("{0:D2}:{1:D2}:{2:D2}", c.Minutes, c.Seconds, c.Milliseconds))
            {
                gen.Generate(data.timestamp[index].x, data.timestamp[index].y);
                if (index < data.timestamp.Count() - 1) index++;
            }
            displayer.value = current;
            DateTime todt = DateTime.Now;
            float mto = todt.Millisecond;
            Debug.Log(mto);
            Debug.Log(mfrom);
            waitAmount = 0.1f - ((mto - mfrom) / 100);
            Debug.Log(waitAmount);
        }
    }

But even i checked the start and the end of code execution and subtract them. When the music is over, the timer is still going. They should end in the same time. After testing several time, the delay was like about 15 seconds, pls help!

frigid berry
#

Where do you actually exit from the infinite loop?

#

You need an exit condition.

wary meteor
#

if(current >= duration) break;

#

It worked fine, when the current's value is greater or equal to duration it will just exit the loop

frigid berry
#

yield break exits coroutine.

#

Also test the condition

wary meteor
#

No i mean, the while loop speed is not same as the music

#

Not syncing

#

Everything work fine except for that

frigid berry
#

Test the condition itself

wary meteor
frigid berry
#

The condition that doesn't fire - current >= duration test what's happening there.

#

You need to debug everything

#

Examine values

wary meteor
frigid berry
#

Thread is active 24 hours by default. You can repost link to it with a reminder what it is about later.

wary meteor
#

the loop still doesnt sync with the music

inland wing