Indentation and Program Flow
In Progress
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.
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:
Basic rules of indentation
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.
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.
When to indent?
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:
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.
This follows suit for things like loops, if statements, spawn sections, anything that can have code "inside" it:
If you're ever uncertain, a good rule of thumb is to indent one more time whenever you end a line with a colon (:
).
Last updated