- Home /
Can i check if a coroutine is running?
hello, i have multiple coroutines and i want certain ones to run in certain conditions so my simple question is can i have an if statement were i check if a coroutine is running?
so kind of like:
if( coroutineX is running)
{
//do stuff
}
thanks!
Answer by Headworker · Sep 28, 2013 at 07:37 PM
The answer may be quite simple:
Just use a bool like this: bool CR_running;
void InvokeMyCoroutine()
{
StartCoroutine("Coroutine");
}
IEnumerator Coroutine()
{
CR_running = true;
//do Stuff
yield return //Whatever you want
CR_running = false;
}
// As long as you a within this scope you can just do :
void AnotherFunction()
{
if (CR_running)
{
// Do some other stuff;
}
}
I'm not completely sure, but I think your method will not set the CR_running flag to false, because it will already yield before it. I think that you have to set CR_running to false before you yield return.
If you use it with a
yield WaitforSeconds / WaitForEndofFrame
or any other wait function, it does work, Ive been using it for quite a while now.
This worked for me and is the best answer imo
although I used
if (crRunning == false) {
StartCoroutine ("StartCorRechargeEnergy");
}
ins$$anonymous$$d of
if (crRunning) {
StartCoroutine ("StartCorRechargeEnergy");
}
I think it won't. So you just have to manually set the bool to false after StopCoroutine
Answer by $$anonymous$$ · May 20, 2013 at 09:10 AM
I don't know if this is they way they have done it in the program Barrett Fox talks about, but one way I have used is this:
You can hijack the IEnumerator like this:
IEnumerator someCoroutine = aCoroutine();
Coroutine startSomeCoroutine = StartCoroutine(someCoroutine );
while (someCoroutine.MoveNext())
{
Debug.Log("Im running");
yield return null;
}
yield return startSomeCoroutine;
So when the loop is over, the routine is done.
Problem with this is you are running the coroutine twice as fast, which may or may not be an issue. The coroutine will recieve a movenext from you and from unity.
I see, that was not a problem in my solution though. But obviously that is not the best solution then. Is there a way to use my idea without doing two $$anonymous$$oveNext's?
Answer by Epholl · Apr 02, 2015 at 07:39 AM
I had a similar problem and came with a solution based on Headworker's.
public IEnumerator Coroutine() {
coroutineRunning = true;
while (courutineCondition) {
DoStuff();
yield return null;
}
coroutineRunning = false;
}
Wouldn't this be the simplest and the easiest implementation? What could be wrong with this?
Answer by GingerLoaf · Jun 03, 2015 at 10:23 PM
Why not create a quick framework to handle this? After reading all the comments on this page, I had several ideas of my own that all begin with my own custom StartCoroutine method.
PLEASE NOTE: This is experimental code only and is not optimized at all. This is only to serve the purpose of a prototyped idea.
I would do something similar to this:
public class TrackedCoroutineBehaviour : MonoBehaviour {
private List<string> runningCoroutinesByStringName = new List<string>();
private List<IEnumerator> runningCoroutinesByEnumerator = new List<IEnumerator>();
public Coroutine StartTrackedCoroutine(string methodName)
{
return StartCoroutine(GenericRoutine(methodName, null));
}
public Coroutine StartTrackedCoroutine(IEnumerator coroutine)
{
return StartCoroutine(GenericRoutine(coroutine));
}
public Coroutine StartTrackedCoroutine(string methodName, object parameter)
{
return StartCoroutine(GenericRoutine(methodName, parameter));
}
public bool IsTrackedCoroutineRunning(string methodName)
{
return runningCoroutinesByStringName.Contains(methodName);
}
public bool IsTrackedCoroutineRunning(IEnumerator coroutine)
{
return runningCoroutinesByEnumerator.Contains(coroutine);
}
public void StopTrackedCoroutine(string methodName)
{
if (!runningCoroutinesByStringName.Contains(methodName))
{
return;
}
StopCoroutine(methodName);
runningCoroutinesByStringName.Remove(methodName);
}
public void StopTrackedCoroutine(IEnumerator coroutine)
{
if (!runningCoroutinesByEnumerator.Contains(coroutine))
{
return;
}
StopCoroutine(coroutine);
runningCoroutinesByEnumerator.Remove(coroutine);
}
private IEnumerator GenericRoutine(string methodName, object parameter)
{
runningCoroutinesByStringName.Add(methodName);
if (parameter == null)
{
yield return StartCoroutine(methodName);
}
else
{
yield return StartCoroutine(methodName, parameter);
}
runningCoroutinesByStringName.Remove(methodName);
}
private IEnumerator GenericRoutine(IEnumerator coroutine)
{
runningCoroutinesByEnumerator.Add(coroutine);
yield return StartCoroutine(coroutine);
runningCoroutinesByEnumerator.Remove(coroutine);
}
}
Answer by Barrett-Fox · Jun 06, 2011 at 07:12 PM
This script on the Assest Store ($25) has an "isRunning()" method which is what you're asking for. But I would be interested to hear an answer about how to do this directly.
this does seem better but i would like to try "IsInvoking" before i spend moeny and i am not at the right computer to test it right now but thanks for all the help guys
I'm pretty sure my answer was wrong, looking back on it. It's possible you could have your coroutines set a static flag somewhere when they start, and clear them when they're done. But I think using this Asset Store thing is probably going to be better in the long run.
is not the same. "invoke" set a timer and return nothing. "startcoroutine" return a Ienumerator.
I wouldn't recommend this answer because it's asking someone to spend $25 when there are free answers below
Don't buy that, see @Headworker answer ins$$anonymous$$d!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
NullReferenceException in StartCoroutine 1 Answer
Game architecture problem 0 Answers
problems with coroutines 0 Answers