> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-zh/chang-jing-yun-xing-shi/execution.md).

# 执行

运行时将其视为 [场景](https://github.com/decentraland/docs/tree/main/contributor/contributor/content/entity-types/scenes.md) 像典型的 Node 模块一样。一旦沙箱设置完成（即所有 [globals](https://github.com/decentraland/docs/tree/main/contributor/globals.md) 都已注入，并且 [模块](https://github.com/decentraland/docs/tree/main/contributor/modules.md) 已准备好加载），场景代码就会被求值，以便填充 [导出对象](https://github.com/decentraland/docs/tree/main/contributor/globals.md#module) 对象。

主机会从以下内容中提取两个方法： `导出对象`: [`onStart`](#onStart) （可选）和 [`onUpdate`](#onUpdate) （必需）。

```ts
type Exports = {
  onStart?: () => Promise<void>;
  onUpdate: (deltaSeconds: number) => Promise<void>;
};
```

在场景的生命周期内，运行时会确保对这些方法的调用绝不会并发进行。返回的 `Promise` 在场景接收到新的调用之前，都会始终先等待其完成。

```mermaid
flowchart LR
    A[onStart] -->|await| B[onUpdate]
    B -->|await| B
```

{% hint style="info" %}
场景与游戏渲染引擎之间高效且性能良好的同步是一个复杂的问题。你可以在基金会的实现中找到相关经验，见 [ADR-148](https://adr.decentraland.org/adr/ADR-148).
{% endhint %}

**`onStart`**

场景的生命周期始于异步的 `onStart` 方法。这里适合进行一次性初始化，这些初始化必须在第一次调用 [`onUpdate`](#onUpdate) 之前完成。

```ts
onStart(): Promise<void>
```

场景应使用此调用从运行时请求任何预先存在的状态（例如 [基础实体](https://github.com/decentraland/docs/tree/main/contributor/entities.md)）并执行自己的初始设置。

导出 `onStart` 建议所有场景都进行此操作（在使用 SDK 时会自动完成），但协议并不强制要求。该方法可能缺失，或者返回一个立即解析的 `Promise`.

**`onUpdate`**

当场景正在运行时，异步的 `onUpdate` 方法会由运行时定期调用，以报告时间的流逝。这是场景的核心：在每次后续调用中，都可以更新实体、处理输入、播放动画、发送消息、显示 UI 等。

```ts
onUpdate(deltaSeconds: number): Promise<void>
```

该 `deltaSeconds` 参数是自上次调用 `onUpdate` 由运行时发起以来所经过的秒数的小数部分（无论返回的 `Promise` 何时结算）。这取决于每个正在运行的场景。

由于 `onUpdate` 调用绝不会重叠，因此 `deltaSeconds` 的调用序列会形成连贯的时间线。

在理想情况下，场景有机会运行 `onUpdate` 在每一帧渲染之前。不过，根据可用资源，负载较重的场景可能无法及时完成其 `Promise` 以赶上下一个帧。就这一点而言，每个场景都是独立的：轻量级的 `onUpdate` 实现可能会在同一时间段内被调用多次，而另一个场景中的单次调用却耗时很长。

查看 [ADR-148](https://adr.decentraland.org/adr/ADR-148) 详见。

### 运行多个场景

World Explorer 不仅必须支持运行多个沙箱化场景，还应始终如此。有两个原因：

首先，当玩家探索世界时，他们的视野会超出其所站立场景的地理边界。附近的场景（最多可达任意或可配置的距离）必须运行，以生成玩家应当能够看到的内容。

其次，场景并不只是在各自互不重叠的边界内运行。它们还可以彼此叠加，在影响玩家周围区域的同时提供不同的功能。

这一点尤其适用于两类场景：头像场景和可携带体验。

#### 头像场景 <a href="#avatarscene" id="avatarscene"></a>

从玩家进入世界的那一刻起，一个负责渲染其他玩家头像的全局场景就开始运行。

该场景不受地理边界限制，无论玩家身处何处，都可以显示其实体（以及用于通信的 UI 小组件）。

#### 可携带体验

Decentraland 允许可穿戴物品拥有自己的行为，这些行为在其各自的场景上下文中运行。这些场景以玩家为中心，可通过添加实体或显示 UI 小组件来丰富体验。

当可穿戴物品被装备时，它们开始运行；当其被移除时，它们停止运行。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/contributor/contributor-zh/chang-jing-yun-xing-shi/execution.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
