> 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/blob/main/contributor/contributor/content/entity-types/scenes.md) 就像典型的 Node 模块一样。一旦沙盒设置完成（即所有 [全局对象](https://github.com/decentraland/docs/blob/main/contributor/globals.md) 都被注入并且 [模块](https://github.com/decentraland/docs/blob/main/contributor/modules.md) 已准备好加载），场景代码将被求值，以便填充 [exports](https://github.com/decentraland/docs/blob/main/contributor/globals.md#module) 对象。

主机将从 `exports`: [`onStart`](#onStart) （可选）以及 [`onUpdate`](#onUpdate) （必需）。

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

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

{% @mermaid/diagram content="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/blob/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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
