
1. Global Variables
Global variables are variables declared outside of functions and are accessible throughout the entire package. While they have specific use cases, it's generally discouraged to overuse them. Global variables can lead to issues when multiple processes access them simultaneously, potentially causing bugs in your application.
One common problem arises when a field calculation uses an ACF function that modifies global variables, and another function references this field directly or through a SQL function. In this scenario, the field calculation triggers while still within your ACF function, leading to changes in the global variables.
Here are some important considerations regarding global variables:
- Global variables are local to a package, which means different packages cannot share them.
- Global variables cannot be of array types because there is no initializer code to build arrays.
- Globals cannot be initialized in their declaration, as there is no initializer code. When the package loads, they are initialized to zero values.
- If you have several FMP12 files running in the same FileMaker process, function calls from different files will share global variables. The same applies to packages; once loaded from one file, the functions in it will be available to other databases running in the same FileMaker process.
1.1. Alternatives to Using Globals ↑
One alternative to global variables is to use FileMaker's $$
variables. These variables can be set and read by script steps directly and can also be viewed in the Data Viewer. However, accessing $$
variables from ACF functions may be slower than using built-in globals, as it involves the FileMaker calculation engine to set and get values. Therefore, using $$
variables in loops with a high loop count is discouraged. While $$
variables are shared between packages, they are local to the FMP file. FileMaker variables persist even after the ACF package is reloaded, while ACF globals do not.
ACF globals can be used to transfer global FileMaker variables from one FileMaker file to another running in the same process. This can be achieved by having setter and getter functions in ACF called from each file. Alternatively, you can traditionally share globals between files using global fields in tables.
1.2. Example ↑
package test_globals "Here we test the globals";
// Some global declarations.
string g_test1, g_test2;
// Functions using them.
function aa (string a)
// And some local variables inside the function:
string cc2 = "TestString local";
g_test1 = a;
g_test2 = a*2 + cc2;
return g_test2;
end
function bb ()
// g_test1 and g_test2 are available here, but cc2 is not.
print g_test1+"\n"+g_test2;
return "OK";
end
// Transfer globals between files
// ----------------------------------------------
string UserID, UserName, Initials;
// Setter:
function SetUserInfo ()
UserID = $$sys_user_id;
UserName = $$sys_user_name;
Initials = $$sys_initials;
return "OK";
end
// Getter - called from the other file.
function GetUserInfo ()
$$sys_user_id = UserID;
$$sys_user_name = UserName;
$$sys_initials = Initials;
return "OK";
end
This example demonstrates the use of global variables within an ACF package, including their declaration, usage within functions, and a mechanism for transferring global FileMaker variables between files running in the same process.