|
1 |
| ---- |
2 |
| -description: In Progress |
3 |
| ---- |
4 |
| - |
5 | 1 | # Indentation and Program Flow
|
6 | 2 |
|
7 |
| -show examples for commands, for events, ifs, loops, sections |
| 3 | +Like any programming language, Skript needs some way of recognizing when code is meant to be 'within' other code, like what code a command should run, or what code should be run if an if statement succeeds. Many languages solve this with brackets, like `{}` or `()`. Skript, like Python, decides to solve this with indentation to keep the code you write cleaner and less cluttered. |
| 4 | + |
| 5 | +Indentation is a catch-all term for how far from the left margin your text starts. In Skript, we achieve this with either spaces or tabs, like so: |
| 6 | + |
| 7 | +``` |
| 8 | +I'm not indented. |
| 9 | + I've been indented by 1 tab. |
| 10 | + Only used two spaces were used for me. |
| 11 | + A whole ten spaces were used to indent me! |
| 12 | +``` |
| 13 | + |
| 14 | +## Basic rules of indentation |
| 15 | + |
| 16 | +Skript requires that for each structure you create (ie: an event, a command, a function), you stick to a consistent pattern of indentation. This means every line in that structure has to follow the same rules. If the first line uses 2 spaces for a single level of indentation, the next line can't use a tab, or 3 spaces. Apart from that, though, you can choose whatever amount of spaces OR tabs (not both!) that you want. |
| 17 | + |
| 18 | +``` |
| 19 | +# valid indentation: |
| 20 | +Not indented |
| 21 | + Indented by one tab |
| 22 | + Indented by two tabs! |
| 23 | + |
| 24 | +# valid indentation: |
| 25 | +Not indented |
| 26 | + Indented by three spaces |
| 27 | + Indented by six spaces |
| 28 | +
|
| 29 | +# invalid indentation |
| 30 | +Not indented |
| 31 | + Indented by one tab |
| 32 | + Indented by a tab and two spaces (BAD) |
| 33 | +``` |
| 34 | + |
| 35 | +The standard practice is to use 2 spaces, 4 spaces, or 1 tab as your indentation. Editors like VSCode have tools to automatically indent or convert indentation for you if you have issues. |
| 36 | + |
| 37 | +### When to indent? |
| 38 | + |
| 39 | +Indentation is required whenever you want code to fall under the control of something else. Let's say you have an event, `on chat`, that you want to run code inside of. You write the event itself with no indentation, but any code inside of it has to follow with a layer of indentation: |
| 40 | + |
| 41 | +```applescript |
| 42 | +on chat: |
| 43 | + # indent your code by one layer |
| 44 | + set message to ">> %message% <<" |
| 45 | +``` |
| 46 | + |
| 47 | +For commands it's much the same, but we have the first level taken up by the entries like aliases, cooldown, description, and the trigger. So we have to indent a second time below the trigger to tell Skript that we want the code to be attached to the trigger, specifically. |
8 | 48 |
|
9 |
| -Explain the exact situations in which indentation is used (sections, triggers, etc) |
| 49 | +```applescript |
| 50 | +command /test: |
| 51 | + # the entries are indented once |
| 52 | + aliases: t |
| 53 | + description: a test command |
| 54 | + trigger: |
| 55 | + # but then our code is indented a second time to put it under the trigger. |
| 56 | + broadcast "test" |
| 57 | +``` |
10 | 58 |
|
11 |
| -## Basic rules of Indentation |
| 59 | +This follows suit for things like loops, if statements, spawn sections, anything that can have code "inside" it: |
12 | 60 |
|
13 |
| -### For Commands and Events |
| 61 | +```applescript |
| 62 | +on join: |
| 63 | + # indent to put code in the event |
| 64 | + set {_uuid} to player's uuid |
| 65 | + if {banned::%{_uuid}%} is set: |
| 66 | + # indent a second time to put code in the if statement |
| 67 | + kick player |
| 68 | + # go back to one level of indentation to show that this code isn't in the if statement |
| 69 | + loop all players: |
| 70 | + # indent again to enter the loop |
| 71 | + if {quiet-mode::%loop-player's uuid%} is set: |
| 72 | + # indent again to enter the if |
| 73 | + continue |
| 74 | + # go back one to exit the if statement |
| 75 | + send "%player% has joined." to loop-player |
| 76 | + # go back one to exit the loop |
| 77 | + set {last-seen::%player's uuid%} to now |
| 78 | +``` |
14 | 79 |
|
15 |
| -### In Ifs, Loops, and Sections |
| 80 | +If you're ever uncertain, **a good rule of thumb is to indent one more time whenever you end a line with a colon** (`:`). |
0 commit comments