【问题标题】:How to store different objects in an array and get an object of a certain type?如何将不同的对象存储在一个数组中,并得到一个特定类型的对象?
【发布时间】:2017-09-01 16:32:26
【问题描述】:

您可能听说过Entity Component System,其中所有内容都是Entity,每个实体都有一个控制其功能的Components 列表。

我正在尝试找出如何将不同的对象(每个都继承 Component)存储在一个数组中,并能够根据它们的类型从该数组中取出一个对象。

我能想到的第一个解决方案是为继承组件的对象类型设置一个enum

enum ComponentType : unsigned char    // There will never be more than 256 components
{
    EXAMPLE_COMPONENT,
    ANOTHER_EXAMPLE_COMPONENT,
    AND_ANOTHER_EXAMPLE_COMPONENT
};

然后Component 基类有一个带有getter 的ComponentType type;,每个子组件设置它的类型,例如:

ExampleComponent::ExampleComponent()
{
    type = EXAMPLE_COMPONENT;
}

然后我会有一个GetComponent 函数:

Component* Entity::GetComponent(ComponentType type)
{
    for (unsigned int i = 0; i < m_components.size(); i++)
    {
        if (m_components.at(i).GetType() == type)
        {
            return &m_components.at(i);
        }
    }

    return nullptr;
}

// Note: m_components is an std::vector;

最后你会打电话给GetComponent 例如:

(ExampleComponent*) component = entity.GetComponent(EXAMPLE_COMPONENT);

这样做的问题是您需要为每种类型的组件提供一个enum,并且您还必须在使用GetComponent 之后强制转换组件以确保您可以访问它自己的成员变量。

有没有人知道在不需要enum 并且不需要强制转换组件的情况下执行此操作的正确方法?如果有一个解决方案仍然需要在每个组件中存储一个类型变量,它最好是一个字节,并且不能大于 4 个字节。

编辑:我也不想使用模板

提前致谢!

大卫

【问题讨论】:

  • 我会将Entity::GetComponent 中的原始循环替换为std::find_if 和一个lambda。我还将 NULL 的使用更改为 nullptr 并摆脱 C 风格的演员 (ExampleComponent*) 并用适当的 C++ 演员替换它。还; void* 很讨厌 - 为什么不返回一个基类指针......?
  • 模板和dynamic_cast ?
  • 对我来说,您的 enum 似乎模拟了多态性。通过创建派生类来使用真正的多态性。
  • 顺便说一句:模板提供“编译时多态性”,派生类提供“运行时多态性”。你想要后者。
  • 是的,它会起作用,因为您编译代码以支持“运行时类型信息”。例如。对于g++,您不得使用-fno-rtti 编译器选项。我不期望“最佳性能”,另请参阅stackoverflow.com/questions/4050901/performance-of-dynamic-cast。该问题的部分最佳答案是“我知道您不想谈论这个,但“大量使用 dynamic_cast 的设计”可能表明您做错了什么……”

标签: c++ types enums casting


【解决方案1】:

您的方法模拟了多态性:将type 作为成员并使用if 语句检查该类型通常表明使用类层次结构。您已经说过要使用派生自Componenttype 的对象,因此您还应该正确使用多态性。

您的方法中的第二个问题是您想要过滤“特定类型”,这或多或少等同于向下转换 - 即dynamic_cast&lt;&gt;():当您将某个ComponentType 传递给Entity::GetComponent() ,它返回一个指向Component 的指针,但该指针后面的对象始终是特定派生类的对象:在您的示例中,当您将EXAMPLE_COMPONENT 传递给该函数时,您总是得到一个ExampleComponent 对象。

那么自然会出现下面的问题:你想对这个函数返回的对象做什么?您只能从Component 接口/类调用方法,但不能从派生类调用方法!所以向下转换几乎没有意义(如果你返回一个指向从Component派生的类的对象的指针。

下面是在getComponent()方法中使用多态性和向下转换的样子,返回一个指向派生类的指针——请注意,该方法是一个模板,可以方便地为从Component派生的每个类实现这一点:

#include <string>
#include <vector>
#include <iostream>

class Component {
public:
    virtual std::string getType() = 0;
};

using ComponentContainer = std::vector<Component*>;

class AComponent : public Component { public: virtual std::string getType() { return "A"; }; };
class BComponent : public Component { public: virtual std::string getType() { return "B"; }; };
class CComponent : public Component { public: virtual std::string getType() { return "C"; }; };


class Entity {
public:
    template <typename T>
    T* getComponent();

    void putComponent(Component* c) { m_components.push_back(c); }

private:
    ComponentContainer m_components;
};


template<typename T>
T* Entity::getComponent()
{
    T* t = nullptr;
    for (auto i : m_components) {
        if ((t = dynamic_cast<T*>(i)) != nullptr)
            break;
    }

    return t;
}

int main()
{
    Entity e;
    e.putComponent(new AComponent{});
    e.putComponent(new BComponent{});

    Component* c;
    if ((c = e.getComponent<AComponent>()) != nullptr)
        std::cout << c->getType() << std::endl;

    // delete all the stuff
    return 0;
}

dynamic_cast&lt;&gt;() 的大量使用从性能和设计的角度来看都是有问题的:它应该尽量少用。

所以设计问题可能是所有东西都存储在一个容器中?您可以根据“行为”使用多个容器。由于行为在 ECS 中作为派生类或接口实现,因此该实体的 getComponent() 类似方法将仅返回某些(子)接口的对象。然后这些组件都将实现给定的接口方法,因此将消除向下转换的需要。

例如,假设您有“可绘制组件”,这表明了层次结构:

// Drawable interface
class DrawableComponent : public Component {
public:
    virtual void draw() const = 0;
};

// Drawable objects derive from DrawableComponent
class  DComponent : public DrawableComponent {
public:
    virtual void draw() const { /* draw the D component */ }
};

然后,一个实体可以有一个 DrawableComponent 对象的容器,您只需遍历这些对象并在每个对象上调用 draw()

using DrawableContainer = std::vector<DrawableComponent*>;
// m_drawables is a memober of Entity with above type
const DrawableContainer& Entity::getDrawables() { return m_drawables; }

// then just draw those objects
for (auto d : entity.getDrawables())
    d->draw(); // no downcast!

【讨论】:

  • 我绝对不想使用模板或dynamic_cast。这些组件虽然没有任何功能,但它们仅用于数据。而且我不会有任何继承DrawableComponent 的组件。它只是加载不同的组件,例如DrawableComponentTransformComponentPhysicsComponent,它们存储有关实体的不同数据。然后系统接收具有某些组件的实体并执行可能更改组件数据的功能。
猜你喜欢
  • 1970-01-01
  • 2021-10-15
  • 2022-06-16
  • 2022-11-29
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
相关资源
最近更新 更多