执行器
提示
执行器需要配合 触发器 使用。
以下将使用 Python 风格标注类型和签名
全局变量
globals.TriggerID: int | None
运行当前执行器的触发器编号。
globals.TriggeredItemID: int | None
使上述触发器生效的物体编号。
注册变量至检测器
使用方法
在执行器开头写入 //InspectorVariables<xxx1><xxx2>... 即可将 xxx1 xxx2 等变量注册至检测器面板。
提示
如果变量名以 ID_开头,则会则能保存元件 ID 的相对引用。
此时将一组元件保存为模板后,再从素材库中拖出使用,元件的引用能被自动计算并填入。
原理
例如执行器开头写入了以下代码:
//InspectorVariables<mush1><mush2>
可在检测器面板中的 mush1 和 mush2 输入框中分别写入字符串,假设为 42 和 43 。
则实际运行代码时,相当于在开头额外注入了以下代码:
mush1 = 42; mush2 = 43
因此可以在代码其余部分使用 mush1 和 mush2 变量。
提示
由于代码注入是直接将输入框内容插入至代码中,因此要使用字符串类型的变量时, 请在检测器面板的输入框中将内容用 双引号 括起来。
例如:
//InspectorVariables<hello>
| hello | "Hello, World!" |
|---|
导入模块
使用方法
- 在执行器开头写入
//define<xxx>定义该执行器为xxx模块。 - 可在其他执行器的任意位置写入
//import<xxx1><xxx2>...即可导入xxx1xxx2等模块。
注意
定义模块时注意模块命名不要重复。
原理
本质是代码插入。
例如有两个执行器分别写了以下代码:
//define<mush1>
mush1 = 42
if mush1 > 42 then print "Hello"
//define<mush2>
mush2 = 43
if mush2 > 42 then print "World"
在另一个执行器中写入了以下代码:
foo = "Hello"
//import<mush1><mush2>
bar = "World"
print foo + mush1 + mush2 + bar
则该执行器在运行时,相当于运行以下代码:
foo = "Hello"
mush1 = 42
if mush1 > 42 then print "Hello"
mush2 = 43
if mush2 > 42 then print "World"
bar = "World"
print foo + mush1 + mush2 + bar
注意
由于导入模块的本质是代码插入,因此代码运行时报错提示的行数将会难以定位。 使用导入模块时请做好 debug 困难的准备。
API
类型定义
| 名称 | 定义 | 描述 |
|---|---|---|
| vec2 | tuple[float, float] | 数值列表 [x, y] |
| vec3 | tuple[float, float, float] | 数值列表 [x, y, z] |
| vec4 | tuple[float, float, float, float] | 数值列表 [x, y, z, w] |
| quat | tuple[float, float, float, float] | 四元数列表 [x, y, z, w] |
| xform | tuple[float, float, float, float, float, float, float, float, float, float] | [px, py, pz, qx, qy, qz, qw, sx, sy, sz];[0:3]:位置,[3:7]:旋转,[7:10]:缩放。 |
| color | vec3 | vec4 | [r, g, b] 或 [r, g, b, a];0 <= r, g, b, a <= 1 |