Browse Source

Expose CloneComponent() function to AngelScript & Lua and make it return the created component.

Lasse Öörni 11 years ago
parent
commit
5076f966e1

+ 3 - 1
Source/Engine/LuaScript/pkgs/Scene/Node.pkg

@@ -102,7 +102,9 @@ class Node : public Animatable
     
     // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
     Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
-    
+    Component* CloneComponent(Component* component, unsigned id = 0);
+    Component* CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
+
     int CreateScriptObject(const String scriptObjectType);
     int CreateScriptObject(const String fileName, const String scriptObjectType);
     

+ 17 - 9
Source/Engine/Scene/Node.cpp

@@ -729,24 +729,30 @@ Component* Node::GetOrCreateComponent(ShortStringHash type, CreateMode mode, uns
         return CreateComponent(type, mode, id);
 }
 
-void Node::CloneComponent(Component* component, unsigned id)
+Component* Node::CloneComponent(Component* component, unsigned id)
 {
-    if(!component)
-        return;
-
-    CloneComponent(component, component->GetID() < FIRST_LOCAL_ID ? REPLICATED : LOCAL, id);
+    if (!component)
+    {
+        LOGERROR("Null source component given for CloneComponent");
+        return 0;
+    }
+    
+    return CloneComponent(component, component->GetID() < FIRST_LOCAL_ID ? REPLICATED : LOCAL, id);
 }
 
-void Node::CloneComponent(Component* component, CreateMode mode, unsigned id)
+Component* Node::CloneComponent(Component* component, CreateMode mode, unsigned id)
 {
-    if(!component)
-        return;
+    if (!component)
+    {
+        LOGERROR("Null source component given for CloneComponent");
+        return 0;
+    }
 
     Component* cloneComponent = CreateComponent(component->GetType(), mode, 0);
     if (!cloneComponent)
     {
         LOGERROR("Could not clone component " + component->GetTypeName());
-        return;
+        return 0;
     }
 
     const Vector<AttributeInfo>* compAttributes = component->GetAttributes();
@@ -759,6 +765,8 @@ void Node::CloneComponent(Component* component, CreateMode mode, unsigned id)
                 cloneComponent->SetAttribute(j, component->GetAttribute(j));
         }
     }
+    
+    return cloneComponent;
 }
 
 void Node::RemoveComponent(Component* component)

+ 4 - 4
Source/Engine/Scene/Node.h

@@ -192,10 +192,10 @@ public:
     Component* CreateComponent(ShortStringHash type, CreateMode mode = REPLICATED, unsigned id = 0);
     /// Create a component to this node if it does not exist already.
     Component* GetOrCreateComponent(ShortStringHash type, CreateMode mode = REPLICATED, unsigned id = 0);
-    /// Clone a component from another node.
-    void CloneComponent(Component* component, unsigned id = 0);
-    /// Clone a component from another node.
-    void CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
+    /// Clone a component from another node using its create mode. Return the clone if successful or null on failure.
+    Component* CloneComponent(Component* component, unsigned id = 0);
+    /// Clone a component from another node and specify the create mode. Return the clone if successful or null on failure.
+    Component* CloneComponent(Component* component, CreateMode mode, unsigned id = 0);
     /// Remove a component from this node.
     void RemoveComponent(Component* component);
     /// Remove the first component of specific type from this node.

+ 2 - 0
Source/Engine/Script/APITemplates.h

@@ -631,6 +631,8 @@ template <class T> void RegisterNode(asIScriptEngine* engine, const char* classN
     engine->RegisterObjectMethod(className, "void Remove()", asMETHOD(T, Remove), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "Component@+ CreateComponent(const String&in, CreateMode mode = REPLICATED, uint id = 0)", asFUNCTION(NodeCreateComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "Component@+ GetOrCreateComponent(const String&in, CreateMode mode = REPLICATED, uint id = 0)", asFUNCTION(NodeGetOrCreateComponent), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod(className, "Component@+ CloneComponent(Component@+, uint id = 0)", asMETHODPR(T, CloneComponent, (Component*, unsigned), Component*), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "Component@+ CloneComponent(Component@+, CreateMode, uint id = 0)", asMETHODPR(T, CloneComponent, (Component*, CreateMode, unsigned), Component*), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void RemoveComponent(Component@+)", asMETHODPR(T, RemoveComponent, (Component*), void), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void RemoveComponent(const String&in)", asFUNCTION(NodeRemoveComponent), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "void RemoveAllComponents()", asMETHOD(T, RemoveAllComponents), asCALL_THISCALL);