PDA

View Full Version : Top level local variables unseen my global proc's?


Jakobud
09-04-2003, 01:50 AM
After looking through the Maya docs and a few posts on here, it seems like this should work:

string $str = "testing...";

proc runMe()
{
print $str;
}


but it gives me an error saying that $str is undefined. That doesnt seem to make sense. If $str is defined at the very top level of the script and procedure hierarchy, then every single command and procedure at that top level and below should see it... am I wrong? From reading the doc's that is what I understood.

So if I have some variables that I want to use in many different procedures in a script I have to redeclare them in every single procedure instead of just once at the top level? Why is this?

dmcgrath
09-04-2003, 07:49 AM
Does it work this way? It does doesn't it?

proc runMe()
{
string $str = "testing...";
print $str;
}

Then you can use the runMe proc correctly.

I think thats what you are saying. I'm trying to remember from my MEL knowledge why this would work, but not yours.
I think that the proc overrides what has been declared before. So you have to declare the variable in the proc.

Just a string statment like this would work;



string $str = "testing...";
print $str;

I know it's the same thing I put above it. But the proc command is the key here, you will have to redeclare a variable used inside of a proc, because that is where it is looking for it. They are like closed packets of code that run on their own.


-dan

misterdi
09-04-2003, 12:11 PM
string $str = "testing...";

proc runMe()
{
print $str;
}


Isn't it necessary to use a global var if you want to have that variable pass from one proc to other?.


global string $str = "testing";

proc test()
{
global string $str;
$str = $str + " pass";
print $str;
}


Best regards,

GDC
09-04-2003, 05:08 PM
Just to add to this...
Yes you would need to declare $str as a global if you want to use it in the proc. This has to do with scope. A proc ( be it a global proc or not) only knows about the varibles that are declared with in it. This is why even with global varibles you need to declare them inside your proc.
Think of { and } as blinders on your proc. In the same way they are blinders to any conditional statements or looping statements.

--g

ajk48n
09-04-2003, 05:12 PM
You don't need global variables if you pass a variable into a procedure.

string $str = "testing...";

proc runMe(string $var)
{
print $var;
}

Jakobud
09-04-2003, 11:32 PM
okay thanks. Yeah I know about global variables. I just wasn't sure if they absolutely had to be global if I defined them on the top level of the hierarchy within the script.

mark_wilkins
09-05-2003, 10:50 AM
What's weird is that you CAN have nested scopes, but you CAN'T have procedures within procedures. For example:


proc a () {

proc b () {
// This is not allowed!
}
}



however this is OK:


proc a () {
{
string $b = "test";
}
// you can't see $b out here
}


-- Mark

Jakobud
09-09-2003, 12:55 AM
The thing that brought this question up for me is that in the Maya 5 docs it states:

" - Variables you define outside of procedures are visible (able to be accessed and changed) to all other top level MEL code.

- Variables you define inside a procedure are only visible within that procedure. These cariables are called local variables. "

So when I read that I thought it would consider a procedure to be a top level piece of MEL code and therefore it would see the variables that were declared at that top level. Guess I was wrong.

mark_wilkins
09-09-2003, 01:33 AM
Variables you define outside of procedures are visible (able to be accessed and changed) to all other top level MEL code.

This is true only if "top level MEL code" means code that's not in ANY procedure, which is to say freestanding code in the MEL file that executes when it's sourced.

Interestingly enough, I went back to see what we'd written in the book about this and I note (to my horror) that on pg. 119 the section on variable scoping leads off with a statement that a variable declared outside any procedure in a MEL file can be used in any procedure defined in the MEL file, then proceeds over a series of examples on the following couple of pages to demonstrate exactly that that isn't true.

I guess I have something to add to the errata page tonight!! :D

-- Mark

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