我一直在努力创建自己的实体组件系统,并且我准备通过执行以下操作来获取组件:
const auto& component = entity->GetComponent<ComponentType>();
上面的函数看起来像这样:
template <typename TyComponent>
TyComponent* Entity<T>::GetComponent() const
{
return &(GetComponent(TyComponent::Id());
}
如果找到,则返回基于关联 id 的组件,否则返回nullptr
。
- 我正在做的事情可行吗?
- 有没有办法确保只有从 Component 派生的类型才能用作参数
GetComponent
?