Data types

This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this Introduction To ChoiceScript Game Development series of linked articles: A Basic Tutorial. 本文面向ChoiceScript的初学者。不过,如果您尚未开始学习,那么最佳起点应是本《ChoiceScript游戏开发入门》系列关联文章中的第一篇:《基础教程》。

This article discusses ChoiceScript Data Types, their general purpose from a beginner's point of view, and the main differences between the three types of data. In an effort to cover the topic thoroughly for the beginner, it will doubtless repeat some information given on other pages elsewhere in the ChoiceScriptDev Wiki. 本文从初学者的角度探讨了ChoiceScript的数据类型、其一般用途以及三种数据类型之间的主要区别。为了全面覆盖初学者所需的知识点,本文难免会重复ChoiceScriptDev Wiki其他页面已提及的部分信息。

"Data Types" refers to the type of information that the two Variable Types used in ChoiceScript can contain; numeric data (containing a number, whether an integer or a floating-point value), string data (containing "text" of some description), or boolean data (a simple but very useful true or false binary switch). "数据类型"指的是ChoiceScript中使用的两种变量类型所能包含的信息类型:数值型数据(包含数字,无论是整数还是浮点数)、字符串型数据(包含某种描述的"文本")或布尔型数据(一种简单但非常有用的真/假二进制开关)。

Assigning a data type to variables 为变量分配数据类型

While the two Variable Types in ChoiceScript (Temporary and Permanent) are in themselves very different to each other, the way each uses its data type is identical, and either of the variables types can be assigned any of the three data types. Only the manner of originally creating the variable differs: for more information on Temporary variables see the temp command, and for Permanent variables the create command. 尽管ChoiceScript中的两种变量类型(临时变量和永久变量)本身存在很大差异,但每种变量类型使用其数据类型的方式是相同的,并且任一变量类型都可以被分配三种数据类型中的任何一种。只有最初创建变量的方式有所不同:有关临时变量的更多信息,请参阅temp命令;有关永久变量的信息,请参阅create命令。

Note, however, that each individual variable may only ever contain one of the three different data types. 但请注意,每个独立变量只能包含三种数据类型中的一种。

A variable's data type is determined simply by the type of data assigned to it when first created: if you give it a number (even just zero) it becomes a numeric data type; if you give it "something in quotes" it becomes a string data type; and if you give it either true or false (without any quotes) it becomes a boolean data type. 变量的数据类型仅由首次创建时赋予的数据类型决定:若赋予数值(即使仅为零)即成为数值型;若赋予"引号内的内容"即成为字符串型;若赋予不带引号的true或false则成为布尔型。

Examples of creating a Permanent variable of each type in startup.txt: 在startup.txt中创建各类型永久变量的示例:

*create var1 0

*create var2 "Jill"

*create var3 false

Examples of creating a Temporary variable of each type in any ordinary scene file: 在任何常规场景文件中创建各类型临时变量的示例:

*temp var4 0

*temp var5 "Jack"

*temp var6 false

Numeric Data Type 数值数据类型

数值数据类型包含一个数字。它可以是整数(完整的数字)、浮点值(带小数位),也可以是负数(小于零)或正数(大于零)。当然,也可以是零本身。这就是为什么在维基中,我们选择用这个包罗万象的通用标签“数值”来描述这种数据类型。

Numeric data limitations 数值数据限制

Although for our purposes there is no real limit to the size of the number this data type can contain (either negative or positive, or the number of decimal places), it should be noted that there is one important exception to this general rule. Where a particular numeric variable is used within a stat_chart on the Stats screen to display a percentile bar, that value should range between 0 and 100. If anything in your scripting changes the value to be outside this range, it will have an adverse effect on the appearance of the percentile bar on the Stats screen. The easiest way to always keep a percentile value within the acceptable range is to use ChoiceScript's "Fairmath" system, described in the section on Arithmetic Operators. 尽管就我们的用途而言,这种数据类型能容纳的数字大小(无论是负数、正数还是小数位数)实际上没有限制,但需要注意的是,这条通用规则有一个重要的例外。当特定的数值变量在统计屏幕的stat_chart中用于显示百分比条时,该值应介于0到100之间。如果脚本中的任何操作导致该值超出此范围,将会对统计屏幕上百分比条的外观产生不利影响。始终保持百分比值在可接受范围内的最简单方法是使用ChoiceScript的"公平数学"系统,该系统在算术运算符章节中有详细说明。

Using the numeric data type 使用数值数据类型

Numeric variables can be either overwritten or modified using the *set command, as follows: 数值变量可以通过 *set 命令进行覆盖或修改,具体操作如下:

This will overwrite the current value of the variable called var, regardless of the current value, with the value 10. Alternatively, you can instead modify the current value using arithmetic operators, for example simple addition: 无论当前数值为何,该操作都会将名为 var 的变量当前值覆盖为 10。或者,你也可以使用算术运算符来修改当前值,例如简单的加法运算:

This will add 15 to the current value, so increasing the value of var from (e.g.) a current value of 10 to 25. 该操作会在当前值基础上增加 15,从而将变量 var 的数值从(例如)当前值 10 提升至 25。

