数据类型


在上一部分中,我们讲到了 变量以及它们的作用域规则,但是对于变量所能存储的各种 数据类型(Data types) 所谈甚少。因此,本章将会解释各种不同的类型以及它们都可以被用于做什么。

在继续之前,我们先简单地解释一下 “数据类型” 是什么意思。当你创建一个变量的时候,变量会被用于承载某种信息,还有当你调用一个 GML 函数的时候,它也会返回一些信息。然而这些信息可能来得五花八门各具风味,比如它们可以是一个实数(Real)或者也可以是一段字符串(String)。这些值所使用的的不同类型就被称为 数据类型(Data Types) 并且当使用 GML 时候,它们可以是下述几种类型:

A string is simply any text that has been placed in quotation marks "...". 你可以对字符串执行某些操作,例如将它们叠加到一起形成一段更长的字符串(连接操作(Concatenation)),你也可以改变字符串的属性甚至从中抽取实数部分。For more information on strings and the string functions see: GameMaker Language Reference - Strings.


实数可以是任何一个既不是字符串也不是其他数据类型的值,它就是一个数值而已。因此,124, 45639.566546456, 0, -45.5 等……所有的这些都是实数。All real numbers are stored as double-precision floating point values (even seemingly integer values), so you may experience slight rounding errors when dealing with these. 关于这个及其他与实数相关的函数参见 GML参考 - 实数

Note that while created variables in GameMaker Studio 2 are all stored as double-precision floating point numbers, you can still use other formats when dealing with extensions. 这些其它类型的数值可以从一个扩展中传递到 GameMaker Studio 2 中,然后可以使用下面所列举的相应的 is_ 函数来检测它们的类型。


数组是一个特殊的数据类型,它们可以用来存储多个值。你可以将数组赋予给一个变量,然后用值 “填入” 数组的各个索引元素中。通过传递引用(Pass-by-reference)可以将这个数组传递给脚本或函数中,但是当你要修改所传递的数组的时候,它会变成原来数组的一个拷贝(Copy),因此你需要回到原来需要修改的数组中。关于数组的更多信息,参见 GML概述 - 数组


GameMaker Studio 2 will also accept hexadecimal literals as legitimate values. Hexadecimal values are especially common when working with colours, but can be used anywhere a positive integer value is required. Note that hexadecimal values can be formatted in either of the following two ways:

$abcd
0xabcd

For example, the following values can be expressed as hexadecimal as shown:

11406 = $2c8e = 0x2c8e
16777215 = $ffffff = 0xffffff


布尔值可以是 truefalse。Note that currently GameMaker Studio 2 will interpret a real number equal to or below 0.5 as a false value, and any real number greater than 0.5 as being true. This does not mean however that you should be checking 1 and 0 (or any other real number) for true and false, as you are also provided with the constants true and false which should always be used in your code to prevent any issues should real boolean data types be added in a future update.

You can convert any real number into an implicitly boolean value using the following function:


指针是一个 “指向” 一个内存位置(Memory Location)的一种数据类型你不可以对指针进行操作,它们只是被用于某些极特殊的函数中的,像给其他函数取得纹理或缓冲区地址的函数中。能够返回指针的函数典型例子有 buffer_get_addresssprite_get_texture

还有一个用于检测一个值是否为指针的函数(参见下文的 “数据类型检测”),以及一个能够将值转换为指针的函数

在使用指针的时候,你可能会使用(或通过返回得到)如下的内置常量:

常量 描述
pointer_null 该常量表示指针没有指向一个地址(空地址)(和 C++ 中的 NULL 或 C# 中的 null 一样)
pointer_invalid 该常量表示该值不是一个有效的指针。

一个枚举也就是一个枚举器(Enumerator),它们允许你使用常量值列表创建自己的有限的数据类型。Enums are global scope variables (this is implicit, and they require no "global" prefix) and they have the following structure:

enum <variable>{<constant> [= <value>]}

在下述范例中,我们为彩虹的颜色创建一个枚举,并为其赋予不同的变量以及默认值:

enum rainbowcolors {
   red,
   orange,
   yellow,
   green,
   blue,
   indigo,
   violet
   }

The enum entries can only be integer numbers or expressions with previous enums that evaluate to an integer number, and by default are numbered from 0 upwards, so our example given above would default to red = 0, orange = 1, yellow = 2, etc...

你也可以在制作过程中将值赋给枚举变量:

enum rainbowcolors {
   red = 5,
   orange = 5 * 2,
   yellow = 15,
   green = 20,
   blue = 25,
   indigo = 30,
   violet = 35 * enum_test.entry
   }

请注意,上面的例子中我们使用了另一个枚举为 "violet" 创建了一个表达式。这仅适用在,所引用的枚举在一个表达式中使用它 之前 创建的时候,但它不适用于变量或函数,因为枚举值必须能够在编译时作为常量进行求值。Also note that all enum values evaluate to integer values, and when you create your own you should be aware that only integer values are permitted for enums to work. This value can be any integer number that a floating point double precision number can represent.

要稍后访问给定枚举类型中的值,你可以使用 “.” 方法,像这样:

var value = rainbowcolours.green;

请注意,在任何枚举常量创建后,你无法修改它们的值。


未定义的值(也称为 “null” 值)是指一个表达式没有正确值的情况,尽管它在语法上是正确的,因此必须返回 某些事物。例如,假设你有一个 ds_map 并使用函数 ds_map_find_value()。现在,当 map(译者注:映射数据)没有找到值时会发生什么?好吧,函数格式正确,问题是没有这样的值存在,那么它将返回 常数 undefined,你可以检查这个常数为 true 或任何其他值。


检测数据类型

GameMaker Studio 2 允许你使用以下函数检查给定变量的数据类型: