Hello Guest

Author Topic: MoleScript Question  (Read 4442 times)

indy2005

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
MoleScript Question
« on: September 02, 2013, 08:42:15 pm »
Hi,

I am new to coroutines, and am looking through the MainGameScript.

I have a few questions about the code below.

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: MoleScript Question
« Reply #1 on: September 02, 2013, 10:07:16 pm »
yield return null means "wait for the next frame".
yield return 0 is the same.

yield return StartCoroutine(OkToTrigger());
will wait for the OkToTrigger coroutine to complete.

indy2005

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: MoleScript Question
« Reply #2 on: September 02, 2013, 10:59:52 pm »
Thanks, why is a wait for the next frame necessary here?

Code: [Select]
yield return null;
molesActive = 0;

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: MoleScript Question
« Reply #3 on: September 03, 2013, 09:15:56 am »
The reason I put the yield there (after the 'do') rather than at the end (before the 'while') was because if it had been just before the 'while', the count of the number of active moles would have been done, then a yield to the next frame, then the test while(molesActive >= moleLimit) would have been done, but with the yield just after the 'do', then the count and the test can be done in the same frame and drop out of the loop if there aren't enough active moles.  Strictly speaking it doesn't make a great deal of difference and you probably wouldn't notice it in the game.

If you're asking why the yield is there at all - 1)  The function needs to run once per frame, 2) if the yield wasn't there loop would continue running unnecessarily until the condition was met to drop out of the loop - it only needs to be checked once per frame.
« Last Edit: September 03, 2013, 09:27:04 am by gary-unikronsoftware »