PDA

View Full Version : help about procedures


seb4d
06-07-2004, 01:46 PM
Hi !
I am a beginner in scripting, I have no background at all in programming either.

I'm learning Mel script with Wilkins ' book but there is something I don't understand.

I am writing a simple script that creates a custom window, asks for a number of spheres to create, and creates those spheres when you hit a button.

My problem is that the button's -command flag doesn't execute the procedure I wrote that creates the spheres.

Could someone explain to me why this happens ? And what error did I make ?

Thank you in advance !

Here is the script :


//create a window
window -title "Create spheres" -rtf true ;
columnLayout;

//radio buttons to chose how many
//spheres you want to create
radioButtonGrp -numberOfRadioButtons 4
-label "How many spheres ?"
-labelArray4 "1" "2" "3" "4"
-select 1
buttons;

//query the radioButtonGrp and assign result to a variable
string $choice = `radioButtonGrp -query -select buttons`;

//procedure that creates the spheres
proc creation()
{
string $choice;

switch ($choice)
{
case "1":
sphere;
break;

case "2":
sphere ; sphere ; move 0 3 0 ;
break;

case "3":
sphere ; sphere ; move 0 3 0 ; sphere ; move 0 6 0;
break;

case "4":
sphere ; sphere ; move 0 3 0 ; sphere ; move 0 6 0 ; sphere ; move 0 9 0;
break;
}
}

//creation button
button -label "create !"
-command "creation()";

//display the window
showWindow

GDC
06-07-2004, 02:52 PM
OK, a couple of things. You can't decalre a proc in the middle of
you window creation. You want to have all your procs at the top of
your script and then your UI. Also in your proc you never assign $choices a value, so your switch statement will never evaluate. Try this version. Wrapping your head around this can be frustrating at first, so keep with it!


//

//procedure that creates the spheres
proc creation()
{
//query the radioButtonGrp and assign result to a variable
string $choice = `radioButtonGrp -query -select buttons`;
switch ($choice)
{
case "1":
sphere;
break;

case "2":
sphere ; sphere ; move 0 3 0 ;
break;

case "3":
sphere ; sphere ; move 0 3 0 ; sphere ; move 0 6 0;
break;

case "4":
sphere ; sphere ; move 0 3 0 ; sphere ; move 0 6 0 ; sphere ; move 0 9 0;
break;
}
}
//create a window
window -title "Create spheres" -rtf true ;
columnLayout;

//radio buttons to chose how many
//spheres you want to create
radioButtonGrp -numberOfRadioButtons 4
-label "How many spheres ?"
-labelArray4 "1" "2" "3" "4"
-select 1
buttons;
//creation button
button -label "create !"
-command "creation()";

//display the window
showWindow;


--g

seb4d
06-07-2004, 03:47 PM
Ok this works now !

So I always have to declare procedures at the beginning of the script : Ok !

I didn't assign a value to $choice in the procedure because I thought it would fetch the value in the lign where I query the radio buttons value...

One more question :
In your version of the script you declared the $choice variable at the beginning of the script in the procedure, while the radioButtonGrp is only used at the end of the script. So I guess the order in which you write your script has no importance in this particular case ?

Thanks again ! :beer:

CGTalk Moderation
01-18-2006, 09: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.