Skip to content

Commit e437217

Browse files
authored
while page
1 parent f9062ec commit e437217

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

content/while.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
1-
TBD
1+
Many things in the game are extended in time or depend on the player feedback. A car driving to a destination, a model file loading from the disk, a button awaiting to be pressed all require a specific condition to become true. Did the car reached the point, was the specific button pressed, is the model available for use? If not, wait and check again later.
2+
3+
We can validate certain condition continuosly, each frame, or with some delay. To achieve this in the code, a special construct exists: a loop.
4+
5+
The most common type of loop is a while loop. It will execute a block of code as long as the condition is true.
6+
7+
```
8+
while <condition>
9+
<block of code>
10+
end
11+
```
12+
13+
For example, if we want to do something while the player can play (i.e. not dead or caught by the police), we can write:
14+
15+
```
16+
while is_player_playing 0
17+
wait 0
18+
end
19+
20+
terminate_this_script
21+
```
22+
23+
The `wait 0` command is used to yield the execution for a frame, allowing the game to process other events and not freeze. It is crucial to have a `wait` command inside the loop body.
24+
25+
Once the condition becomes false, the loop stops and the execution moves to `terminate_this_script` command.
26+
27+
The loop condition can be negated using the `not` operator. For example, if we want to wait until the model becomes available, we can write:
28+
29+
```
30+
while not is_model_available 42
31+
wait 250
32+
end
33+
```
34+
35+
The script will be continuously checking if the model 42 is available, processing other game events between iterations.

0 commit comments

Comments
 (0)