Skip to reading
GDScript Programming & Godot

ForEach()

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
On this page
Code example
ForEach(collection_value: Variant, callback: Callable) -> int

Visit array values or dictionary key/value pairs through a Callable.

Returns: Number of entries processed.

Code example
ForEach(["key", "note"], func(value, index): print(index, value))

Array callback gets value,index. Dictionary callback gets key,value,index. Packed arrays are not handled by this helper. It does not await callbacks.

WhileLoop()#

Code example
WhileLoop(condition: Callable, body: Callable, safety_limit: int = 100000) -> int

Check a Callable condition before each Callable body pass.

Returns: Number of iterations.

Code example
WhileLoop(func(i): return i < 3, func(i): print(i))

Both receive the current index. Safety limit prevents unbounded helper iterations; it does not make blocking work asynchronous.

ForLoop()#

Code example
ForLoop(count: int, body: Callable) -> int

Call a body with indices 0 through count-1.

Returns: Number of completed passes.

Code example
ForLoop(3, func(i): print(i))

Negative counts produce no passes. For asynchronous work, prefer an ordinary GDScript for loop with await in its body.

Page 72 of 378Permalink
Go to page