- Home /
Repeatedly creating infinite loops slows down code
So I posted this question earlier, but the mod closed it saying I should write my scripts in c# instead of JavaScript, which is NOT a solution.
Anyway,
So I've built a very simple prototype for a day/night system. The scene has a plane that is 8x8 as the ground, 3 little houses I made out of cubes, a tall skinny cube (just there to cast a shadow), and 2 short cubes to represent characters. I also have 2 text renderers set up, one shows the current time (to include seconds) and the other shows the date.
Then I have a handful of scripts that control flow of time and display of date/time.
I noticed that the longer my app runs in play mode, the lower the framerate drops. It starts around 200fps then over a matter of minutes hangs out around 2fps.
When I play with the profiler on, it seems to be the time.js script I have attached to one of the text renderers.
This is it's code:
#pragma strict
function Updatetime()
{
while(true)
{
var today = System.DateTime.Now;
GetComponent.<TextMesh>().text = today.ToString("hh:mm" + " " + "tt");
yield WaitForSeconds(0.5f);
}
}
function Start()
{
Updatetime();
}
function Update () {
Updatetime();
}
Now again, this was closed before anyone had the chance to offer advice on the topic. Is there a better way to write this? Or, does anyone know specifically what might be causing such a frame rate drop over a matter of minutes?
Thanks, community!
About your problem. I think, the answer is quite obvious.
Each frame, you are calling the UpdateTime() function which has an infinite loop ! The longer your game runs, the more you have inifinite loops running.
Just call UpdateTime() in the Start function. Since UpdateTime is a coroutine inside an infinite loop, you don't have to call it each frame to "update" your function.
Hellium,
Thank you for explaining what the problem was, but best answer goes to rocket350 for explaining why it was a problem.
Thanks everyone!
Answer by rocket350 · Jul 21, 2015 at 08:38 PM
Ok, so the problem is that every frame, a script is launched which runs every 0.5 secondes. This means that every frame your computer has to proccess the loop in "Updatetime" again in addition to the ones lying back 0.5 seconds. My suggestion would be remove the "Updatetime()" from the "Update()" function.
Exactly what I needed. Thank you for breaking that down AND providing a possible solution.
Thank you!
Answer by tanoshimi · Jul 21, 2015 at 08:37 PM
I believe what the mod in question said was "You're misunderstanding how to create a coroutine in unityscript. Carefully read the documentation on this topic and repost if you're truly stuck.".
Judging by the fact that you're posting again, I'm guessing that you're truly stuck, but perhaps you could share what you've tried since last you posted, and why, on every frame of Update(), you're launching another infinite-running function...?
That is, indeed, part of what the mod said, yes. But you didn't quote the section where the mod told me to use c# ins$$anonymous$$d of JavaScript because it is better. It would have been helpful for the mod to help me understand what was wrong with the script ins$$anonymous$$d of just telling me that I didn't understand how to write it properly.
Your answer
Follow this Question
Related Questions
Stuttering / performance issue with certain JS scripts 0 Answers
Very Bad Performance on Android 3 Answers
Screen go Gray and only Cursor active OnTriggerEnter 1 Answer
Using Time.DeltaTime doesn't result in framerate independence 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers