将 std::shared_ptr 添加到地图会破坏程序行为
Adding std::shared_ptr to a map ruins programs behaviour
很抱歉,我无法为我的问题想出更好的标题。我遇到了以下问题:
我有一个游戏对象class,代表世界上一切可能的事物。它可以包含多个定义其行为的组件。游戏对象可渲染如下:
//GameObjectPtr is std::shared_ptr<GameObject>
GameObjectPtr skyBoxGameObject = GameObject::makeGameObject();
skyBoxGameObject->AddComponent<RenderComponent>(skyBox);
在主循环中,如果我现在从 GameObject 中获取 RenderComponent,如下所示:
planeGameObject->GetComponent<RenderComponent>()->Draw();
skyBoxGameObject->GetComponent<RenderComponent>()->Draw();
一切正常。另一方面,我现在想实现一个 RenderSystem,它通过 ID 存储 GameObjects 并再次使用 std::shared_ptr:
保存对它们的引用
//In RenderSystem:
std::map<unsigned int, std::shared_ptr<GameObject>> m_gameObjects;
将游戏对象添加到 RenderingSystem 通过以下方式完成:
void RenderSystem::AddGameObject(std::shared_ptr<GameObject> gameObjectPtr)
{
if (m_gameObjects.count(gameObjectPtr->GetId()) > 0)
throw std::runtime_error("GameObject already registered");
if (gameObjectPtr->GetComponent<RenderComponent>() == nullptr)
throw std::runtime_error("GameObject doesn't provide rendercomponent");
m_gameObjects[gameObjectPtr->GetId()] = gameObjectPtr;
}
然后画成这样:
void RenderSystem::DrawAll() const
{
for (auto const & gameObject : m_gameObjects)
{
gameObject.second->GetComponent<RenderComponent>()->Draw();
}
}
很遗憾,Objects 显示不正确。我的代码有什么问题还是 copied/moved 将它添加到不应该的地图?我没有头绪。
感谢您的帮助!
查看这段代码:
void RenderSystem::DrawAll() const
{
for (auto const & gameObject : m_gameObjects)
{
gameObject.second->GetComponent<RenderComponent>()->Draw();
}
}
Draw
函数按照映射 m_gameObjects
中存储的 gameObject
的顺序调用,而不是您实际需要的顺序。
vector
或 deque
似乎是存储对象的更好选择,但要按照您要绘制的顺序。
很抱歉,我无法为我的问题想出更好的标题。我遇到了以下问题:
我有一个游戏对象class,代表世界上一切可能的事物。它可以包含多个定义其行为的组件。游戏对象可渲染如下:
//GameObjectPtr is std::shared_ptr<GameObject>
GameObjectPtr skyBoxGameObject = GameObject::makeGameObject();
skyBoxGameObject->AddComponent<RenderComponent>(skyBox);
在主循环中,如果我现在从 GameObject 中获取 RenderComponent,如下所示:
planeGameObject->GetComponent<RenderComponent>()->Draw();
skyBoxGameObject->GetComponent<RenderComponent>()->Draw();
一切正常。另一方面,我现在想实现一个 RenderSystem,它通过 ID 存储 GameObjects 并再次使用 std::shared_ptr:
保存对它们的引用 //In RenderSystem:
std::map<unsigned int, std::shared_ptr<GameObject>> m_gameObjects;
将游戏对象添加到 RenderingSystem 通过以下方式完成:
void RenderSystem::AddGameObject(std::shared_ptr<GameObject> gameObjectPtr)
{
if (m_gameObjects.count(gameObjectPtr->GetId()) > 0)
throw std::runtime_error("GameObject already registered");
if (gameObjectPtr->GetComponent<RenderComponent>() == nullptr)
throw std::runtime_error("GameObject doesn't provide rendercomponent");
m_gameObjects[gameObjectPtr->GetId()] = gameObjectPtr;
}
然后画成这样:
void RenderSystem::DrawAll() const
{
for (auto const & gameObject : m_gameObjects)
{
gameObject.second->GetComponent<RenderComponent>()->Draw();
}
}
很遗憾,Objects 显示不正确。我的代码有什么问题还是 copied/moved 将它添加到不应该的地图?我没有头绪。
感谢您的帮助!
查看这段代码:
void RenderSystem::DrawAll() const
{
for (auto const & gameObject : m_gameObjects)
{
gameObject.second->GetComponent<RenderComponent>()->Draw();
}
}
Draw
函数按照映射 m_gameObjects
中存储的 gameObject
的顺序调用,而不是您实际需要的顺序。
vector
或 deque
似乎是存储对象的更好选择,但要按照您要绘制的顺序。