Sub-tick loops in functions without recursions
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.
Solution 1:
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.