aboutsummaryrefslogtreecommitdiff
path: root/docs/modding/squirrel/loops-functions-and-if.md
blob: 3a2b8efb6a4a332c3cf294d7176cddebc9dc6432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Loops, Functions and if statements
===========================

The vast majority of GNUT modding within northstar will be done through functions, so understanding the formatting of functions can be somewhat important

Declaring Functions
--------------------
Functions in squirrel are first defined by stating both the **output** followed by the keyword **function**, for example if you wanted to define a function that returns TRUE or FALSE you would:

```cpp
bool function ReturnTrueOrFalse()
```

But what if I want my function not to give stuff, but to DO stuff? for that you can define your function as `void`, this indicated that your function does not return anything. for example:

```cpp
void function ThisDoesStuff()
```

If statements
---------------
If statements use a similar style to most programming languages and will execute their asigned code if the test returns the boolean value true, it is worth remembering that unlike some languages the interger 1 is NOT considered equal to the boolean true, true is also always expressed in lowercase when used by a bool function. if i wanted to have something occur if, and only if, our previous `ReturnTrueOrFalse` function returned true, then you can use:

```cpp
if(ReturnTrueOrFalse())
```

If statements are also functions however, and can be used to determine true or false on their own when provided the == (equal to?) != (not equal to) < (less than) or >(greater than) symbols, if I wanted to simulte rolling a dice and only execute code on a 5 then i could instead:

```cpp
if(RandomInt(5)+1 == 5)
```


Loops
------
Loops fall into a few categories but for our purposes we will be only using `foreach` and `while`. `foreach` loops are given a data set, such as a list, and will repeat their asigned script for each entry on that list.
```cpp
array<interger> someinformation = [1,2,3,4,5,6]
foreach( interger information in someinformation)
```
while a `foreach` loop will occur only as many times as the length of the list. a `while` loop functions more like an `if` statement, repeating itself endlessly until the test is no longer true
```cpp
while(ReturnTrueOrFalse)
```
This script will repeat endlessly until `ReturnTrueOrFalse` returns false

Formatting of actions
-------------------
So great, we can loop and check things, but what can we do with this information? Squirrel uses {} to denote the contense of a series of actions caused by such a statement, however single-line scripts can be used without these, and will just be assumed.

For example, lets make our ReturnTrueOrFalse function, that randomly picks either true or false, first:
```cpp
bool function ReturnTrueOrFalse()
  return(RandomInt(1) == 1)
```
As this is a 1 line function it can be executed without needing any {}, but for a longer function we might need one, now lets make a more complicated function that will use the previous script to determine true or false, then each time it returns true it will print each number in the `someinformation` array
```cpp
array<interger> someinformation = [1,2,3,4,5,6]
void ThisDoesStuff(){
  while(ReturnTrueOrFalse()){
    foreach( interger information in someinformation){
      print(information)
    }
  }
}
```