We all know that adding the minecraft:tick tag to a function can make the function run once every tick, but this would create a really slow loop. The question is, how to complete a loop in a single tick, without using some sort of tail-recursion (because the call stack would be really deep thus lag the game, am I right?).

The tail-recursion pattern I'm talking about is like this

scoreboard players set global var 0
# Do something
scoreboard players add global var 1
execute if score global var matches ..(some number) run function (this function)

Since someone misunderstood my question, I'm going to edit this a bit more... BTW I'm not English native speaker so some words may not be so accurate...

My goal is to complete a loop in one single tick, not running something like 40 times a second evenly.

  • So you want to run a function for example 40 or 1000 times in a second, but in regular intervals? That's not possible, since everything in the game runs in 20 Hz ticks. Also, I'm pretty sure stack size doesn't contribute to lag. – Fabian Röling Sep 26 '18 at 11:17
  • @FabianRöling Question updated. – Chlorie Sep 26 '18 at 12:17
  • What do you mean with a "loop"? The game loop is already what a tick is. And a ticking function or a repeating command block already runs once per tick. What else do you want? – Fabian Röling Sep 26 '18 at 17:15

There's no proper call-stack with function recursion. For example if you call a function that creates and sets a score, that score will still be set when the function is exited.

When a function is called, its commands are essentially just appended to the front of the to-do list.

For this reason, you shouldn't worry about recursion depth. And, as there are no iteration constructs, recursion is the way you'd achieve what you want.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.