# Temporary storage (/ink/temp-storage)



<CalloutContainer type="info">
  <CalloutDescription>
    You can read more about temporary variables in ink on the [official documentation](https://github.com/inkle/ink/blob/master/Documentation/WritingWithInk.md#4-temporary-variables).
  </CalloutDescription>
</CalloutContainer>

Sometimes a global variable is unwieldy. **_ink_** provides **temporary variables** for quick, local calculations. Unlike global variables (`VAR`), a temporary variable is **discarded as soon as the story leaves the knot or stitch in which it was defined**.

In the ***ink* + Pixi’VN integration**, `temp` variables are equivalent to <DynamicLink href="/start/storage#temporary-storage">temporary variables in storage</DynamicLink>.

## Define [#define]

<CalloutContainer type="info">
  <CalloutDescription>
    This functionality in Javascript/TypeScript corresponds to <DynamicLink href="/start/storage#temporary-storage">temporary variables in storage</DynamicLink>.
  </CalloutDescription>
</CalloutContainer>

Declare a temporary variable with the `temp` keyword inside a `~` logic line:

<CodeBlockTabs defaultValue="ink">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="ink">
      ink
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="CLI">
      CLI
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="ink">
    ```ink
    ~ temp myTemp = 42
    ```
  </CodeBlockTab>

  <CodeBlockTab value="CLI">
    ```txt
    ~ temp <variable_name> = <value>

    where:
        variable_name = the name of the temporary variable
        value = the initial value of the variable
    ```
  </CodeBlockTab>
</CodeBlockTabs>

You can use any type that **_ink_** supports: integer, floating point, boolean, or string.

```ink title="ink/start.ink"
=== near_north_pole ===
    ~ temp number_of_warm_things = 0
    { blanket:
        ~ number_of_warm_things++
    }
    { ear_muffs:
        ~ number_of_warm_things++
    }
    { gloves:
        ~ number_of_warm_things++
    }
    { number_of_warm_things > 2:
        Despite the snow, I felt incorrigibly snug.
    - else:
        That night I was colder than I have ever been.
    }
```

The variable `number_of_warm_things` is created when the knot is entered and thrown away when it is left.

## Set [#set]

<CalloutContainer type="info">
  <CalloutDescription>
    This functionality in Javascript/TypeScript corresponds to <DynamicLink href="/start/storage#set">`storage.set`</DynamicLink>.
  </CalloutDescription>
</CalloutContainer>

After the initial declaration you can reassign a temporary variable with the same `~` syntax (without repeating the `temp` keyword):

<CodeBlockTabs defaultValue="ink">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="ink">
      ink
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="CLI">
      CLI
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="ink">
    ```ink
    ~ score = score + 10
    ```
  </CodeBlockTab>

  <CodeBlockTab value="CLI">
    ```txt
    ~ <variable_name> = <expression>

    where:
        variable_name = the name of an already declared temporary variable
        expression = the new value, which can reference the variable itself
    ```
  </CodeBlockTab>
</CodeBlockTabs>

For example:

```ink title="ink"
~ temp score = 0
~ score = score + 10
~ score++
```

## Get [#get]

To get a temporary variable, use the normal <DynamicLink href="/ink/storage#get">`storage.get` function</DynamicLink>.

Inside **_ink_** itself, you read a temporary variable exactly the same way as any other variable – by using its name in an expression, inline print, or condition:

<CodeBlockTabs defaultValue="ink">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="ink">
      ink
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="CLI">
      CLI
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="ink">
    ```ink
    The duration is: {duration}
    ```
  </CodeBlockTab>

  <CodeBlockTab value="CLI">
    ```txt
    {<variable_name>}

    where:
        variable_name = the name of the temporary variable to print or evaluate
    ```
  </CodeBlockTab>
</CodeBlockTabs>

For example:

```ink title="ink"
~ temp duration = 3

The duration is: {duration}
{ duration > 2:
    It's a long scene.
- else:
    It's a short scene.
}
```
