- Home /
waitforseconds in update good for perfomance ?
hey at all,
i'm experimenting with waitforseconds in update(). In the manual is explained that this would greatly reduce the number of checks carried out.
So i changed my Script from :
void Update () {
thisMiss1conf = Miss1confirmedScr.Miss1confirm;
if(thisMiss1conf)
{
Einl_Sound_2.SetActive(true);
thisRatPack.SetActive(true);
Miss1Canvas.SetActive(false);
BeendenButt.SetActive(false);
}
if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf &&
!jalthiPack1[4].activeSelf &&thisMiss1conf && thisRatPack.activeSelf)
{
Miss1done = true;
thisMissaccImg.SetActive(true);
}
}
to :
void Start ()
{
Miss1confirmedScr = Einl1Obj.GetComponent<Einleitung_1>();
thisRatPack.SetActive(false);
Einl_Sound_1.SetActive(true);
Einl_Sound_2.SetActive(false);
StartCoroutine(xxx());
}
void Update ()
{
thisMiss1conf = Miss1confirmedScr.Miss1confirm;
StartCoroutine("xxx");
if(thisMiss1conf)
{
Einl_Sound_2.SetActive(true);
thisRatPack.SetActive(true);
Miss1Canvas.SetActive(false);
BeendenButt.SetActive(false);
StartCoroutine("xxx");
}
if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf &&
!jalthiPack1[4].activeSelf &&thisMiss1conf && thisRatPack.activeSelf)
{
Miss1done = true;
thisMissaccImg.SetActive(true);
StartCoroutine("xxx");
}
StartCoroutine("xxx");
}
IEnumerator xxx()
{
yield return new WaitForSeconds(0.1f);
}
well, it works and i dont get any errors. What i would like to know if its really correct or nonsens. And if its really a benefit for perfomance although i cant "see" a big difference in fps.
But i think "if so" i have several more scripts in which the functions in Update() hasn't to be calculated absolutely precise in every frame and could be also changed in this way.
And in summary it would make a difference for perfomance, wouldn't it?
I'm very nosily about your opinion or tips and tricks
yours
This is not correct usage. You are starting the coroutine xxx at least twice every single frame.
Could you provide a link to the place in the documentation you refer?
BTW, if you refer to the DoCheck() coroutine in:
https://docs.unity3d.com/$$anonymous$$anual/Coroutines.html
It's being used in a different way. The coroutine is supposed to get started only once (not every single frame) , in order to call the ProximityCheck() method according to the WaitForSeconds interval, rather than every single frame.
thx for the fast reply!
yes, thats the right link. I thought i could do it similar to the "Fade()" example. In which Fade() is in the update there. So is $$anonymous$$e completely false ? or would it be ok to set my coroutine only once at the end of the update() ?
Answer by pako · Sep 26, 2016 at 12:30 PM
Fade() is started inside Update() but ONLY if the "f" key is pressed. So, if the end user pressed the "f" key repeatedly, it would start a new instance of the Fade() coroutine per key press. Not a good practice!
Similarly, if you start your coroutine at the end of Update, a new instance of the coroutine would get started every single frame, very bad for performance, and it's not intended behavior anyway.
However, you could have an if condition inside Update() to start the coroutine, e.g.
if(gameOver)
{
StartCoroutine("xxx");
}
This would work, because in practice you can't have gameOver = true occurring very frequently.
Instead of gameOver you can use any condition you like of course, but you should have in mind that the frequency that this boolean variable gets set to true is small.
You must also put the behavior that needs repeating every 0.1s inside the xxx coroutine, not inside Update. This is illustrated in both Fade() and DoCheck() in the examples.
In other words, if you just start the xxx coroutine, its WaitForSeconds(0.1f) will not affect at all in practice the rate at which Update() is called. To be more precise, the rate at which Update() gets called might be affected by other things in your code, but not by the WaitForSeconds(0.1f) itself.
Answer by Ego65 · Sep 26, 2016 at 03:33 PM
puuh pretty confusing for a beginner ):
if i understand you wright the code should look like this (?) :
public bool nevertrue = true; // never become false
void Update ()
{
if(nevertrue)
{
StartCoroutine("xxx");
}
}
IEnumerator xxx()
{
thisMiss1conf = Miss1confirmedScr.Miss1confirm;
if(thisMiss1conf)
{
Einl_Sound_2.SetActive(true);
thisRatPack.SetActive(true);
Miss1Canvas.SetActive(false);
BeendenButt.SetActive(false);
}
if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf
&& !jalthiPack1[4].activeSelf &&thisMiss1conf && thisRatPack.activeSelf)
{
Miss1done = true;
thisMissaccImg.SetActive(true);
}
yield return new WaitForSeconds(0.1f);
}
i've tried that and all seems to work i'm just hoping that it will take effect even if its not noticeable.
I said that gameOver must NOT be true VERY FREQUENTLY, not never. So, if you should have a "sometimesTrue" variable ins$$anonymous$$d of nevertrue. Otherwise, it will never start the coroutine.
Also, the code inside xxx doesn't repeat at all, so you don't gain anything like this! However, let's introduce another variable "isRunning", which is true while the game is running. Then xxx becomes:
IEnumerator xxx()
{
While(isRunning)
{
this$$anonymous$$iss1conf = $$anonymous$$iss1confirmedScr.$$anonymous$$iss1confirm;
if(this$$anonymous$$iss1conf)
{
Einl_Sound_2.SetActive(true);
thisRatPack.SetActive(true);
$$anonymous$$iss1Canvas.SetActive(false);
BeendenButt.SetActive(false);
}
if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf
&& !jalthiPack1[4].activeSelf &&this$$anonymous$$iss1conf && thisRatPack.activeSelf)
{
$$anonymous$$iss1done = true;
this$$anonymous$$issaccImg.SetActive(true);
}
yield return new WaitForSeconds(0.1f);
}
}
Also, in this case, you don't need to start the coroutine inside Update() but only once. This can happen maybe inside Start(), or your own method StartGame().
e.g.
StartGame() //call to start
{
isRunning = true;
StartCoroutine("xxx");
}
EndGame() //to end the while loop in the coroutine
{
isRunning = false;
}
I hope this is more clear now.
oh oh me stupid :)
I've done it your way. Ins$$anonymous$$d an EndGame() i've added:
if($$anonymous$$iss1done) { isRunning = false; } yield return new WaitForSeconds(0.1f);
All seems to work and isRunning is set to false correctly. This way the Update() isn't necessary anymore !
many many thx for your help and patience !
just to make sure that my script is ok :
void Start ()
{
$$anonymous$$iss1confirmedScr = Einl1Obj.GetComponent<Einleitung_1>();
thisRatPack.SetActive(false);
Einl_Sound_1.SetActive(true);
Einl_Sound_2.SetActive(false);
isRunning = true;
StartCoroutine(xxx());
}
IEnumerator xxx()
{
while(isRunning)
{
this$$anonymous$$iss1conf = $$anonymous$$iss1confirmedScr.$$anonymous$$iss1confirm;
if(this$$anonymous$$iss1conf)
{
Einl_Sound_2.SetActive(true);
thisRatPack.SetActive(true);
$$anonymous$$iss1Canvas.SetActive(false);
BeendenButt.SetActive(false);
}
if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf
&& !jalthiPack1[3].activeSelf && !jalthiPack1[4].activeSelf &&this$$anonymous$$iss1conf && thisRatPack.activeSelf)
{
$$anonymous$$iss1done = true;
this$$anonymous$$issaccImg.SetActive(true);
}
if($$anonymous$$iss1done)
{
isRunning = false;
}
yield return new WaitForSeconds(0.1f);
}
}