完整版BedrockWiki镜像!

This commit is contained in:
boybook
2025-03-20 11:52:46 +08:00
parent 1994c41f01
commit bf9aa4b056
214 changed files with 9042 additions and 8867 deletions

View File

@@ -1,6 +1,6 @@
---
title: Functions
category: General
title: 函数 mcfunction
category: 基础
mentions:
- Bedrock Commands
- cda94581
@@ -8,19 +8,24 @@ mentions:
- jordanparki7
nav_order: 3
tags:
- info
- 信息
---
## Introduction
[Sourced By Bedrock Commands Community Discord](https://discord.gg/SYstTYx5G5)
# 函数 mcfunction
Functions are `.mcfunction` files which contain multiple lines of commands. They are run with the `/function` command in-game.
<!--@include: @/wiki/bedrock-wiki-mirror.md-->
Functions are created in a **Behavior Pack**, nested within the **functions** folder. A function pack creates a system using solely function files.
## 简介
Functions are useful in many ways to reduce the time spent going from command block to command block debugging a system. They also help with packaging systems for use in multiple worlds and provide many functions that can change how everything works.
[源自 Bedrock Commands 社区 Discord](https://discord.gg/SYstTYx5G5)
## Function Pack Folder Structure
函数是包含多行命令的 `.mcfunction` 文件,可通过游戏中的 `/function` 命令运行。
函数需要创建在**行为包**的 **functions** 文件夹内。纯函数包系统完全由函数文件构成。
函数在多个方面非常有用,可以减少逐个命令方块调试系统所花费的时间。同时也便于将系统打包用于多个世界,并提供了许多可以改变整体运行方式的函数。
## 函数包目录结构
<FolderView
:paths="[
@@ -34,47 +39,46 @@ Functions are useful in many ways to reduce the time spent going from command bl
]"
></FolderView>
## Notes For Beginners
## 新手须知
<CodeHeader>mcfunction</CodeHeader>
```yaml
#Spawn Effects
::: code-group
```yaml [mcfunction]
#生成效果
effect @a [tag=atSpawn] regeneration 12 255 true
effect @a [tag=atSpawn] saturation 12 255 true
effect @a [tag=atSpawn] weakness 12 255 true
```
- Each new line in a function file represents a new command. You may start a line with # to add comments. Commands in a function do not need to begin with a slash `/`, however doing so will not cause any errors.
:::
- All commands in a function are run in the *same tick*. Because of this, a function which causes large changes may cause a sudden lag spike and it is helpful to delegate some commands across multiple ticks, if possible.
Commands in a function are still run in the same order, however.
- 函数文件中每一新行代表一个新命令。可以使用 `#` 开头添加注释。函数内的命令不需要以斜杠 `/` 开头,但添加了也不会报错。
- Minecraft can **not** run more than 10,000 lines of commands in one function file. This includes any other function files that are executed inside of the original file.
- 函数中的所有命令会在**同一游戏刻**内执行。因此,包含大量变更操作的函数可能导致突然的卡顿,建议尽可能将命令分摊到多个游戏刻中执行。但函数内的命令仍会按顺序依次运行。
- It is not possible to run conditional commands. Those will still need to utilize command blocks in some way, or could utilize the 1.19.50 execute syntax.
- Minecraft **无法**在一个函数文件中运行超过10,000行命令这包括原始文件中调用的其他函数文件。
- Running commands with a specified delay in a function would involve using scoreboard timers to incrementally count up every tick (to a certain point), and executing at certain scores along the file. You may refer to [Scoreboard Timers](/commands/scoreboard-timers) system to learn how to set it up.
- 无法执行条件型命令。这类命令仍需通过命令方块实现或使用1.19.50版本的execute语法。
## Creating a Function
- 要在函数中实现延迟执行命令,需使用计分板计时器逐刻计数(达到指定值),并在特定分数时执行命令。可参考[计分板计时器](/wiki/commands/scoreboard-timers)系统了解设置方法。
1. Locate the `📁 com.mojang` folder and navigate to `📁 development_behavior_packs`
- The development folders are used for quick reloading of packs, as the packs aren't cached to the world files.
## 创建函数
2. Create a folder (of any name) for the function pack. This will be referred to as Behavior Pack or BP.
1. 找到 `📁 com.mojang` 文件夹并进入 `📁 development_behavior_packs`
- 开发文件夹用于快速重载资源包,因为这些包不会被缓存到世界文件中。
3. Create a `📄 manifest.json` file and a `🖼 pack_icon.png` file (optional) within the BP folder.
- A manifest file contains all the information needed to register a pack, while a pack icon displays visually in the pack menu. A pack icon is typically a 128x128 or a 256x256 image, though any power-of-2 resolution will do, they will be upscaled and downscaled accordingly.
2. 为函数包创建任意名称的文件夹称为行为包或BP
<Spoiler title="Sample 📄 manifest.json">
3. 在BP文件夹内创建 `📄 manifest.json` 文件和 `🖼 pack_icon.png` 文件(可选)
- 清单文件包含注册资源包所需的所有信息包图标则用于在资源包菜单中显示。包图标建议使用128x128或256x256分辨率支持2次幂分辨率会自动缩放
<CodeHeader>BP/manifest.json</CodeHeader>
<Spoiler title="示例 📄 manifest.json">
```json
::: code-group
```json [BP/manifest.json]
{
"format_version": 2,
"header": {
"description": "Write Your Pack Description Here",
"name": "Write Your Pack Name Here",
"description": "在此填写资源包描述",
"name": "在此填写资源包名称",
"uuid": "00000000-0000-0000-0000-000000000000",
"version": [ 1, 0, 0 ],
"min_engine_version": [ 1, 19, 73 ]
@@ -89,45 +93,44 @@ Commands in a function are still run in the same order, however.
]
}
```
:::
Note that the uuid field needs to be replaced with an actual uuid, and the two generated must be different from one another. You can generate a uuid at https://uuidgenerator.net/
注意uuid字段需要替换为实际值且两个uuid必须不同。可通过 https://uuidgenerator.net/ 生成
</Spoiler>
<Spoiler title="Sample 🖼 pack_icon.png">
<Spoiler title="示例 🖼 pack_icon.png">
Sample A:
示例A
![pack_icon.png](/assets/images/commands/pack_icon.png)
Sample B:
示例B
![pack_icon.png](/assets/images/guide/project-setup/pack_icon.png)
</Spoiler>
4. Create a `📁 functions` folder. Any file within this folder that ends with **.mcfunction** will be registered as a function in-game, which can be run with `/function <function_name>`.
- Nested functions are allowed, simply list the file path in relation to the functions folder as shown in the function pack folder structure.
4. 创建 `📁 functions` 文件夹。该文件夹内所有以 **.mcfunction** 结尾的文件都将注册为游戏内可用的函数,可通过 `/function <函数名称>` 运行。
- 支持嵌套函数,只需按函数包目录结构示例中的方式组织文件路径即可。
5. Apply the behavior pack in-game and try out the functions. Function file changes can be reflected in the world by running `/reload` or by simply relogging.
5. 在游戏中应用行为包并测试函数。修改函数文件后可通过执行 `/reload` 或重新登录来刷新改动。
:::tip NOTE
Functions are versioned; therefore, they will run in the version listed in the `📄 manifest.json`, such as:
- `min_engine_version` 1.19.50 or above will adopt the new execute syntax.
- `min_engine_version` 1.19.70 or above will require aux values be replaced with block states.
:::tip 注意
函数具有版本依赖性,将运行在 `📄 manifest.json` 中声明的版本环境下:
- `min_engine_version` 1.19.50+ 将采用新版execute语法
- `min_engine_version` 1.19.70+ 需将aux值替换为方块状态
:::
## Execution
## 执行方式
Functions can be executed in-game by typing `/function name_of_function`. This will execute all the commands in the function file, all in a single tick.
Nested functions, for example `BP/functions/lobby/items/1.mcfunction` can be run using the nested folder path, in this case `/function lobby/items/1`
在游戏中输入 `/function 函数名称` 即可执行函数文件内的所有命令(同一游戏刻内完成)。例如嵌套函数 `BP/functions/lobby/items/1.mcfunction` 需使用路径 `/function lobby/items/1` 调用。
## tick.json
The final file within a function is the **tick.json** file. This specifies functions to run server-side on every game tick, (similar to a repeating command block.) It is located in the `BP/functions` folder. By default, functions running in this file execute at origin `0, 0, 0` in the overworld.
**tick.json** 是函数包中的特殊文件,用于指定服务器每游戏刻自动运行的函数(类似循环命令方块)。该文件位于 `BP/functions` 文件夹中,默认在主世界原点坐标 `0, 0, 0` 处执行。
<CodeHeader>BP/functions/tick.json</CodeHeader>
```json
::: code-group
```json [BP/functions/tick.json]
{
"values": [
"function_1",
@@ -135,24 +138,26 @@ The final file within a function is the **tick.json** file. This specifies funct
]
}
```
> Note: functions in this file are run as soon as the world is *initialized*, regardless of whether or not the player has been *loaded*. This may cause unintended behavior if used incorrectly.
:::
## Sample Function Pack
> 注意:该文件中的函数会在世界初始化后立即运行(无论玩家是否加载完毕),使用不当可能导致意外行为。
## 示例函数包
<CardLink
imgsrcLight="assets/images/commands/BClogo.png"
title="Download Sample Function Pack"
title="下载示例函数包"
link="https://github.com/Bedrock-OSS/wiki-addon/releases/download/download/functions_sample.mcpack"
/>
## Troubleshooting Functions
## 函数排错指南
Your functions may not appear within the command suggestions when using `/function`. This is normally due to an error with one or more commands in the function.
使用 `/function` 时若未出现命令建议,通常是由于函数中存在错误命令导致。
Enabling the [Content Log](/guide/troubleshooting#content-log) in creator settings will allow you to see if there are any errors in your function pack, in which function the error is in, at which line and exactly what the syntax error for that command is.
在创作者设置中启用[内容日志](/wiki/guide/troubleshooting#content-log)可查看函数包中的具体错误信息,包括错误所在函数、行号及语法问题。
The list of errors will be generated every time you load a world or run `/reload` to reflect changes after editing files. The list can be viewed on-screen for a few seconds as well as in the content log history in settings.
每次加载世界或执行 `/reload` 后都会生成错误列表,可在屏幕上短暂查看或通过设置中的内容日志历史记录查阅。
![contentLogToggles](/assets/images/commands/contentLogToggles.png)
![contentLogHistory](/assets/images/commands/contentLogHistory.png)
![contentLogHistory](/assets/images/commands/contentLogHistory.png)