0
\$\begingroup\$

I have problem, when i override a Coroutine loop from the base code. But it seems that it only execute once. While on the base code it does what it suppouse to do and that is execute forever.

protected virtual IEnumerator OnThink(float interval)
{
    while (true)
    {
        yield return new WaitForSeconds(interval);
    }
}

protected override IEnumerator OnThink(float interval)
{
    Debug.Log("Execute");
    yield return StartCoroutine(base.OnThink(interval));
}
\$\endgroup\$
6
  • \$\begingroup\$ Can you show us where you call your overridden version? \$\endgroup\$
    – DMGregory
    Aug 7, 2018 at 11:16
  • \$\begingroup\$ You haven't surrounded the overridden Coroutine in any kind of loop so it will only run the once. \$\endgroup\$ Aug 7, 2018 at 13:01
  • \$\begingroup\$ @DMGregory it's is called from another class. \$\endgroup\$
    – Printer
    Aug 7, 2018 at 16:13
  • \$\begingroup\$ @Pheonix2105 could you provide me with an example? \$\endgroup\$
    – Printer
    Aug 7, 2018 at 16:13
  • \$\begingroup\$ That's totally OK. We'll accept a relevant code example no matter which class it's sitting in. \$\endgroup\$
    – DMGregory
    Aug 7, 2018 at 16:16

1 Answer 1

1
\$\begingroup\$

The base method IEnumerator OnThinkyou are overriding, overriding means you want to implement all the logic yourself instead of what the method class provides

//base
protected virtual IEnumerator OnThink(float interval)
{

    while (true)
    {
        yield return new WaitForSeconds(interval);
    }
}

//so this function is now overridden meaning it has no logic 
//unless you add it or call the base
//to make it work like the base implementation you just add the while loop
protected override IEnumerator OnThink(float interval)
{
    while(true)
    {

    Debug.Log("Execute");
    yield return StartCoroutine(base.OnThink(interval));
    }
}

This particular while loop will run forever since true always will equal true.

while(true) is short for hand for while(true == true) so will run forever, if you add your own boolean for example

bool canThink = false;

then use that instead of true = true like so

    private bool canThink = false;

    protected virtual IEnumerator OnThink(float interval)
   {
     while (true)
     {
        yield return new WaitForSeconds(interval);
     }
    }

protected override IEnumerator OnThink(float interval)
{
   //for every X interval if canthink is true run the code inside the loop 
   while(canThink == true)
   {
    Debug.Log("Execute");
    //we also just call yield return new WaitForSeconds ourselves
    //in this case there isnt much point in calling the base method.
    yield return new WaitForSeconds(interval);
   }
}

This way you control the flow of the logic, you can stop and start it just by changing canThink - bare in mind you probably don't want to be calling base in your case as a infinite loop inside a infinite loop will either crash or slow things down a fair bit.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .