- Home /
Can I use StartCoroutine inside of the same coroutine with a new variable?
I have a couroutine which takes a class of mine, "Wire", as a parameter.
This coroutine, changes a boolean state on this wire class, and then calls the same coroutine on the 1-5 other wires stored in its class instance as a list to also change their state (if, of course, they need to be changed, to avoid an infinite back-and-forth loop)....which then of course calls the same coroutine passing those, etc etc.
Does this count as garbage? Is there a way to trigger that coroutine without it being nested? Am I correct to say that, this means, the coroutine called on the first wire is not stopping until the last wire in the chain's coroutine stops?
Here is the coroutine in question. "pulser" is just taking a single reference to a single wire object, and calling this coroutine, passing that wire....that wire, in this snapshot of the profiler, is connected to a line of other wires.
public IEnumerator RefreshOneSimple(WireNode theNode){
WireNode temp;
//For every wire this one is connected to,
foreach (Behaviour conn in theNode.ports) {
if (conn != null) {
if (conn.name.Contains ("WireNode")) {
temp = conn as WireNode;
if (temp.state != theNode.state) {
//If it is not the same state as this one, change that now.
temp.state = theNode.state;
foreach (LineRenderer cable in theNode.wirelines) {
if (theNode.state == true)
cable.materials [0].color = theNode.onColor;
if (theNode.state == false)
cable.materials [0].color = theNode.offColor;
}
foreach (LineRenderer cable in temp.wirelines) {
if (temp.state == true)
cable.materials [0].color = theNode.onColor;
if (temp.state == false)
cable.materials [0].color = theNode.offColor;
}
//yield return null;
StartCoroutine (RefreshOneSimple (temp));
yield return null;
}
//Now do the same thing with each one of those, and then let the next frame happen.
}
if (conn.name.Contains ("Powerable")) {
//print ("Doing powerable in enum");
Powerable p = conn as Powerable;
p.powered = theNode.state;
p.StartRefresh ();
yield return null;
}
} else {
}
}
yield return null;
}
EDIT: I thought, well, if instead of calling this routine inside of itself, I could call a new routine which simply starts the original routine with the new variable, then once that new "StartCoroutine" exits, the first one can disappear.
But, this resulted in, only my first wire updating. So I reverted.
Edit: In case I am not clear, I only need any of this to happen when a wire has its state changed. I.e., during the moment a lever is switched.