Shared Variables

When running multiple instances of a module within one application, each will keep a separate copy of its own variables. Prefixing a variable definition with the keyword "Shared" makes that variable a shared variable. When multiple copies of one module are running and using this shared variable, all copies use the same value. For example, if one module writes the value 4 to a shared variable, X, all other copies of that module will see the value 4 in X. X is global to the module instances and only one memory location will be used for all instances of X. Shared variables are deleted only when the application stops running.

Shared variables are not shared across differing modules or across applications.

A common use of Shared is often found within tags when declaring local instances of modules that are plugins, configuration folders, and widgets. By declaring these as shared, you avoid the overhead of creating a fresh instance of each module for each tag instance.

Practice with shared variables

  1. Create a new script application. (Workspace for Script Applications)
  2. Add the following declaration to the variables section of AppRoot.SRC (line 10):
  LaunchedWindow Module "LaunchedWindow.SRC" { a launched  child module  };
  1. Edit the Graphics module within AppRoot.SRC as follows:
Graphics
[
  Shared X              { a shared variable                      };
  Y                     { an unshared variable                   };
]

Main [ If ZButton(50, 75, 150, 50, "Launch!", 1);
  [
    LaunchedWindow();
  ]
  ZText(100, 100, Concat("Edit X. Currently: ", PickValid(X, "Invalid")), 5, 0);
  ZText(100, 200, Concat("Edit Y. Currently: ", PickValid(Y, "Invalid")), 5, 0);
  WinEditCtrl(100, 140, 180, 120, 0, X, 1);
  WinEditCtrl(100, 240, 180, 220, 0, Y, 1);
]
{ End of System\Graphics }
>
  1. Using a text editor, create a new file named LaunchedWindow.SRC. Contents follow. Note that this is very nearly an exact copy and paste from AppRoot.SRC. Variations shown in bold font.
{============================== System =================================}
{=======================================================================}
(
  System                { Provides access to system library functions };
  Layer                 { Provides access to the application layer    };
)
[
  Graphics         Module { Contains user graphics                    };
  WinTitle =      "Launched Application" { Window title             };
  RunningOnVIC            { TRUE if this is a VIC session              };
]

Init [
  If 1 Main;
  [
    RunningOnVIC = IsVICSession();
  ]
]

Main [
  Window(  0,   0           { Upper left corner   },
         800, 600           { View area           },
         800, 600           { Virtual area        },
         Graphics()         { Start user graphics },
          {65432109876543210}
         0b00010000000110011,
         Concat(WinTitle, RunningOnVIC ? " - %S" : ""),
         7, 1);
]

<
{============================= System\Graphics ========================}
{ This module handles all of the graphics for the application          }
{======================================================================}
Graphics
[
  Shared X                  { a shared variable                      };
  Y                         { an unshared variable                   };
]

Main [
  ZText(100, 100,Concat("Edit X. Currently: ", PickValid(X,"Invalid")),5, 0);
  ZText(100,200,Concat("Edit Y. Currently: ", PickValid(Y,"Invalid")),5, 0);
  WinEditCtrl(100, 140, 180, 120, 0, X, 1);
  WinEditCtrl(100, 240, 180, 220, 0, Y, 1);
  
]
{ End of System\Graphics }
>
  1. Compile and run the application.
  2. Click the launch button twice to open two instances of LaunchedWindow. Move the windows so that you can see each.
  3. Enter values for X and Y in each of the windows, noting the results.
  4. Close both the launched windows, then reopen. The value of X should remain.
  5. Stop, then restart the application, launching the windows. X should be Invalid to start.

Shared versus persistent variables.

  1. Edit both AppRoot.SRC and LaunchedWindow.SRC, changing the declaration of X from shared to persistent.
  2. Compile and run, noting any difference in behavior. (There shouldn't be any difference at this point.)
  3. After running, stop then restart the application, reopening the launched windows. Values of X should persist for each of the two modules, AppRoot and LaunchedWindow.