Hi,
I am new to coroutines, and am looking through the MainGameScript.
I have a few questions about the code below.
private IEnumerator MainGameLoop()
{
float hitTimeLimit = 1.0f;
int randomMole;
while(!gameEnd)
{
yield return StartCoroutine(OkToTrigger());
yield return new WaitForSeconds((float)Random.Range(1, timeLimitMS) / 1000.0f);
randomMole = (int)Random.Range(0, moles.Count - 1);
while(moles[randomMole].sprite.gameObject.activeSelf)
{
randomMole = (int)Random.Range(0, moles.Count - 1);
}
moles[ randomMole ].Trigger(hitTimeLimit);
hitTimeLimit -= hitTimeLimit <= 0.0f ? 0.0f : 0.01f; // Less time to hit the next mole
yield return null;
}
}
public void RegisterMole(MoleScript who)
{
moles.Add(who);
}
// Currently only 3 moles at a time can be active. So if there are that many, then we can't trigger another one...
private IEnumerator OkToTrigger()
{
int molesActive;
do
{
yield return null;
molesActive = 0;
foreach(MoleScript mole in moles)
{
molesActive += mole.sprite.gameObject.activeSelf ? 1 : 0;
}
}
while(molesActive >= moleLimit);
yield break;
}
Could you explain this to me please:
yield return StartCoroutine(OkToTrigger());
and why this 'yield return null' is needed (i.e. what does it do as it seems to work when commented out:
do
{
yield return null;
molesActive = 0;
foreach(MoleScript mole in moles)
{
molesActive += mole.sprite.gameObject.activeSelf ? 1 : 0;
}
}
Many thanks.
I am also wondering when to do a yield return 0 instead of yield return null...but that's a 'brucie bonus' ;-)
i