Files
XCEngine/docs/api/XCEngine/Scripting/ScriptEngine/OnRuntimeStop.md

58 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ScriptEngine::OnRuntimeStop
**命名空间**: `XCEngine::Scripting`
**类型**: `method`
**头文件**: `XCEngine/Scripting/ScriptEngine.h`
## 签名
```cpp
void OnRuntimeStop();
```
## 当前实现流程
- 如果订阅了场景创建事件,会先退订并处理退订队列。
- 若当前并未运行:
- 清空 `m_runtimeScene`
- 清空跟踪状态和顺序表
-`m_runtimeFixedDeltaTime` 重置为 [DefaultFixedDeltaTime](DefaultFixedDeltaTime.md)
- 返回
- 若正在运行:
- 遍历所有跟踪脚本,调用 `StopTrackingScript(..., true)`
- 清空状态表与顺序表
-`m_runtimeRunning` 置为 `false`
- 清空 `m_runtimeScene`
-`m_runtimeFixedDeltaTime` 重置为 [DefaultFixedDeltaTime](DefaultFixedDeltaTime.md)
- 最后调用 `m_runtime->OnRuntimeStop(stoppedScene)`
## 当前真实语义
`StopTrackingScript()` 会在实例存在时按 `OnDisable -> OnDestroy -> DestroyScriptInstance` 清理,因此这不是简单停更,而是完整结束脚本运行态。
此外,这一步也会把当前运行时固定步长配置恢复成默认值;如果下一次启动仍想使用非默认 fixed delta需要在启动前重新调用 [SetRuntimeFixedDeltaTime](SetRuntimeFixedDeltaTime.md)。
## 关键语义
- 即使当前 `m_runtimeRunning == false`,这个方法也仍然会清空 `m_runtimeScene`、跟踪表和 fixed delta 配置。它不只是“停止已运行中的脚本”,也是“把脚本运行时状态整体收回默认值”的统一入口。
- 只有在原本确实处于运行态时,才会调用底层运行时 `m_runtime->OnRuntimeStop(stoppedScene)`
- 场景创建事件的退订发生在最前面,避免停机过程中再被新对象创建事件打断。
## 设计意义
把“实例停机”“状态表清空”“fixed delta 恢复默认值”“通知具体运行时停机”统一收口到一个出口,可以减少 Play 模式切换和场景重启时的残留状态。
## 真实行为依据
- `engine/src/Scripting/ScriptEngine.cpp`
- `tests/Scripting/test_script_engine.cpp`
## 相关文档
- [DefaultFixedDeltaTime](DefaultFixedDeltaTime.md)
- [SetRuntimeFixedDeltaTime](SetRuntimeFixedDeltaTime.md)
- [OnRuntimeStart](OnRuntimeStart.md)
- [OnScriptComponentDestroyed](OnScriptComponentDestroyed.md)