docs: expand gameobject helper docs

This commit is contained in:
2026-04-03 16:11:48 +08:00
parent ee44e14960
commit 2c2e1fab1c
34 changed files with 597 additions and 603 deletions

View File

@@ -1,42 +1,32 @@
# GameObject::GetComponents
获取相关状态或对象
返回当前对象自身上的全部匹配组件
该方法`XCEngine/Components/GameObject.h` 中提供了 2 个重载,当前页面统一汇总这些公开声明。
## 重载 1: 声明
该方法当前提供两个公开重载:
```cpp
template<typename T> std::vector<T*> GetComponents();
```
**参数:** 无。
**返回:** `template<typename T> std::vector<T*>` - 返回值语义详见头文件声明。
## 重载 2: 声明
```cpp
template<typename T> std::vector<const T*> GetComponents() const;
```
**参数:** 无。
## 收集顺序
**返回:** `template<typename T> std::vector<const T*>` - 返回值语义详见头文件声明。
两个重载的收集顺序一致:
**示例:**
1. 先尝试把内建 `m_transform` `dynamic_cast``T`
2. 若命中,则先把它加入结果
3. 再按 `m_components` 当前顺序,把所有匹配普通组件追加到结果
```cpp
#include <XCEngine/Components/GameObject.h>
## 当前语义边界
void Example() {
XCEngine::Components::GameObject object;
// 根据上下文补齐参数后调用 GameObject::GetComponents(...)。
(void)object;
}
```
- 只收集当前对象自身,不递归父节点或子节点
- `TransformComponent` 即使不在 `m_components` 中,只要类型匹配也会出现在结果里
- 结果顺序受普通组件添加顺序影响
`tests/Components/test_game_object.cpp` 中的 `GetComponents_Multiple` 已验证多个同类型普通组件会全部被返回。
## 相关文档
- [返回类总览](GameObject.md)
- [返回模块目录](../Components.md)
- [GetComponent](GetComponent.md)
- [GetComponentsInChildren](GetComponentsInChildren.md)
- [GetTransform](GetTransform.md)