Component Based Entity System Design Part 2

Finally! Part 2 has arrived. Purple Pwny is really still in a formulation state as far as development of an actual game goes, so we haven’t had a chance to extensively use the component system in anything other than a testing scenario. The only finalized portion of the ComponentManager interface thus far is the GetComponent function.

GetComponent takes the name of a component family and an optional specialization parameter that applies to said family. For instance, to retrieve a component of type HealthComponent specialized for an Elephant character one would call as follows:

HealthComponent *health = m_ComponentManager->GetComponent<health>("Health", "Elephant");

The ComponentManager class serves mostly to act as a factory for customized components at this time. However, I dont foresee it needing much functionality outside of that as Entities are intended to “manage” their own components.

Here is the relevant portion of the class:

template <typename T>
T* ComponentManager::GetComponent(std::string name,std::string specialization) {

    m_xmlElement = doc.FirstChildElement(name);
    m_xmlElement= m_xmlElement->FirstChildElement(specialization);
    std::map<std::string, boost::variant<int,std::string> > props;
    for ( TiXmlElement *m_xmlElementTemp = m_xmlElement->FirstChildElement(); m_xmlElementTemp; m_xmlElementTemp = m_xmlElementTemp->NextSiblingElement()) {
        int ival;
        std::string sval;
        if (m_xmlElementTemp->Attribute("intval")) {
            m_xmlElementTemp->QueryIntAttribute("intval",&ival);
            props[m_xmlElementTemp->ValueStr()] = ival;
        }
        else if (m_xmlElementTemp->Attribute("strval")) {
            m_xmlElementTemp->QueryValueAttribute("strval",&sval);
            props[m_xmlElementTemp->ValueStr()] = sval;
        }
    }

    T* comp = new T();

    comp->Set(props);
    return comp;
}

At the moment, this actively parses the XML with 0 caching of configurations. This allows changes to occur in real-time to newly added components, which, with little modification, leads to a variety of interesting editor/game possibilities. It’s very simple to convert the current system into a cached mapping of key/value pairs. The reason we have yet to do this however, is because the plan is to permanently cache components in a SQLite database in the very near future.

On the horizon: Caching component configurations, and a 3 part introduction to the Harmony renderer.

<3

Follow us @purple_pwny and the author @noxsucks. thank you :)