UserMain - how to get a global variable

How do I get a global variable in a UserMain function?

Function AskFolder: String;
defaultDirectory := GetGlobalVar( ‘defaultDirectory’, “”);
Result := SelectDirectory(“Choose a folder”, defaultDirectory);

Did not work. Also tried exo_getglobalvariable. I’m probably missing something simple.

Figured out one thing - no script error when using double quotes around global var.

defaultDirectory := GetGlobalVar(“defaultDirectory”, “C:\wamp64”);

But it does not get the global variable “defaultDirectory”. It’s always set to C:\wamp64.

Are UserMain functions able to pick up a global variable set within the .exe? It does not look like it. Or I cannot figure out how to get it.

As expected, if I set the variable before the SelectDirectory, it works:
defaultDirectory := “C:\wamp64”;
Result := SelectDirectory(“Choose a folder”, defaultDirectory);

But did you define the global variable “defaultDirectory” before trying to read it with GetGlobalVar?

Yes, but outside UserMain.

I figured out an alternative that works. I set the global var inside a UserMain function:

Result := SelectDirectory(“Choose a folder”, “”);
if (Result <> “”) then SetGlobalVar(“defaultSearchFolder”, Result, true);

Now I’m able to use it throughout the program.