Scripting help
Repeating
Do some steps again and again: a set number of times, as many times as a @variable says, counting with repeat for, through a list, until a check is true, or forever, and stop early with stop repeating.
A repeat does the same steps more than once. Put the steps between a repeat line and an end line, and the character goes through them again and again before it moves on.
This character jumps three times, with a second between jumps, then says "Done!". Press the play button to try it, then change the 3 and try again.
In Blocks, a repeat is a steel blue block that holds its steps inside it. In Code, the steps go on the lines under repeat, indented, and the end line closes it.
The kinds of repeat
| Written like | How many times it goes round |
|---|---|
repeat 3 times | 3 times. The number can be from 1 to 20. |
repeat 1 time | Once. Handy while you try out the steps inside. |
repeat @count times | As many times as @count is when the repeat starts. |
repeat @count + 1 times | Sums work too, worked out when the repeat starts. |
repeat (the bigger of @a, @b) times | A function in brackets works too, run when the repeat starts. |
repeat for @count from 1 to 5 | 5 times, with @count as 1, then 2, up to 5. See Counting with repeat for. |
repeat until @count is 5 | Until the check is true. See Repeating until a check is true. |
repeat for @month in @months | Once for each value in a list. See Going through a list. |
repeat all {Enemies} | Once for each one in a family. See Going through a family. |
repeat forever | Again and again, until a stop repeating or another rule takes over. |
repeat on its own | The same as repeat forever. |
A number bigger than 20 is turned down to 20, and the box under the script tells you. repeat 0 times skips the steps inside, just as a @count of 0 does.
Repeating with a @variable
repeat @count times reads @count once, when the repeat starts. Changing @count inside the repeat doesn't change how many times it goes round.
When @count is | What happens |
|---|---|
| 0, or less | The steps are skipped. |
| Words, or nothing | The steps are skipped. |
| More than 20 | It goes round 20 times. |
| 2.6 | It goes round 3 times, the nearest number. |
Each time this character gets hit, it says "Ouch!" one more time than before. Using @variables to remember has more about @variables.
Repeating forever
repeat forever keeps going until something stops it. That makes it good for a character that does the same thing over and over, like a guard walking back and forth.
A repeat that goes forever doesn't freeze the game. If one round has no step that takes time, like wait or move, it goes round once each frame, not hundreds of times at once.
Repeating until a check is true
repeat until and a check keeps going round until the check is true. The check can be anything an if can check, like the {Hero} is near or @count is 5. See Choosing with if and end for the checks.
This character calls out every second until the hero comes near. Then it says "There you are!".
When it checks
The check is made at the start of every round, before the steps. If the check is true already when the repeat starts, the steps are skipped, and the character carries on after the end.
It only checks at the start of a round. If the hero comes near during wait 1 second, the wait finishes first, and the repeat ends at the next check. To stop straight away, put an if with stop repeating inside, as in Stopping early.
A check that never comes true keeps going like repeat forever, and it doesn't freeze the game either.
Counting up to a number
This character says "Get away!" five times. After the fifth time, @count is 5, so the check is true and the repeat ends. To count, `repeat for` is shorter.
Repeating while something is true
A repeat always goes until something is true, so there's no repeat while. Turn the check round instead.
| To repeat while | Write |
|---|---|
| The hero is near | repeat until the {Hero} is far |
@count is under 5 | repeat until @count is at least 5 |
| Any other check is true | repeat until not and then the check |
Counting with repeat for
repeat for and a @variable counts as it goes round. The @variable starts at the first number, and changes by 1 each time round until it gets to the second number.
This character counts down. It says "3...", then "2...", then "1...", and then "Go!".
The ways to count
| Written like | What @count is each time round |
|---|---|
repeat for @count from 1 to 5 | 1, 2, 3, 4, 5. |
repeat for @count from 5 to 1 | 5, 4, 3, 2, 1. It counts down when the first number is bigger. |
repeat for @count from 0 to 10 by 2 | 0, 2, 4, 6, 8, 10. by says how much it changes each time. |
repeat for @count from 1 to 10 by 4 | 1, 5, 9. It stops before it would go past 10. |
repeat for @count from 1 to @lives | The ends can be @variables, sums, or functions in brackets. They're read when the repeat starts. |
It always goes round at least once, so from 4 to 4 goes round once, with @count as 4. Words or nothing count as 0.
by is always more than 0, as the ends already say which way it counts. by -2 gets a red note. When by is a @variable that holds 0 or less, the repeat is skipped, and the script log says why.
The repeat's own @variable
The @variable in a repeat for belongs to the repeat. Only the steps inside it can see it, and every character has its own. Two goblins can count at the same time without mixing up their numbers, and a @variable with the same name elsewhere in the game isn't changed.
After the end, it's gone. To keep the number, copy it into another @variable inside the repeat, like @last is @count.
Changing it inside the repeat, like @count goes up by 10, lasts until the end of that round. The next round carries on counting from where it was.
A long count doesn't freeze the game. Like repeat forever, it goes round at most once a frame when nothing in it takes time.
Going through a list
repeat for and in goes through a list, one value at a time. Each time round, the @variable after for is the value it's on now.
This script logs "Hello, Jan!", then "Hello, Feb!", then "Hello, Mar!".
Knowing where you are in the list
Put two @variables after for, with a comma between them. The first one is the value's place in the list, counting from 1. The second one is the value.
This script logs "Number 1 wins a gold cup", then "Number 2 wins a silver cup", then "Number 3 wins a sticker".
The place is handy with item … of, to find the value in the same place in another list, like @price is item @place of @prices.
How it goes through
| When | What happens |
|---|---|
| The list has nothing in it | The steps are skipped. |
| You add to or remove from the list inside | It goes through the list as it was when the repeat started, so no value is skipped or done twice. |
| The @variable holds one value, not a list | It goes round once, with that value. The builder gives a note. |
| You go through a family | repeat for @ghost in {Ghosts} works like repeat all {Ghosts}, with @ghost in place of {this one}. |
The @variables after for belong to the repeat, like a counting one does. Only the steps inside can see them, and they're gone after the end. Their names need to be different from each other and from the list's.
Going through a family
repeat all and a family goes round once for each one in it. Inside the repeat, {this one} is the one it's on now. It works anywhere an ID does.
This level script hides every enemy, one at a time, half a second apart.
Which ones, and in what order
| Family | Who it goes through |
|---|---|
{Enemies} | Every enemy in the hero's level that hasn't been beaten. |
{Heroes} | The heroes. |
Your own, {Ghosts} | Everything in that family in the hero's level, still there. |
It goes through the ones there when the repeat starts, top row first, and left to right along each row. One that turns up later isn't in this round.
all {Enemies} attack and repeat all {Enemies} are different. all {Enemies} attack tells them all at once, on one line. repeat all goes one at a time, so you can wait between them, or tell each one something different.
{this one} only works inside a repeat all. In a repeat inside another one, it's the inner repeat's.
repeat all @ghosts goes through the things a @variable holds, the same way. See Remembering things.
Stopping early
stop repeating leaves a repeat straight away. The character carries on with the line after the repeat's end.
Put it under an if, and the repeat runs until the check is true. See Choosing with if and end for the checks you can make.
This character wanders and calls out, up to 20 times. As soon as the hero comes near, it stops looking and gives chase.
To keep looking for as long as it takes, write repeat until the {Hero} is near in place of repeat 20 times, and leave out the if.
A stop repeating outside a repeat gets a red note, and the line is skipped.
A repeat inside a repeat
A repeat can go inside another repeat. The inside one runs all the way through on every round of the outside one.
This character jumps twice, rests for 2 seconds, and does that 3 times: 6 jumps in all.
stop repeating only leaves the repeat it's in, the inside one. The outside repeat carries on with its next round.
When another rule takes over
In a character's script, only one rule runs at a time. When another rule starts, like when I get hit, it takes over, and the repeat stops where it was. It doesn't pick up again afterwards.
So a repeat forever in when I start runs until something else happens. To go back to it, start it again from the other rule, or put the repeat in a function that both rules can use.
Triggers are different: a when "alarm" puts the rule on hold, and the repeat carries on where it was once the trigger's rule is done.
Good to know
| When | What happens |
|---|---|
A repeat has no end | A red note says so, and the repeat is skipped. end repeat works as the closing line too. |
| A repeat has nothing inside it | A note says there's nothing to repeat yet. |
| A line inside is less indented | It's still inside the repeat until the end, and a grey tip says so. |
| You want to see each round | Put log in front of the repeat. The Script log says time 2 of 3 each time round. |
You log a repeat until | The log says the check's answer each time round, like repeat until @count is 5: no, @count is 2, so time 3. |
You log a repeat for | The log says the @variable each time round, like repeat for @count from 1 to 5: @count is 2, time 2 of 5. |
You log a repeat for … in | The log says the value each time round, like repeat for @month in @months: @month is "Feb", time 2 of 3. |
| A repeat is in a function | It works just the same. A give back inside it leaves the repeat and the function together. |

