# Indentation

{% hint style="info" %}
**"What is indentation?"**

Indentation refers to how far "in" a line of code has been pushed; its distance from the left side of the window. The farther to the right a line starts, the more indented it is.

Some coding languages organise code with curly braces (`{}`), and have flexible indentation:

{% code overflow="wrap" lineNumbers="true" %}

```python
code{
  nested code
}
```

{% endcode %}

It doesn't actually matter how indented line 2 is, as long as it stays within the curly braces. These sections can even be collapsed:

{% code overflow="wrap" lineNumbers="true" %}

```python
code{nested code}
```

{% endcode %}

In languages without curly braces, sections are created solely with indentation:

{% code overflow="wrap" lineNumbers="true" %}

```python
code:
  nested code
```

{% endcode %}

The section(s) the `nested code` is in depends entirely upon its indentation, as there are no curly braces to define the opening and closing of the sections.

In summary, many languages use curly braces to open sections. In these languages, indentation is mostly a formatting or readability perk. Other languages—*including Skript*—instead use whitespace, so rules for indentation are far less lenient.

{% hint style="warning" %}
This means indentation is not only to make code look nice, but is actually a required fundamental element defining the hierarchy of code.
{% endhint %}
{% endhint %}

## In practice

Any time a new section is opened, a line will end with a colon (`:`). All code you want inside that section must be indented by one more level. To exit a section un-indent, removing a unit of indentation.

Indentation requires a *consistent* amount of either spaces or tabs, (usually 2 or 4 spaces, or 1 tab) before a line of code. It does not matter which is chosen, but once a decision is made it must be adhered to.

{% hint style="warning" %}
Any time indentation is added or removed, it ***must*** be by the same amount. If indenting with 2 spaces, always add or remove 2 spaces each time a line's indentation is adjusted. When indenting with 1 tab, always add or remove 1 tab.

Anything else (resulting in an inconsistency) will cause Skript to be unable to understand the intent, resulting in an error. If you already have an indentation error, the [Errors section](#errors) will cover it.
{% endhint %}

### Example

{% code overflow="wrap" lineNumbers="true" %}

```python
on right-click:
  if (type of player's tool) = diamond:
    send "Ooh, shiny!" to player

  else:
    send "Not shiny." to player
```

{% endcode %}

{% hint style="info" %}
Notice how line 2 has 2 spaces before it, placing it within the `on right-click:` event. Line 3 also has 2 spaces, placing it within the event, but then has an additional 2 spaces to be nested within the if statement as well.
{% endhint %}

This example starts with the `on right-click:` event, and all code to run on that event is indented inside the event. Then, inside the event, the script checks if the player is holding a diamond. The code to run if they are is then indented again, inside that if statement.

{% hint style="info" %}
Once the if statement runs all the lines inside of it (4 spaces), Skript will back out and look for more lines indented within the event (2 spaces), and run those.
{% endhint %}

To check if the player is not holding a diamond, it backs out one level to be aligned with the if statement. This is because the `else:` section is checking for a different scenario after the event. Then, again, all the code to run in that situation is indented inside the `else:`.

Lines *must* be indented into their respective sections, otherwise they will be run at incorrect times. Consider the following example:

{% code overflow="wrap" lineNumbers="true" %}

```python
on right-click:
  if (type of player's tool) = diamond:
  send "Ooh, shiny!" to player
  
  else:
  send "Not shiny." to player
```

{% endcode %}

Above, the indentation for line 3 and line 6 were neglected. They belong nested within the if statement and else respectively. Since the indentation is off, this code **will not run as intended**.

Since lines 3 and 6 are not within the correct sections, they are only indented within the event. But, that's not what this script is supposed to do; it is supposed to send a message depending on the item the player is holding. But in the above example, that is not how Skript interprets the code.

This mistake is basically identical to the following:

{% code overflow="wrap" lineNumbers="true" %}

```python
on right-click:
  if (type of player's tool) = diamond:
    [no code to run]
  send "Ooh, shiny!" to player
  
  else:
    [no code to run]
  send "Not shiny." to player
```

{% endcode %}

So Skript thinks the if statement and else are empty, and that the messages are only a part of the event, to be sent any time a player right-clicks.

{% hint style="info" %}
Neglected indentation as shown above usually results in an empty configuration section warning.
{% endhint %}

***

## Errors

{% hint style="warning" %}
Recall that indentation must be **consistent**, and any inconsistencies or changes will result in errors.
{% endhint %}

If indentation isn't consistent in type or isn't changing by a steady magnitude, an indentation error will occur.

{% hint style="info" %}
Here, type refers to whether spaces or tabs are being used, and magnitude refers to the quantity of indents used to change a line.
{% endhint %}

### Type

Indentation must be uniformly spaces or tabs. You cannot use spaces to indent one line and then tabs for another. Pick one style and stick to it.

{% hint style="warning" %}
One tab can be visually identical to four spaces; copy-pasted code might look okay but actually use indentation that doesn't match the rest of your code.

Minehut's online dashboard editor can also cause indentation errors when used. Their editor will automatically indent new lines to the same level as the previous line, *using tabs*.
{% endhint %}

Thankfully, an indentation error will tell you exactly which lines have incorrect indentation, along with how many tabs and spaces were used. To fix the errors, simply correct or even redo the indentation for a line. If the errors span across a whole section of code, a find and replace can quickly replace all tabs with spaces.

{% code overflow="wrap" lineNumbers="true" %}

```python
on right-click:
  if (type of player's tool) = diamond:
    send "Ooh, shiny!" to player
  
  else:
    send "Not shiny." to player
```

{% endcode %}

Here, although the length looks correct, line 6 is actually indented by 1 tab instead of 2 sets of 2 spaces, like line 3 is.

### Magnitude

Indentation's magnitude must also follow a strict pattern. Skript requires changes in indentation to always increase or decrease by the same length.

{% code overflow="wrap" lineNumbers="true" %}

```python
on right-click:
  if (type of player's tool) = diamond:
    send "Ooh, shiny!" to player
  
   else:
     send "Not shiny." to player
```

{% endcode %}

After the event, the if statement is indented by two spaces, **+2** from the previous line. This starts the pattern, so now Skript will always expect a change by **2** spaces. Line 3 is indented by 2 spaces for the event, and then another 2 for the if statement, following the pattern.

However, line 5 has five spaces before it, changing by ***+3 spaces*** from the event. Since the pattern is always changing by 2 spaces, the extra space at the end is incorrect and will cause an error. Line 6 is indented by **+2 spaces** within the `else:`, which is correct, however it has a total of ***5 spaces*** indenting it instead of 4. This is because it also has an incorrect **3 spaces** after the event.

The pattern must be consistent, so the indentation should increase by two (0, 2, 4, ...) across the script, or always increase by three (0, 3, 6, ...) across the script.

{% hint style="info" %}
Yes, it is possible to indent by three spaces. However, it is almost never seen. Typical indentation lengths are two spaces, four spaces, and one tab.

Anything longer than four spaces or a tab is just a waste of window width, and also requires your eyes to wander more.

Anything less than two spaces (one space being the only option) is also discouraged, as it can be hard to notice a difference in indentation.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://x8ight.gitbook.io/syntask/fundamentals/indentation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
