---
url: /statelake/scripting-reference/variables/local-globals.md
---

# Local Globals

**Local Globals** are **Global Variables** that can be defined by the user, but are limited in scope to a single execution of an **Action**. They are always of type **Variant**.

They are **Local Globals** because they are global only within a single execution of an **Action**, and as such can be referenced from any object as long as it is executing within the same **Action**.

**Local Globals** are classed as being **non-persistent**, which means that they are **persistent** only across the entire execution of an **Action**, but are reset for each new execution of the same **Action**.

Particular care should be taken when using **Local Globals** - so that you don't use the same **Local Global** for different purposes within the same **Action**. Trying to use them in this way would cause a conflict as to the value you expect to find within the **Local Global** at any one time.

**Local Globals** are referenced like fields through the special locals array.

Their value can be accessed through the properties .Value, .AsString, .AsInteger, and .AsFloat.

For additional information on how these properties work with fields, please refer to the section on [Scripting](/statelake/scripting-reference/).

## Setting A Local Global

The following example sets a **Local Global** called **MyLocalGlobal** to the string value **abc**.

If the **Local Global** does not already exist, then it will be created, but if it does exist then it will be updated with the new value as supplied.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  Locals['MyLocalGlobal'].AsString := 'abc';
end;
```

## Reading A Local Global

This following example reads the value of the **Local Global** called **MyLocalGlobal**.

If the **Local Global** does not already exist it will return a null value, depending on the accessor used.

In the example .AsString is used therefore it will return a blank string.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  Value := Locals['MyLocalGlobal'].AsString;
end;
```

## Reading A Local Global Using Tags

**Local Globals** can be accessed outside of scripts wherever **Tags** are supported.

To access a **Local Global** as a **Tag**, you should enter the name of the **Local Global** surrounded by the opening Tag <# and closing Tag #>.

For example, to access a **Local Global** called **MyLocalGlobal** you would enter the Tag <#MyLocalGlobal#>.

For additional information, please refer to [Tags](/statelake/tag-reference/).

## Specifying A Display Format For .AsString And Tags

When accessing float and date/time type **Local Globals** with .AsString or through **Tags**, you can control the format of the resulting string value.

The following example shows how to set the display format.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  Locals['MyLocalGlobal'].DisplayFormat := 'yyyy-mm-dd';
  Locals['MyLocalGlobal'].DisplayFormat := '#0.00';
end;
```

For more information, please refer to [Format Specifiers](/statelake/scripting-reference/functions-and-procedures/formatting-functions/format-specifiers/).

## Removing A Local Global

This example below deletes the **Local Global** called **MyLocalGlobal**.

If the **Local Global** specified does not exist, then nothing will happen, and you will not receive an error message.

If the **Local Global** does exist, then it will be removed.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  Locals.RemoveLocal('MyLocalGlobal');
end;
```

## Clear All Local Globals

This example clears all **Local Global** variables.

```pascal
procedure ScriptEvent (var Value : variant);
begin
  Value := Locals.ClearAllLocals;
end;
```
