Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   Script doesn't remember variables (https://simplymaya.com/forum/showthread.php?t=14382)

Renderizer 17-11-2004 04:10 AM

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;
}

kbrown 17-11-2004 08:39 AM

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;
        }
}


Renderizer 17-11-2004 02:20 PM

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!

Alan 17-11-2004 04:13 PM

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)

:ninja:
Alan


All times are GMT. The time now is 01:39 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018