PDA

View Full Version : mel gui question


twinky
10-25-2007, 06:07 PM
Hi, anyone know why I'm getting:
Error: "$var" is an undeclared variable.
when I press the button?

global proc test () {
string $var[] = {"asas", "ekvn"};
window;
columnLayout;
button -c "test2($var)";
showWindow;
}

global proc test2 (string $var[]) {
print $var;
}

??

Robert Bateman
10-26-2007, 09:16 AM
proc string arrayToString(string $var[])
{
string $s = "{";
for($i=0;$i<size($var)-1;++$i)
{
$s += $var[$i];
$s += ",";
}
$s += $var[size($var)-1];
$s += "}";
return $s;
}
global proc test () {
string $var[] = {"asas", "ekvn"};
window;
columnLayout;
// the callback test2($var) will be executed out of scope of test.
// $var will no longer exist, so convert values to a text string...
button -c ("test2("+arrayToString($var) +")");
showWindow;
}

global proc test2 (string $var[]) {
print $var;
}

twinky
10-26-2007, 02:13 PM
Thanks!! Thats awesome!!
I'm not a newbie programmer but I don't really understand why $var is out of scope. If $var was a string it would work I think. Why if it's an array does it become out of scope or is it something to do with the button?
It seems a bit longwinded to have to convert it to a string first but I guess thats just MEL.

twinky
10-26-2007, 06:35 PM
Thanks! Thats Awesome!

I'm not a programming newbie but I still don't get why $var is out of scope.
It would work with a string I think.
seems quite long winded to have to convert it to a string first.
Is it something to do with the button?

Robert Bateman
10-29-2007, 11:04 AM
Thanks! Thats Awesome!
I'm not a programming newbie but I still don't get why $var is out of scope.



proc someFunc()
{
// $var is now local to someFunc. It only exists within someFunc, and not outside...
$var = "whatever";

window;
columnLayout;
button -c ("print $var");
button -c ("print " + $var);
showWindow;
}

// at this point, the window will now be created...
// ... and $var will no longer exist!
someFunc();

// so... lets do a test.
//
// this next line will fail because $var is not known...
print $var;

// when you click the first button, the command that is called is :
eval( "print $var" );

// which still will not work because $var is now unknown.

// when you click the second button, the following is executed...
eval( "print whatever" );

// and since "whatever" is just a string, it works....



It would work with a string I think.

It will not work with any variable name, i.e $whatever. You could of course use a global variable, i.e.


proc someFunc()
{
// $var is now a global variable, so $var can be accessed outside of the function.
global string $var = "whatever";

window;
columnLayout;
button -c ("print $var");
showWindow;
}

However global variables are nasty nasty nasty nasty things to use! Don't use them (because once you've defined one, you can never get rid of it again!). Always better to use locals if possible...

twinky
11-12-2007, 12:14 PM
Thanks for this!

CGTalk Moderation
11-12-2007, 12:14 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.