Is it possible to run multiple Forever Blocks?

I want to know if it’s possible to run multiple forever blocks in sequence without using the Break Block. I’ve been programming robots with blocks for a couple of years now, and I’ve reached a barrier one of my codes. It’s with the Forever Block.
I know that once a Forever block starts running, the blocks below it won’t work unless I use the break block, in which the blocks attached to the Forever Block starts running, which is a big hassle. I want to find another method so I can run this forever while running another thing forever without the break block.

My Potential Answer: Put a forever block in another forever block.

If my answer is incorrect, please correct me. Your feedback would be appreciated a lot so I can finish my current code.

Yes you can indeed, however to do it you need to use the broadcasting blocks so multiple forever blocks will run at the same time.

4 Likes

I never though about using broadcasting blocks to run them. I’ll definitely try that!
Thank you for the quick response!

1 Like

You can also use multiple “when started” hat blocks to run multiple things at the same time.

7 Likes

This also worked with another one of my old codes.
With my recent code though, using these would’ve been so tedious as I’d have to add tons of If-Then blocks.
Plus, with me using a 1st generation Brain, I can only add three ‘when started’ hat blocks.
But this is also a good solution.

There’s a reason for that: the brain can only handle trying to do about three things at once. You could “get around” the hat block limit by broadcasting, but the fundamental reason for its existence is still there. If you’re trying to run more than three forever loops at the same time, I advise you to add wait 0.03 seconds blocks in at least some of them to avoid overloading the brain.

2 Likes

With IQ it automatically adds the delay to each task. If you look at the code preview it shows the wait

//Copied from a sample program with two 'When Started' blocks.
// "when started" hat block
int whenStarted1() {
  while (true) {
    Brain.Screen.print("Task 1 is Running");
  wait(20, msec);
  }
  return 0;
}

// "when started" hat block
int whenStarted2() {
  while (true) {
    Brain.Screen.print("Task 2 is Running");
  wait(20, msec);  
  }
  return 0;
}

int main() {
  vex::task ws1(whenStarted2);
  whenStarted1();
}
2 Likes

I was recommending an additional wait. It may not be necessary.

1 Like

It’s typically poor coding practice to create nested loops. They lead to several problems with the code getting stuck in specific areas which prevents other code from running. Quite often you can use one Forever block with subsequent If / else blocks or custom My Block to separate the tasks.

2 Likes