简体   繁体   English

基于组件的体系结构c ++

[英]Component based architecture c++

I'm having trouble figuring out a way to make a component based engine architecture in c++. 我无法找到在c ++中制作基于组件的引擎架构的方法。 But i cant figure out a way to combine a vector of components with a class that derives from component. 但我无法找到一种方法来组合组件的向量与从组件派生的类。

I want to override components virtual function. 我想覆盖组件虚函数。 But the only way i can make it call the overrided function is to make the component-derived class a pointer, but i want every gameobject to contain its own components in a vector and not outside the class as a pointer. 但是,我可以调用覆盖函数的唯一方法是使组件派生类成为指针,但我希望每个游戏对象在向量中包含自己的组件,而不是作为指针在类外部。

I tried to remove as much unnecessary code as possible. 我试图删除尽可能多的不必要的代码。

My structure: 我的结构:

//GameObject class, contains components and other objects
class GameObject
{
public:

    GameObject(){}
    ~GameObject(){}

    void AddChild(GameObject child)
    {
       children.push_back(child);
    }
    void AddComponent(Component component)
    {
       components.push_back(component);
    }

    void Input(){}
    void Update(){}
    void Render()
    {
       for(unsigned int i = 0; i < components.size(); i++)
            components[i].Render();
    }

private:

    std::vector<GameObject> children;
    std::vector<Component> components;
};

//base class component
class Component
{
public:
    Component(){}
    ~Component(){}

    virtual void Input(){}
    virtual void Update(){}
    virtual void Render(){ cout << "Component -> Render()" << endl; }

};

class MeshRenderer : public Component
{
public:
    MeshRenderer(Mesh _mesh, Material _material)
    {
        mesh = _mesh;
        material = _material
    }

    ~MeshRenderer(){}

    //override components virtual Render()
    void Render(Transform transform)
    {
        mesh.Render(material);
        cout << "MeshRenderer -> Render()" << endl;
    }

private:
    Mesh mesh;
    Material material;
};

GameObject* root = new GameObject();
MeshRenderer meshRenderer(mesh, material);

root->AddComponent(meshRenderer);

//GameLoop
while(!quit)
{
   root->Render();
}

Looks like you want to pass your objects by reference, use 看起来你想通过引用传递你的对象,使用

void AddComponent(Component& component);

to avoid any slicing . 避免任何切片


For proper usage with std::vector<> 's and polymorphic inheritance, you'll need smart pointers, eg std::unique_ptr<Component> to preserve ownership, or std::shared_ptr<Component> for shared ownership (raw pointers as Component* might work as well, but are far harder to manage correctly). 为了正确使用std::vector<>和多态继承,你需要智能指针,例如std::unique_ptr<Component>来保留所有权,或者std::shared_ptr<Component>用于共享所有权(原始指针为Component*也可以正常工作,但要正确管理起来要困难得多。

void AddComponent(std::unique_ptr<Component> componentPtr); // Unique ownership

or 要么

void AddComponent(std::shared_ptr<Component> componentPtr); // Shared ownership

and accordingly 因此

std::vector<std::unique_ptr<Component>> components;

or 要么

std::vector<std::shared_ptr<Component>> components;

It depends on your actual use cases if these Component instances should be uniquely owned by their aggregating parent GameObject class, or not. 如果这些Component实例应该由它们的聚合父GameObject类唯一拥有,则它取决于您的实际用例。


To use std::shared<> pointers, that could expire outside their usages scope you may consider using std::weak_ptr<> . 要使用std::shared<>指针,它们可以在其使用范围之外到期,您可以考虑使用std::weak_ptr<>

As mentioned, it totally depends on your use cases, and how you want these aggregated components being accessible from outside of the GameObject class. 如上所述,它完全取决于您的用例,以及您希望如何从GameObject类外部访问这些聚合组件。

It would be the best if you could use unique_ptr : 如果你可以使用unique_ptr那将是最好的:

void AddComponent(std::unique_ptr<Component> component) {
    components.push_back(std::move(component));
}

std::vector<std::unique_ptr<Component>> components;

Thus by calling AddComponent() you transfer ownership of the component to containing GameObject . 因此,通过调用AddComponent()您可以将组件的所有权转移到包含GameObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM