PDA

View Full Version : why "undeclared variable"-Error


McMaster
06-12-2003, 02:28 PM
Hi!
Im trying to get into Mel right now, working with Marc`s book. I just tried to make my own little project, but canīt find the mistake. Maybe one of you could help me.
I tried to make a light flicker with a random Value with this script, but I alwas get the error:
// Error: buttonAction($flickerFloatSliderGrp); //
// Error: "$flickerFloatSliderGrp" is an undeclared variable. //
But $flickerFloatSliderGrp to me seems already declared?
The scripts need a scene with a Spotlight named spotLightShape1!


proc make_light_flicker(string $lightToFlicker, float $flickerValue)
{
select -cl;
select $lightToFlicker;
float $current_Intensity = `getAttr ($lightToFlicker + ".intensity")`;
string $Expression_String = $lightToFlicker + ".intensity = " + $current_Intensity + "-(" + $flickerValue + "/2)+ rand( 0, " + $flickerValue + ")";
expression -s $Expression_String; //Create the Expression
}

proc buttonAction (string $floatSliderGrpName)
{
float $flicker = `floatSliderGrp -q -value $floatSliderGrpName`;
make_light_flicker("spotLightShape1", $flicker);
}

global proc LightFlickerUI()
{
string $window_name = `window -title "Window1"
-widthHeight 483 203`;
columnLayout;
frameLayout -collapsable true -label "Flicker" -width 475;
string $flickerFloatSliderGrp = `floatSliderGrp -min 0
-max 0.7
-label "FlickerValue"
-field true
-fieldMinValue -10
-fieldMaxValue 10`;
setParent ..; //Return to ColumnLayout
frameLayout -label "" -width 475;
button -label "Execute" -command "buttonAction($flickerFloatSliderGrp)";
showWindow $window_name;
}

mark_wilkins
06-12-2003, 03:28 PM
When you set something up as a command to be called by a UI element, it won't be able to see the variables defined in your UI procedure.

Instead of
string $flickerFloatSliderGrp

try
global string $flickerFloatSliderGrp

-- Mark

McMaster
06-12-2003, 04:55 PM
Thanks for your fast answer Mark, but it still doesn`t work!
The problam is, that a global variable needs a constant value, which isn`t the case!
But anyway, the call is in the same procedure, where I defined the variable, so I think this procedure should be able to use it without global?!
It works when I do not put it into a procedure though!

But by the way, in your book it says, there should be files or Codes used in your book on the website wwww.mkp.com/MELscripting, but there you get transferred to elsevierhealth.com, where I can't find any files?

mark_wilkins
06-12-2003, 06:21 PM
The problam is, that a global variable needs a constant value, which isn`t the case!

No. Do it like this:

global string $blah;
$blah = `whatever`;

It'll work fine if you split it up like that, for some reason I'm not sure I can discern.

But anyway, the call is in the same procedure, where I defined the variable, so I think this procedure should be able to use it without global?!

No, because the command that you add to the button does not run within that procedure -- it runs separately, in response to the button's actually being clicked. The procedure that sets up the UI will have stopped running a long while before.

BTW, if you don't put the variable definition into a procedure, it will work, but only in the script editor. When you put this code in a separate MEL file, it will fail. Defining a variable in the script editor makes it global even if you don't use the global keyword unless it's within a procedure, but defining a variable in a MEL file is always local.

As for the supplemental material, the actual site is buried on the book's page at the publisher, which is where the URL you quoted will take you, though it's more prominent on the USA site than on the overseas site. The site with the materials is www.melscripting.com, and this is the address that will be printed in future printings of the book.

Be sure to have a look at the errata on the site as well.

-- Mark

McMaster
06-12-2003, 06:31 PM
Thanks, great, now it works! :bounce:

But I still can`t get where the difference is between initializing a variable and initializing a variable with a value at once!
But hey, doesn`t matter, it works, so why ask questions?!

I`ll check the site, thanks Mark!

CGTalk Moderation
01-15-2006, 10: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.