# Gosub scene

## 命令

## Gosub scene

`*gosub_scene` 是基本 \[\[Gosub|\*gosub]] 命令的扩展。

普通的 `*gosub` 要求其子程序必须位于同一场景文件内，而 `*gosub_scene` 则提供了从任意游戏文件中“调用”（即加载并运行）子程序的能力，从而进一步减少了在多个文件中重复使用代码的需求。

关于子程序是什么以及它们实际如何工作的更完整解释，请参阅基础的 \[\[Gosub|\*gosub]] 命令页面。

## 用法｜Usage

在以下示例中，玩家面对一小群敌对敌人，需要做出一个简单的选择。

```
*choice
    #Strike the first blow.
        *set enemy_type "orc"
        *set enemy_group 4
        *gosub_scene combat
        *finish
    #Flee!
        You very sensibly take to your heels and escape your pursuers.
        *finish
```

如果玩家选择参与战斗，我们将设定他们面对的敌人类型和数量，然后通过`*gosub_scene combat` 来“调用”一个子程序，从而无需在当前场景文件中包含实际的战斗脚本。

该命令所引用的“战斗”文件本质上将包含处理这场战斗并向玩家描述所需的所有脚本和叙事内容，甚至可能包含详细的回合制描述及过程中的选择（如果游戏设计需要的话）。此外，该战斗程序可能处理各种类型和数量的敌人，或许还会考虑主角自身的武器装备与技能等多种额外因素。

通过使用 `*gosub_scene` 命令，所有这些脚本只需在独立的文件中编写一次，从而在游戏开发过程中更容易进行优化和扩展。更重要的是，此后可以在任何场景文件中随时“调用”——使用`*gosub_scene combat`——而无需在其他游戏文件中重复编写。

当然，并非每款游戏都会包含如此精细的战斗系统（或可能需要大量可复用脚本的类似功能），但许多游戏会使用一系列不同的小型子程序来实现各种目的，例如属性检查或提供条件性叙事回应。由于 `*gosub_scene` 也允许我们根据需要引用特定的 \[\[Label|\*label]] 名称，这使我们能够将所有不同的子程序集中存放在单个文件中，从而有效避免了在每个独立场景文件中重复嵌入各种次要子程序的需求。例如：

```
*choice
    #Train body.
        *set strength +10
        *set dexterity +10
        *gosub_scene routines cap_stats
        You train your body.
        *finish
    #Train mind.
        *set intelligence +10
        *set wisdom +10
        *gosub_scene routines cap_stats
        You sharpen your mind.
        *finish
```

在这个例子中，我们正在"调用"子程序 `cap_stats`，这将是位于名为 routines 的文件中某个特定 `*label` 部分。当它遇到这行 `*gosub_scene` 命令时，ChoiceScript 将在后台加载 routines 文件，直接跳转到该文件内的 `*label cap_stats` 行，并立即运行那里的脚本，直到遇到 \[\[Return|\*return]] 命令。此时，它将返回到当前场景文件中如上所示的 `*gosub_scene` 行，并从那里继续执行，以显示其下方适当的叙事响应。

要查看 `cap_stats` 子程序可能包含的实际脚本内容，请参阅 \[\[Gosub|\*gosub]] 命令页面

## `gosub_scene` 与 `goto_scene` 的区别｜Difference between Gosub\_scene and Goto\_scene

ChoiceScript 的基本特性是在加载新场景时“卸载”当前场景（即使用\[\[Finish|\*finish]] 或 \[\[Goto\_scene|\*goto\_scene]]命令时），这意味着所有现有的场景特定\[\[Temp|\*temp]]变量和 `#option_reuse` 状态（\[\[Hide\_reuse|\*hide\_reuse]]、\[\[Disable\_reuse|\*disable\_reuse]] 和 \[\[Allow\_reuse|\*allow\_reuse]]）都会从内存中丢失——主要是因为游戏进入新场景文件后，这些数据实际上已不再需要。

`*gosub_scene` 本质上是在后台加载指定文件（尽管短暂），且在此过程中并不会真正“卸载”当前场景文件。这意味着，与 `*goto_scene` 不同，当前场景的所有 `*temp` 变量和 `#option_reuse` 状态都会保留在内存中，并在子程序使用后继续生效。


---

# 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://raster.gitbook.io/zh-hans_choicescript-guide/gosub-scene.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.
