Author: Daniel Bergqvist
Keywords: jmri LogixNG reference local variables

# LogixNG Reference - Chapter 8

## Local Variables

JMRI has memories that can store data. And that's great. But there is a problem with memories. They are global, which means that anyone can change them. Lets assume you want to count something so you create a memory IMCOUNTER. You create a LogixNG that does the counting and everything works fine.

A year passes and you need to count something else. You create a new LogixNG that does the new counting and you need a memory, so you decide to use the memory IMCOUNTER. And your new LogixNG works fine. But then you suddenly realise that something else on the layout stops working. Why? What has happen? Well, the problem is that you use the same memory IMCOUNTER for two different things in two different places. And when that happens, you get into trouble.

LogixNG has a simple solution to this problem, local variables. A local variable is similar to a memory, but it only exists in a very limited context and nothing outside that context can interfer with the variable. The local variable is also transient. It is only available during the execution of the ConditionalNG and it goes away when the execution is finished. This protects the local variable from being changed in an unpredictable way.

### Defining local variables

Local variables are created in the ConditionalNG editor. Open the editor of a ConditionalNG and right-click on an action or expression and select Local variables.

The local variable dialog window will open.

To create a local variable, click on the Add variable button. A new row will be added. Double click in the Name field and give it a name. Select the Type and provide an initial value if desired. The Select menu is used to Delete a local variable and has Move Up and Move Down options if there is more than one variable.

Click on OK to finish. The local variable will be added to the tree. See the image below.

### Order of local variables

The order of the local variables that are defined in an action or expression matters if you use one variable in the initialization of another. Lets say you define two variables "a" and "b", and that you define "a" to be initialized to the formula "34 * 4" and you define "b" to be initialized to "a * 3". This will work if "a" is defined before "b". You can define both variables in the same action or expression, but "a" needs to be before "b" since "b" uses "a" in its initialization.

### Local variables are local

This may seem obvious, but when writing scripts that uses local variables, it's important to remember this. A script can access the symbol table if it has access to the ConditionalNG, but the symbol table is dependent on which action or expression that's currently running.

The scope of a local variable depends on where it is defined in the tree. This a contrived example to show scope behavior.

There are two local variables defined. One at the root of the tree and a second one part way up the first branch.

The output on the JMRI system console shows the result when the ConditionalNG is executed.

    in if trees
    WARN  - Log local variables: [JMRI LogixNGThread]
    WARN  - Name: rootlevel, value: null [JMRI LogixNGThread]
    WARN  - Name: iftree, value: null [JMRI LogixNGThread]
    WARN  - Log local variables done [JMRI LogixNGThread]

    root level
    WARN  - Log local variables: [JMRI LogixNGThread]
    WARN  - Name: rootlevel, value: null [JMRI LogixNGThread]
    WARN  - Log local variables done [JMRI LogixNGThread]

### Initialization of local variables

When an action or expression is executed that defines local variables, these local variables are created with an initial value. When you create the local variable, you define what the inital value should be. 'None' means that the value will be 'null'. Reference means that the value will be the value that the reference is pointing at. Formula means the result of the formula.

### Debugging local variables

What to do if the ConditionalNG doesn't do the expected? The debugger provides one tool, but another tool is the action Log local variables. Each time it's executed, it prints all the local variables and their values to the JMRI Sytem Console.

Chapter 9 - Formula

Return to the Reference TOC