Pencil Code Reference > forever

 

forever a simple loop

A forever loop runs as many times per second as possible, capping at around 30.

A forever loop will continue to run until stop() is used or the code execution is terminated.

Syntax

forever ->
  ...
  ...
  ...
forever fps, ->
  ...
  ...
  ...

If the "loops per second" arguement is used, a comma is required after it before the ->.

Unlike tick, multiple forever loops can be running simultaniously.

forever ->
  if pressed 'w'
    fd 2
forever ->
  if pressed 'a'
    lt 2
forever ->
  if pressed 'd'
    rt 2
forever ->
  if pressed 's'
    bk 2
forever ->
  if pressed 'w'
    fd 2
  if pressed 'a'
    lt 2
  if pressed 'd'
    rt 2
  if pressed 's'
    bk 2

IDs

Every forever loop has its own unique ID that is created when it is first called. This can be used in the stop() command to stop only a specific loop.

The ID can be found using the following method.

forever ->
  if pressed 'w'
    fd 2
forever ->
  if pressed 'a'
    lt 2
forever ->
  if pressed 'd'
    rt 2
id = forever ->
  if pressed 's'
    bk 2
button 'Stop Backwards Loop', ->
  stop(id)