脚本引擎 API

变量操作 API

runtime.variables.* 读写脚本变量。key 为字符串,value 支持 string/number/boolean/null。

当前:变量操作 API
脚本引擎 API

变量操作 API

runtime.variables.* 读写脚本变量。key 为字符串,value 支持 string/number/boolean/null。

概述

读写当前脚本的变量上下文。变量可在不同动作节点间共享,支持 string/number/boolean/null 类型。

方法列表

  • runtime.variables.get(key: string): any — 读取变量。key 为变量名。未设置时返回 undefined。
  • runtime.variables.set(key: string, value: any): void — 设置变量。value 可为字符串、数字、布尔值或 null。
  • runtime.variables.remove(key: string): void — 删除变量。
  • runtime.variables.keys(): string[] — 返回所有变量名数组。

调用示例

以下代码可直接复制到 run_code 属性面板中运行:

// 设置计数器
runtime.variables.set("count", 0);
// 循环递增
for (var i = 0; i < 5; i++) {
    runtime.variables.set("count", runtime.variables.get("count") + 1);
    runtime.process.log("当前计数: " + runtime.variables.get("count"));
}
// 删除临时变量
runtime.variables.remove("count");
// 多变量示例
runtime.variables.set("username", "user123");
runtime.variables.set("score", 99);
runtime.variables.set("isPremium", true);
var keys = runtime.variables.keys();
runtime.process.log("变量数: " + keys.length);

注意事项

变量名区分大小写。变量值在脚本执行结束后仍保留,可在后续动作节点中通过变量引用 ${varName} 读取。变量值不支持对象或数组嵌套,复杂数据结构建议序列化为 JSON 字符串存储。使用 runtime.variables.set(key, null)runtime.variables.remove(key) 效果相同。