Nodes
While Loop#
The While Loop node in Magick allows you to repeatedly execute a series of nodes as long as a specified condition remains true. It's useful for implementing iterative logic and loops within your spells.
Inputs#
flow(required): The entry point for the loop. Connect the node(s) that should execute before the loop begins.condition(required): A boolean value determining whether the loop should continue. The loop will run as long as this input istrue.
Outputs#
completed: Triggers when the loop has finished executing, either because the condition becamefalseor the maximum iterations were reached.loopBody: Connects to the node(s) that should execute on each iteration of the loop.
Configuration#
maxIterations: The maximum number of times the loop is allowed to run before forcibly exiting. This prevents infinite loops. Default is 10.
Instructions#
- Add a While Loop node to your spell.
 - Connect the node(s) that should run before the loop starts to the 
flowinput. - Connect a node that outputs a boolean value to the 
conditioninput. This will be checked at the start of each iteration to determine if the loop should continue. - Connect the 
loopBodyoutput to the first node that should run on each loop iteration. - Chain additional nodes to implement the logic that should repeat each iteration.
 - Connect the final node of the loop body back to the While Loop node's 
flowinput to complete the loop circuit. - Connect the 
completedoutput to any nodes that should run after the loop has finished. - If needed, adjust the 
maxIterationsconfiguration value to set an appropriate limit for your use case. 
Example#
Here's an example spell that uses a While Loop to count down from 10 to 1:
- Add a Variable node configured with the name "count" and an initial value of 10.
 - Connect the Variable node to the 
flowinput of a While Loop node. - Add a Greater Than node and connect the Variable to both its inputs. This will output 
trueas long as count is greater than 0. - Connect the Greater Than node to the While Loop's 
conditioninput. - Connect the While Loop's 
loopBodyto a Subtract node. Connect the Variable to the Subtract node's first input and set the second input to 1. This will decrement count by 1 each iteration. - Connect the Subtract node's output back to the Variable to update its value.
 - Also connect the Subtract node to a Debug node to log the current value of count.
 - Connect the Debug node back to the While Loop node's 
flowinput. - Finally, connect the While Loop's 
completedoutput to a final Debug node with the message "Loop finished!". 
When run, this spell will log the values 10 through 1 to the debug console, then log "Loop finished!" once the loop condition becomes false (i.e. count is no longer greater than 0).
Best Practices#
- Keep loop bodies as small and focused as possible. If a loop is becoming long or complex, consider refactoring the logic into a separate spell and invoking it as a module.
 - Be mindful of the 
maxIterationslimit. Choose a value high enough that the loop can complete under normal conditions, but not so high that a buggy infinite loop would hang your spell for too long. - Ensure there is logic in place to eventually make the loop condition 
false, otherwise the loop will run until the iteration limit and may not complete its intended task. - Avoid mutating loop control variables (like a counter) inside the loop body. Instead, use nodes like Increment/Decrement or Assign to update values in a controlled way.
 
Gotchas#
- The loop condition is only checked at the start of each iteration. If the condition becomes 
falsein the middle of an iteration, the current iteration will still complete. - If the loop completes by hitting the 
maxIterationslimit, thecompletedoutput will still trigger. Check the iteration counter if you need to determine whether the loop exited "naturally" or not. - Be cautious when using While Loop with asynchronous code. Ensure all async operations complete before concluding an iteration, or unintended behavior may occur.