Esempio n. 1
0
Component* Node::CloneComponent(Component* component, CreateMode mode, unsigned id)
{
    if (!component)
    {
        LOGERROR("Null source component given for CloneComponent");
        return 0;
    }

    Component* cloneComponent = SafeCreateComponent(component->GetTypeName(), component->GetType(), mode, 0);
    if (!cloneComponent)
    {
        LOGERROR("Could not clone component " + component->GetTypeName());
        return 0;
    }

    const Vector<AttributeInfo>* compAttributes = component->GetAttributes();
    const Vector<AttributeInfo>* cloneAttributes = cloneComponent->GetAttributes();
    
    if (compAttributes)
    {
        for (unsigned i = 0; i < compAttributes->Size() && i < cloneAttributes->Size(); ++i)
        {
            const AttributeInfo& attr = compAttributes->At(i);
            const AttributeInfo& cloneAttr = cloneAttributes->At(i);
            if (attr.mode_ & AM_FILE)
            {
                Variant value;
                component->OnGetAttribute(attr, value);
                // Note: when eg. a ScriptInstance component is cloned, its script object attributes are unique and therefore we
                // can not simply refer to the source component's AttributeInfo
                cloneComponent->OnSetAttribute(cloneAttr, value);
            }
        }
        cloneComponent->ApplyAttributes();
    }
    
    return cloneComponent;
}