给变量和属性指定值

 
 
 

一旦已声明变量,则可以使用 = 运算符给其指定值。运算符右侧表达式的值指定 = 运算符左侧的变量。例如:

$x = $y * 10;

如果指定不匹配变量类型的值,MEL 解释器则将尝试将值转化为变量类型。

例如:

// Declare the variables...
float $foo;
float $bar;
int $baz;
// Now assign values...
$test = 5.4;
// value of $test is 5.4
$bar = $test + 2;
// value of $bar is 7.4
$baz = $bar;
// the value of $baz is 7
// MEL converted the value to an integer.

指定作为指定值求值。

注意MEL 不支持将字符串属性指定给诸如以下语句中的字符串:

string $str = foo.str

而使用:

string $str = `getAttr foo.str`;

结合声明和指定

为了方便起见,在创建变量时可以给变量指定值。例如:

float $param = 1.5;
int $counter = 10;
string $name = "Alice";
vector $position = <<1.5, 2.5, 3.5>>;

如果变量的类型明显出自指定的值,可以省去类型关键字。例如:

$name = "Alice";
$position = <<1.5, 2.5, 3.5>>;

这称为隐式声明。但是,好的做法是始终使用类型关键字以使脚本更具备可读性。

链接指定

当给若干变量指定相同的值时,可以将指定链接在一起:

$x = $y = $z = 0;

方便指定运算符

编程时常常出现以下类型的表达式:

$x = $x + 1;

这很常见,因此 MEL 提供了几种方便运算符以便使用较少键入来完成此操作。例如:

$x++ // adds 1 to the value of $x
$x-- // subtracts 1 from the value of $x
$x += 5 // adds 5 to the value of $x
$x -= 3 // subtracts 3 from the value of $x