Repeat

From Castle Wiki
(Redirected from Loop)

Repeat is a Response which repeats other responses.

Repeat N times

Repeat N times immediately repeats the enclosed responses a predetermined number of times.

For example, you might have a Rule which drops three apples out of a tree. One way to do this would be to write three responses: Create apple, Create apple, Create apple. Instead, you could write Repeat 3 times: Create apple. Both approaches do the same thing, but the second one is easier to read, and easier to change later.

Note that N can be an Expression. For example, you could use the Value of a variable expression to write: Repeat $apples times: Create apple.

Repeat every N seconds

Repeat every N seconds repeats the enclosed responses forever, but waits for a fixed time interval in between each repeat.

For example, to create a rain drop every half second, you could write: Repeat every 0.5 seconds: Create rain drop.

Instead of seconds, you can optionally use units from the Clock. For example, to move an actor downward on every beat, you could write: Repeat every 1 beat: Set Y position to 1 (relative).

The time interval N is not allowed to be an Expression, which means it can't be changed after the repeat has started. If you need to repeat with a varying amount of time between each repeat, you can use a Wait response inside Repeat N Times.

Repeat every frame

Sometimes it can be useful to repeat logic as fast as possible over time. Currently, the best way to do this is to use Repeat every N seconds with the minimum possible value for N, which is bounded at 0.0167 (1/60).

Stop repeating

Use the Stop Repeating Response to stop repeating. This will prematurely break out of a Repeat N Times, and it is the only way to exit a Repeat every.

Repeats are also interrupted at the next iteration if the owning Actor is destroyed.

Count the number of repeats

It can be useful to know how many repeats have happened so far. For example, you might want to create a row of cheeseburgers without having any of the cheeseburgers overlap. You can use the Number of repeats finished Expression for this:

Repeat 5 times: Create Cheeseburger at absolute position (The number of repeats finished, 0).

This will create cheeseburgers at (0, 0), (1, 0), (2, 0), (3, 0) and (4, 0).