Is my problem delta time or just bad code
I have extensively researched this on here and elsewhere and while I have read a lot I am just not seeing where this code is not working. The problem only exists when the game is not the active tab in the browser. I read something in another post about some browsers throttling inactive tabs but I would think even it was only processing 1 frame a minute that deltaTime would be correct. public void earn() { if (count > 0) { time += Time.deltaTime; //if time goes over the time to act while (time >= timer) { //remove one cycle worth of time time -= timer; //give cash float cashToAdd = valueFormulaRemovedForClarity; gm.cash += cashToAdd; gm.totalEarnings += cashToAdd; } } }
I haven't tried Time.fixedDeltaTime but it is my understanding that it doesn't really apply to my needs since I don't care how often this code runs but rather that the built up deltaTime is correct when it does. 1 FPS or 60 FPS are not relevant to me here.
It seems like when it is running on a background tab the user is not getting cash at the correct rate. I guess I need to wire something up to see exactly what is going on.
The Time.deltaTime represents time in seconds it took to complete the last frame. So it seems like the game isn't running when the tab isn't active.
Have you enabled "Run in background" in the Player Settings?
I have. It appears to run fine when the browser is $$anonymous$$imized just not if it is active but on a background tab.
Answer by Pengocat · Jan 21, 2017 at 03:24 PM
A while loop can easily stop the execution if used incorrectly. Everything outside the while loop is not updating while the while loop runs. So Time.deltaTime is only called once before the while loop so depending on whatever timer is it is becoming very brittle. You may want to restructure your code to use an if statement and a for loop instead.
Timer rarely changes but it will always be a value between .1 and 90.0 seconds
So if time equaled 5 $$anonymous$$utes and timer is 60 seconds it would execute the loop 5 times and be done. If a case occurs where it were to get stuck in that while loop things would have gone so horribly wrong that there would be no recovering from it gracefully anyway. The only case I can see that happening is if time = infinity which like I said means that something is hopelessly broken at that point anyway
Your answer
Follow this Question
Related Questions
Need help with making disappearing platforms. Error CS1525 1 Answer
Timer float not evaluating correctly when I check if it's less than another number 3 Answers
Timer only goes for .02 seconds 1 Answer
Traffic Light won't change colors State machine 0 Answers
is it possible to reset Time.time 1 Answer