PDA

View Full Version : variables gloabl?! local ?!


X-Tender
03-22-2003, 06:54 PM
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 ? ..

bigfatMELon
03-22-2003, 09:49 PM
All variables are local unless you define them as being global.


example:

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. //

X-Tender
03-22-2003, 09:56 PM
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 ..

bigfatMELon
03-22-2003, 10:39 PM
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.

-jl

mark_wilkins
03-26-2003, 03:18 AM
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

bigfatMELon
03-26-2003, 04:45 AM
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.

-jl

mark_wilkins
03-26-2003, 05:08 PM
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. :D

-- Mark

CGTalk Moderation
01-14-2006, 05:00 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.