Files
XCEngine/docs/api/XCEngine/Components/GameObject/Constructor.md

50 lines
1.3 KiB
Markdown
Raw Normal View History

2026-04-03 16:11:48 +08:00
# GameObject::Constructor
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
构造一个 `GameObject`
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
`GameObject` 当前提供两个公开构造形式:
2026-03-26 16:45:24 +08:00
```cpp
GameObject();
2026-04-03 16:11:48 +08:00
explicit GameObject(const std::string& name);
2026-03-26 16:45:24 +08:00
```
2026-04-03 16:11:48 +08:00
## 默认构造行为
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
默认构造当前会建立如下初始状态:
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
- `name = "GameObject"`
- `tag = "Untagged"`
- `activeSelf = true`
- `started = false`
- `layer = 0`
- `scene = nullptr`
- `parent = nullptr`
- 自动创建一个内建 `TransformComponent`
- 分配新的递增 ID 与随机 UUID
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
`tests/Components/test_game_object.cpp` 中的 `DefaultConstructor_DefaultValues` 已验证默认 `tag``layer`、激活态和 `Transform` 都会按这套语义建立。
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
## 命名构造行为
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
`name` 的构造只会把对象名替换为传入值,其他默认状态保持一致:
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
- `tag` 仍是 `"Untagged"`
- `layer` 仍是 `0`
- 仍然会创建内建 `Transform`
- 仍然不会自动加入任何 `Scene`
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
## 重要边界
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
- 直接构造对象不会自动注册到全局 registry
- 不会自动触发 `Awake()`
- 不会自动接入场景层级
如果你需要完整运行时语义,应优先通过 [Scene::CreateGameObject](../../Scene/Scene/CreateGameObject.md) 创建。
2026-03-26 16:45:24 +08:00
## 相关文档
2026-04-03 16:11:48 +08:00
- [GameObject](GameObject.md)
- [GetTransform](GetTransform.md)
- [Scene::CreateGameObject](../../Scene/Scene/CreateGameObject.md)