Difference between revisions of "Repeat"

From Castle Wiki
(description)
 
(Restructure Repeat article)
Line 1: Line 1:
The Repeat function repeats a group of responses in a cyclical manner. Repeat functions can be forced to stop by using the Stop Repeating function.  
'''Repeat''' is a [[Response]] which repeats other responses.  


===Types of Repeat functions:===
==Repeat N times==
'''Repeat N times''' immediately repeats the enclosed responses a fixed number of times.


* Repeat n 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.
* Repeat every n seconds


=== Repeat n times ===
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 n times repeats a group of responses n amount of time/s before stopping. It requires a whole number input. If by any chance a fractional number(a number that has decimals, e.g. 4.2, 6.9) is inputted, it will round it down automatically. Repeat n times does not have a time interval between repeats and will repeat the group of responces n times instantaneously.


Repeat n times is commonly used when a particular group of responses is needed to be repeated in an exact amount of times.  
== Repeat every N seconds ==
'''Repeat every N seconds''' repeats the enclosed responses forever, but waits for a fixed time interval in between each repeat.


=== Repeat every n seconds ===
For example, to create a rain drop every half second, you could write: '''Repeat every 0.5 seconds: Create rain drop'''.
Repeat every n seconds repeats a group of responses forever with n second/s time intervals. It requires any real number as an input. The inputs of this function is clamped from the minimum number 0.0167, to the maximum number 30. This repeat function will only stop with the use of the Stop Repeating function or when the Actor that holds this function is destroyed.


Repeat every n seconds is commonly used if a particular group of responses needs to be repeated forever.
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)'''.
 
== 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 relative position (The number of repeats finished, 0)'''.
 
This will create cheeseburgers at (0, 0), (1, 0), (2, 0), (3, 0) and (4, 0).
#
#

Revision as of 17:06, 12 May 2022

Repeat is a Response which repeats other responses.

Repeat N times

Repeat N times immediately repeats the enclosed responses a fixed 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).

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 relative position (The number of repeats finished, 0).

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