# 变量

变量可以是任意基本数据类型，也可以是任意自定类型。变量的类型由其标识符后面的特殊字符决定。

## 变量类型 <a href="#variable-types" id="variable-types"></a>

这些特殊字符被称为**类型标签**，其中包括：

* `%` = 整数值变量
* `#` = 浮点值变量
* `$` = 字符串变量
* `.{类型名}` = 自定类型变量

以下是有效变量的示例：

* `Score%`
* `speed#`
* `name$`
* `player.Player`

类型标签只需要在定义（第一次调用时）添加即可。在此之后，您可以选择性忽略掉类型标签。

如果在定义时没有指明类型标签，则变量将默认认为是整数值变量。

将同一变量名用于定义不同类型的变量是非法的。例如，如果您已定义了一个叫做 `name%` 的整数值变量，那么再定义一个叫做 `name$` 的字符串变量就是非法的。

## 变量赋值 <a href="#setting-variables" id="setting-variables"></a>

Blitz 使用 `=` 关键字为变量赋值。例如：`score% = 0` 将为整数值变量 `score` 赋值 `0`。

## 变量作用域 <a href="#variable-scope" id="variable-scope"></a>

变量可以是全局变量，也可以是本地变量。这定义了程序中可以使用该变量的位置。

* **全局变量**可以在程序的任意位置使用。
* **本地变量**只能在创建变量的函数内使用。

`Global` 关键字被用于定义一个或多个全局变量。例如：

```basic
Global Score = 0, Lives = 3, Player_up = 1
```

将定义三个全局变量。

相似地，`Local` 被用于定义本地变量：

```basic
Local temp_x = x, temp_y = y
```

如果变量在定义时未指明是全局变量还是本地变量，则变量默认为本地变量。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.ziyuesinicization.site/blitz-basic-language-reference/zh_hans/variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
