SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.

Home / forum / Animation / Mel Scripts & Programming / Script doesn't remember variables
Register FAQ Members List Calendar Mark Forums Read

New Thread
 
Thread Tools
Old 17-11-2004, 03:10 AM   #1
Renderizer
Subscriber
 
Renderizer's Avatar
 
Join Date: Jun 2002
Location: Germany
Posts: 276
Thanks: 0
Thanked 0 Times in 0 Posts
Default Script doesn't remember variables

Hi all,


this is really my first attempt to dabble in MEL, so be kind, please...

Now, please take a look at the following if-statement.

The reason I post this, is that the script seems to forget the values for $MR_rotX, $MR_rotY and $MR_rotZ as soon as it leaves the if-block. Inside the block everything is fine (I printed the values to the script editor to confirm this), outside the block all values are set to zero. If I delete the if-statement and the brackets, the values remain intact..

Any idea why this happens?

Thanks!

Mario


if ($MR_SnapButton == 1)
{
int $MR_randX = rand (360/$MR_snapx);
int $MR_rotX = $MR_randX*$MR_snapx;

int $MR_randY = rand (360/$MR_snapy);
int $MR_rotY = $MR_randY*$MR_snapy;

int $MR_randZ = rand (360/$MR_snapz);
int $MR_rotZ = $MR_randZ*$MR_snapz;
}
__________________
Subdivide and conquer!

Free your mind, and your ass will follow!
Renderizer is offline   Reply With Quote
Old 17-11-2004, 07:39 AM   #2
kbrown
Moderator
 
kbrown's Avatar
 
Join Date: Sep 2002
Location: London, UK
Posts: 3,198
Thanks: 0
Thanked 8 Times in 8 Posts
Default

It's because you declare the variables inside the block. When you do that they become local variables to that block. When you try to use them outside of the block you are actually declaring new variables and that's why they show zero in the contents. To make the variables available outside the block you have to declare them in "upper level":

Code:
{
	int $MR_randX;
	int $MR_rotX;
	int $MR_randY;
	int $MR_rotY;
	int $MR_randZ;
	int $MR_rotZ;

	if ($MR_SnapButton == 1) 
	{ 
		$MR_randX = rand (360/$MR_snapx); 
		$MR_rotX = $MR_randX*$MR_snapx; 
	
		$MR_randY = rand (360/$MR_snapy); 
		$MR_rotY = $MR_randY*$MR_snapy; 
	
		$MR_randZ = rand (360/$MR_snapz); 
		$MR_rotZ = $MR_randZ*$MR_snapz; 
	}
}
__________________
Kari
- My Website
- My IMDB

Do a lot, Fail a lot and Learn a lot!
kbrown is offline   Reply With Quote
Old 17-11-2004, 01:20 PM   #3
Renderizer
Subscriber
 
Renderizer's Avatar
 
Join Date: Jun 2002
Location: Germany
Posts: 276
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yep, already found out. It just had to be some minuscle detail that's so upfront, right under your nose, that you would just overlook it.

Thanks anyway!
__________________
Subdivide and conquer!

Free your mind, and your ass will follow!
Renderizer is offline   Reply With Quote
Old 17-11-2004, 03:13 PM   #4
Alan
Moderator
 
Alan's Avatar
 
Join Date: Oct 2002
Location: London, UK
Posts: 2,800
Thanks: 0
Thanked 5 Times in 5 Posts
Default

it's called "variable scoping" and a scope is defined by {code}

they are hierarchical and so:

int x = 1;
{
int x = 2;
print x (will print 2)
{
int x = 3;
print x (will print 3)
}
}
print x (will print 1)


Alan
__________________
Technical Director - Framestore

Currently working on: Your Highness

IMDB
Alan is offline   Reply With Quote
New Thread

« Previous Thread | Next Thread »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

1 Month

$34

Month-to-month billing
$34/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
 
 

3 months

$87

Billing every 3 months
$29/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $5 per month or $15 in total compared to monthly billing with this package.

12 months

$288

Billing every 12 months
$24/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $10 per month or $120 in total compared to monthly billing with this package.

Your plan will automatically renew. You can cancel at any time via your PayPal account to avoid being charged again.

You can view the FAQ on our video plans here. You can contact us directly here. By clicking one of the buttons above, you agree to our Terms and Privacy Policy.