PDA

View Full Version : Global variable visibility


Tesctassa
03-28-2008, 01:50 PM
Hi all! I'm having some troubles with globals variables visibility between scripts.
I have two scripts which define two dialogs with serveral rollouts each. Both dialogs are defined as global variables, so that I can call their functions from every script in Max.
There are also some other global variables that hold certain values. When I try to use those variables into my script they're cosidered as implicitly declared, and are automatically set to undefined, but if I call them from the listener, the correct value is returned.
Note that also the rollout variable is defined as global, but when accessed, functions and properties work properly.

I hope this was clear enought. In case it wasn't ask for more details.

Thanks in advance!
Regards,
Adriano

Bobo
03-28-2008, 02:33 PM
Hi Adriano,

Keep in mind that the ORDER of declaration plays a significant role here.
For example, if a script attempts to access a variable that WILL BE global later because another script will be run later to declare it, it will NOT see it as global if the access happens from inside of a local scope, like a rollout, function, ( ) block, MacroScript etc. Instead, it will be implicitly local and "hide" later global variables with the same name.

For example, evaluating and running

macroScript test category:"Test"
(
print testGlobalValue
)

and then

(
global testGlobalValue = 10
)

will print undefined from the first script because the first code block will assume testGlobalValue is implicitly local to its scope.

This is especially true in cases where implicit local variables have a long life expectancy like the top-level of MacroScripts, in rollout definitions etc.

If the two blocks were evaluated in reverse order, it would have worked as expected.

Running the MacroScript a second time would then see the global declaration, that's why so many people complain their scripts do not work the first time they evaluate them, but do later.

Since you cannot control the exact order of script evaluation (for example at Max boot time), the solution is to PRE-DECLARE any future global variables that your scripts will share IN BOTH scripts to reserve a memory address for those variables even before their values are defined.

Thus, you can say


macroScript test category:"Test"
(
--Pre-declare as global.
--Will be undefined until defined by the other script, but it will reserve memory
--for the other script to write to:
global testGlobalValue
print testGlobalValue
)


(
global testGlobalValue = 10 --this accesses the SAME memory as the first script
)

Tesctassa
03-28-2008, 03:10 PM
Hi Bobo! Thanks for your reply!

I supposed something similar to what you said (actually reading the guide it's also written there :P) but I was excluding that scenario because the variable I'm trying to access is initialized for sure before it is used. The only way what you described could happen should be if Max "compiles" the script at startup... thus not finding the global variable in the first script it considers the occurrance in the second script as an implicit declaration. Is that right??
I always thought scritps in Max where interpreted. I'm sure you'll aswer "and you were wrong". Afterall what's happening is confirming that :P

Anyway, thanks for your help!

Regards,
Adriano


EDIT: there was also a reason why I was excluding what you said. There are some other variables (in the same two scripts) that work fine, including those referencing the two dialogs... What's the difference with what you described earlier?? -__-

CGTalk Moderation
03-28-2008, 03:10 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.