variables gloabl?! local ?!

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 03 March 2003   #1
variables gloabl?! local ?!

Ho, .. i have a problem .. i never understend when a global variable gets local or a local gets global .. if a global variable in an loop local or global? .. and or in an window / layout?!

can someone explain it ? ..
| xt_textureStudio | xt_prefabLibrary |xt_normalis | Get it @ Fuchtelworld V.3 |
Old 03 March 2003   #2
All variables are local unless you define them as being global.


global proc localGlobal (){
string $thisIsALocalVariable = "It's local.";
global string $thisIsAGlobalVariable = "It's global.";

print $thisIsAGlobalVariable;
print "\n";
It's global.
print $thisIsALocalVariable;
// Error: print $thisIsALocalVariable; //
// Error: Line 1.28: "$thisIsALocalVariable" is an undeclared variable. //
Old 03 March 2003   #3
k .. but when i make inside the procedure some loops/windows/ifs ect .. did i always have to say that he msut use the global or only on time at thebeginningof the procedure ..
| xt_textureStudio | xt_prefabLibrary |xt_normalis | Get it @ Fuchtelworld V.3 |
Old 03 March 2003   #4
In terms of local vs global, it matters not when you declare but how. Unless you explicitly state that a varial is global, it is local.

However, you should declare all variables upfront to avoid confusion on your own part as to what the exact type of each variable will be.

Old 03 March 2003   #5
Well, it does matter both when AND how you declare it (if by "when" you mean where in your script.)

For example, if you do this:

for ($x in $y) {
float $a = 3;

print $a;

you'll get an error because a local variable's scope is restricted to the enclosing curly brackets.

You can use this intentionally if you want to create and throw away a bunch of variables:

int $a = 1;
int $b = 2;
print $a + $b;

float $a = 1.0;
float $b = 3.0;
print $a + $b;

Without the curly brackets you'd get an error. With them it works because the first set of variable definitions are local to the enclosing set of curly brackets.

-- Mark
Mark R. Wilkins
author of MEL Scripting for Maya Animators
Old 03 March 2003   #6
Yes, when/where matters to locals but the question was about local vs global and when a variable becomes one or the other.

I originally wrote that it is both where/when and how as well. But I changed it because where a variable is declared has no bearing on whether it will be recognized outside its current scope. In other words, you can't cause a local variable to be recognized within all scopes by placing it at some magical spot within your script.

Old 03 March 2003   #7
My only point was that a variable being "local" is a relative thing. A local variable can encompass an entire file by being defined at the top of the file, in which case it's local to the file.

Alternatively, you can have a local variable that's local to a single for loop.

Even global variables are not immediately available in every MEL file -- they have to be defined in each file that they're used, though they will share a value globally.

BTW I know that you know this, bigfatMELon, I'm just trying to provide more detail for the original poster.

-- Mark
Mark R. Wilkins
author of MEL Scripting for Maya Animators
Old 01 January 2006   #8
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 09:06 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.