跳转到内容
Pixi’VN

临时存储

了解如何使用 Pixi’VN 存储,在 ink 的 knot 或 stitch 内定义、设置和获取局部临时变量。

有关 ink 中临时变量的更多信息,请参阅官方文档。

有时使用全局变量并不方便。 ink 提供用于快速局部计算的临时变量。与全局变量(VAR)不同,临时变量会在故事离开定义该变量的 knot 或 stitch 时立即被丢弃。

在 ink 与 Pixi’VN 的集成中,temp 变量相当于存储中的临时变量。

Define

在 JavaScript/TypeScript 中,此功能对应于 存储中的临时变量。

在 ~ 逻辑行中使用 temp 关键字声明临时变量:

~ temp myTemp = 42

可以使用 ink 支持的任何类型:整数、浮点数、布尔值或字符串。

file_type_ink
ink/start.ink
=== near_north_pole ===
    ~ temp number_of_warm_things = 0
    { blanket:
        ~ number_of_warm_things++
    }
    { ear_muffs:
        ~ number_of_warm_things++
    }
    { gloves:
        ~ number_of_warm_things++
    }
    { number_of_warm_things > 2:
        Despite the snow, I felt incorrigibly snug.
    - else:
        That night I was colder than I have ever been.
    }

变量 number_of_warm_things 在进入 knot 时创建,离开时被丢弃。

设置

在 JavaScript/TypeScript 中,此功能对应于 storage.set。

初次声明后,可以使用相同的 ~ 语法重新为临时变量赋值(无需重复 temp 关键字):

~ score = score + 10

例如:

file_type_ink
ink
~ temp score = 0
~ score = score + 10
~ score++

获取

要获取临时变量的值,请使用常规的 storage.get 函数。

在 ink 中,读取临时变量的方式与其他变量完全相同:在表达式、行内输出或条件中使用变量名即可:

The duration is: {duration}

例如:

file_type_ink
ink
~ temp duration = 3

The duration is: {duration}
{ duration > 2:
    It's a long scene.
- else:
    It's a short scene.
}

本页内容