# Scope

A **Scope** is an area of a program where an object is recognized.\
Procedures have their own *scope*. To the rest of the script, any [**variables**](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) [declared](https://docs.shrinefox.com/flowscript/variables-and-procedures#declaring-variables) inside a [**procedure**](https://docs.shrinefox.com/flowscript/flowscript/procedures) do not exist.

For instance, if you declare a variable in [**`Main()`**](https://docs.shrinefox.com/flowscript/procedures#main), you will not be able to use it from any other [procedure](https://docs.shrinefox.com/flowscript/flowscript/procedures). Also, unless you assign the value to a variable of greater *scope*, when the [procedure](https://docs.shrinefox.com/flowscript/flowscript/procedures) ends, the value will be discarded.

```csharp
// variableName only exists within Main() since it's local
void Main() {
    var variableName = 25;
    // no other procedure will be able to recognize 'variableName'
    AnotherProcedure();
}

void AnotherProcedure(int variableTest ) {
    // This would cause an ERROR.
    var test = variableName;
}
```

However, [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) can be given different *scopes*.

## Variable Scopes

| Name         | Description                                                                                                              |
| ------------ | ------------------------------------------------------------------------------------------------------------------------ |
| `local`      | Default *scope* for [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures).              |
| **`global`** | Persists across all scripts while the game is running.                                                                   |
| **`static`** | Value persists per [procedure](https://docs.shrinefox.com/flowscript/flowscript/procedures) while the script is running. |
| **`const`**  | Must be initialized with a value. Cannot be changed afterward.                                                           |

### Local

In the example above, the value of `variableName` cannot be accessed by `OtherProcedure()` since it only exists to [`Main()`](https://docs.shrinefox.com/flowscript/procedures#main), where it was defined.

However, you can **still pass it by reference** to use it for math or [initializing a variable](https://docs.shrinefox.com/flowscript/variables-and-procedures#initializing-variables) in another[ procedure](https://docs.shrinefox.com/flowscript/flowscript/procedures).

```csharp
// variableName only exists within Main() since it's local
void Main() {
    var variableName = 25;
    // but since AnotherProcedure() takes an int... 
    AnotherProcedure( variableName );
}

// ... we can make a copy of its value with a different name
void AnotherProcedure(int variableTest ) {
    // This would create a new local variable equal to 25
    var test = variableTest;
}
```

### Global

`global` [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) are persist even after a script is done executing. \
They stay in memory while the game is running, but **aren't saved to your save file**.&#x20;

A practical use case would be keeping track of the last entered number after closing and re-opening a menu.

```csharp
// globalTest exists for as long as the game is running, even after Main()
global int globalTest = 50;

void Main() {
    globalTest = globalTest + 30;
    AnotherProcedure();
}

void AnotherProcedure() {
    // This would create a new local variable equal to 80
    var test = globalTest;
}
```

### Static

`static` [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) can be declared but not initialized outside of a [procedure](https://docs.shrinefox.com/flowscript/flowscript/procedures). \
These keep their value between executions while the game is running, but **aren't saved to your save file**.&#x20;

```csharp
static int staticNumber;

void Main() {
    // the first static int will be 10 higher every time the script is run
    staticNumber = staticNumber + 10;
}
```

### Const

`const` [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) are constant, so **their value cannot be changed** after they're initialized.\
This can be useful for referring to values you know in advance by a convenient name.

```csharp
const int constantNumber = 99;

void Main() {
    constantNumber = 0;
    // test would still equal 99
    var test = constantNumber;
}
```

### Out Variables

Aside from [passing by reference](#local) or using `global`, `static` or `const`, you can also use **out variables** to pass values between [procedures](https://docs.shrinefox.com/flowscript/flowscript/procedures), even `void`. It would look like this:

```csharp
void Main() {
    OutTest( out x, out y );
    var x + y; // result equaling 35
}

void OutTest( out int x, out int y )
{
    x = 5;
    y = 30;
}
```

As you can see, the [variables](https://docs.shrinefox.com/flowscript/flowscript/variables-and-procedures) were already [declared](https://docs.shrinefox.com/flowscript/variables-and-procedures#declaring-variables) in the parameters of `OutTest()` using the **`out` keyword**. \
After being initialized with a value, the `x` and `y` variables are then passed to `Main()`, once again using the **`out` keyword**.

{% hint style="info" %}
If this seems confusing, don't worry about it for now. You will not have to do this very often.
{% endhint %}

## Conclusion

Now you should have a grasp on how data is stored and handled by scripts. You're ready to learn more advanced usages of these techniques, such as [**Arrays**](https://docs.shrinefox.com/flowscript/flowscript/arrays) and [**Loops**](https://docs.shrinefox.com/flowscript/flowscript/loops-and-conditionals).

{% content-ref url="arrays" %}
[arrays](https://docs.shrinefox.com/flowscript/flowscript/arrays)
{% endcontent-ref %}
