All the posts on recursion I’ve read is for triggers, but never for blocks somehow. In Python, C, Java, and many other languages, recursion is calling a function within itself, creating a sometimes, more efficient, loop. Recursion is one of two types of functions, the other being iterative functions, where instead of calling itself, it repeats a certain part of the code multiple times.
For example, a recursive function for calculating fibonacci in Python, would look like this:
while an iterative function would look like this:
In Gimkit, we can make recursive functions using blocks, by utilizing the “Broadcast message on channel” block.
First, we need to make a counter variable so the function doesn’t run indefinitely. In order to do this, we can make a property named “Counter”, and set it to 10 (or however number of times you want it to run).
Next, we need to make a lifecycle, that will run on channel “Start Game”, when the game starts.
In this example, we will be using a text as the means to access the block and the channel will be “Start Game”.
In order to make it stop after a certain amount of loops, we can make it so that if the counter > 0 it will run, if not, it will stop.
Now in that if statement, we can decrement the counter by one, and broadcast message on “Start Game” calling itself again.
Now directly before it decrements the counter, you can run whatever action you need to loop.
You are now finished, and can continue to explore this new idea of recursive functions with gimkit blocks