The current value of a numeric variable can also be displayed on the Stats screen, or anywhere within your story scripting, by placing the variable name in curly braces and preceded by the dollar $ symbol, as follows: 数值变量的当前值还可以通过以下方式显示在状态界面中,或呈现在故事脚本的任意位置:将变量名置于花括号内,并在其前方添加美元符号 $,具体格式如下:

If that line followed the previous two examples in your scripting, it would display the current value of var as: 如果脚本中的这一行遵循前两个示例,它将显示变量var的当前值为:

The value of a numeric variable can also be modified by the value of another numeric variable, also using the arithmetic operators. For example: 数值变量的值也可以通过另一个数值变量的值来修改,同样使用算术运算符。例如:

This would add the current value of var1 to the current value of var. If var had been 10 and var1 15, var will now contain the value of 25. 这会将变量var1的当前值加到变量var的当前值上。如果var原本是10而var1是15,那么var现在将包含25这个值。

Numeric variables are most commonly used within scripting for conditional purposes, using the if command: 数值变量在脚本中最常用于条件判断,通过if命令实现:

In this example, we are checking to see if the current value of var is greater than 50, which is just one of many types of conditional checks possible using arithmetic operators. Subsequent scripting would then determine what to do in either of two cases -- if the stated condition is true, and if the condition is not true (i.e. is false). 在这个例子中,我们正在检查变量var的当前值是否大于50,这只是使用算术运算符进行多种条件检查中的一种。随后的脚本将决定在两种情况下该做什么——如果所述条件为真,以及如果条件不为真(即假)。

String data type 字符串数据类型

The string ("text") data type consists of an array of alphanumeric characters (letters or numbers, but including common symbols such as exclamation or question marks). The string itself must always be enclosed within "quotation marks", by which means ChoiceScript knows that any words or numbers contained within are intended to be "just text", not actual numeric values. 字符串("文本")数据类型由字母数字字符(字母或数字,但也包括常见符号,如感叹号或问号)组成的数组构成。字符串本身必须始终用"引号"括起来,通过这种方式,ChoiceScript 知道其中包含的任何单词或数字都只是"纯文本",而非实际的数值。

Using the string data type 使用字符串数据类型

String values are also created or overwritten using the *set command, as follows: 字符串值也可以通过 *set 命令创建或覆盖,如下所示:

String values are also displayed on the Stats screen or within your story by enclosing the string variable name within curly braces and preceded by the dollar $ symbol, as follows: 字符串值也会在状态屏幕或故事中显示,方法是将字符串变量名用花括号括起来,并在前面加上美元符号 $,如下所示:

The above would then display as: 上述内容随后将显示为:

Strings are however modified (i.e. joined together to form longer strings) by a process called concatenation, still using the basic *set command, but is too advanced a topic to include in this "Introduction to Data Types" article. 字符串可以通过称为连接的过程进行修改(即组合成更长的字符串),这仍然使用基本的 *set 命令,但对于这篇“数据类型介绍”文章来说,这是一个过于高级的主题。

The string data type can also be used for conditional purposes, employing the *if command: 字符串数据类型也可用于条件判断,例如使用 *if 命令:

Note that all references to the contents of a string variable must always properly use "quotation marks". 请注意,所有对字符串变量内容的引用都必须正确使用“引号”。

Boolean data type 布尔数据类型

The boolean data type is a simple binary switch, meaning that it can only ever contain one of two values, either true or false. 布尔数据类型是一个简单的二进制开关,这意味着它只能包含两个值之一,要么为真,要么为假。

Note that even though the boolean data type essentially uses an actual word as its value, it is not treated the same as the ordinary text of a string variable. It should therefore never be enclosed within "quotation marks". 请注意,尽管布尔数据类型本质上使用一个实际的单词作为其值,但它与字符串变量的普通文本处理方式不同。因此,它绝不应被括在“引号”内。

Using the boolean data type 使用布尔数据类型

As with the numeric and string data types, the boolean data type is also *set with that command: 与数值和字符串数据类型一样,布尔数据类型也通过 *set 命令设置:

or

The boolean data type is not, however, generally displayed on the Stats screen or within the story. Its primary purposes is storing information for conditional purposes: 布尔数据类型通常不会在状态屏幕或故事中显示。其主要用途是为条件判断存储信息:

Which for a boolean is the same as: 对于布尔值而言,这等同于:

Either syntax is acceptable. Likewise, for a boolean; 两种语法均可接受。同样地,对于布尔值而言:

Is the same as: 等同于:

Again, for a boolean variable, either syntax is acceptable. 同样,对于布尔变量,两种语法均可接受。

circle-info

TIP: When first defining a new permanent boolean variable using the create command in startup.txt (or creating a new temp one in any scene file), consider carefully if a simple value of true or false is definitely all you will ever need to use this particular variable for. If there is even a small possibility that you may want it to contain different information at some point in the future, consider instead making it a numeric variable with a starting value of either 0 (false) or 1 (true). To all intents and purposes this can be used as a boolean, but also allows for future variation of use with a value of 2, or 3, or . . . 提示:在startup.txt中使用create命令首次定义新的永久布尔变量(或在任意场景文件中创建新的临时变量)时,请仔细考虑是否确定仅需为该特定变量使用简单的true或false值。若未来存在任何可能需要其包含不同信息的可能性,请考虑将其设为数值变量,并设置起始值为0(假)或1(真)。实际上,这可以完全作为布尔变量使用,同时允许未来通过2、3等值实现用途的扩展。