Declare Temporary Variable


描述

此动作是用来给 临时变量 (也叫 本地变量)赋予初始值,临时变量就是 仅仅存在于一个事件或脚本的变量。临时变量用于那些只在一个事件或脚本中需要使用的值。否则脚本将因为大量只使用一次的值而变得混乱。They are also very useful for breaking down complex operations, or for setting values in other instances when you change the action scope. This is because variables declared as temporary are not scoped to any particular instance, but instead scoped to the event or script itself, so if you create a temporary variable and then change the action scope using the Apply To... action, you can still check the temporary variable without issues and without changing its action scope.

值得注意的是当我们谈论“值”时,我们不仅是说数学上的值,而是包括字符串,指针,资源ID,或其他任何函数可以使用或返回的值,甚至可以是一个 表达式。更多信息请参考数据类型

许多动作都拥有“临时”选择框,这允许你创建一个新的临时变量来存储动作的返回值,此变量可以在此动作脚本或事件中被使用。但在其它动作脚本或事件中,你就需要其它的变量了。注意临时变量的作用域就是它们所在的脚本或事件,所以可以使用 Apply To来将它们用在脚本或事件的其它实例中。

你可以点击动作旁边 “增加” 图标,为其添加额外的变量,并给出需要添加的临时变量名称和值。有关详细信息,请看变量和全局变量


动作语法:

Declare Temporary Variable Syntax


参数:

参数 描述
名称 将要被赋值的变量
将要被赋给的值


例如:

Declare Temporary Variable Action Example

上述代码块将创建两个临时变量, "_dir" 和"_spd" 并将它们的值设为一个随机值。然后将使用这些值来为一个实例设置随机移动的方向和随机的速度。