Przeglądaj źródła

Add CreateScriptObject GetScriptObject Lua API in Node class.

Aster Jian 12 lat temu
rodzic
commit
80acf3e718

+ 4 - 4
Bin/Data/LuaScripts/05_AnimatingScene.lua

@@ -71,10 +71,10 @@ function CreateScene()
         -- Add the Rotator script object which will rotate the scene node each frame, when the scene sends its update event.
         -- This requires the C++ component LuaScriptInstance in the scene node, which acts as a container. We need to tell the 
         -- class name to instantiate the object
-        local instance = boxNode:CreateComponent("LuaScriptInstance")
-        instance:CreateObject("Rotator")
-        --instance.object.rotationSpeed = Vector3(10.0, 20.0, 30.0)
-        instance.object.rotationSpeed = {10.0, 20.0, 30.0}
+        
+
+        local object = boxNode:CreateScriptObject("Rotator")
+        object.rotationSpeed = {10.0, 20.0, 30.0}
     end
 
     -- Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can

+ 3 - 3
Bin/Data/LuaScripts/06_SkeletalAnimation.lua

@@ -97,9 +97,9 @@ function CreateScene()
         state.looped = true
 
         -- Create our Mover script object that will move & animate the model during each frame's update. 
-        local instance = modelNode:CreateComponent("LuaScriptInstance")
-        instance:CreateObject("Mover")
-        instance.object:SetParameters(MODEL_MOVE_SPEED, MODEL_ROTATE_SPEED, bounds)
+
+        local object = modelNode:CreateScriptObject("Mover")
+        object:SetParameters(MODEL_MOVE_SPEED, MODEL_ROTATE_SPEED, bounds)
     end
 
     -- Create the camera. Limit far clip distance to match the fog

+ 4 - 0
Docs/LuaScriptAPI.dox

@@ -3738,6 +3738,10 @@ Methods:<br>
 - void AddListener(Component* component)
 - void RemoveListener(Component* component)
 - Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0)
+- object CreateScriptObject(const String scriptObjectType)
+- object CreateScriptObject(const String fileName, const String scriptObjectType)
+- object GetScriptObject()
+- object GetScriptObject(const String scriptObjectType)
 - unsigned GetID() const
 - const String& GetName() const
 - StringHash GetNameHash() const

+ 7 - 7
Source/Extras/LuaScript/LuaScriptInstance.cpp

@@ -58,9 +58,9 @@ void LuaScriptInstance::RegisterObject(Context* context)
     context->RegisterFactory<LuaScriptInstance>();
 }
 
-bool LuaScriptInstance::CreateObject(const String& objectType)
+bool LuaScriptInstance::CreateObject(const String& scriptObjectType)
 {
-    if (objectType_ == objectType)
+    if (scriptObjectType_ == scriptObjectType)
         return true;
 
     ReleaseObject();
@@ -76,10 +76,10 @@ bool LuaScriptInstance::CreateObject(const String& objectType)
     }
 
     // Get table as first paramter.
-    lua_getglobal(luaState_, objectType.CString());
+    lua_getglobal(luaState_, scriptObjectType.CString());
     if (!lua_istable(luaState_, -1))
     {
-        LOGERROR("Could not find lua table " + objectType);
+        LOGERROR("Could not find lua table " + scriptObjectType);
         lua_settop(luaState_, top);
         return false;
     }
@@ -96,13 +96,13 @@ bool LuaScriptInstance::CreateObject(const String& objectType)
         return false;
     }
 
-    objectType_ = objectType;
+    scriptObjectType_ = scriptObjectType;
     scriptObjectRef_ = luaL_ref(luaState_, LUA_REGISTRYINDEX);
 
     return true;
 }
 
-bool LuaScriptInstance::CreateObject(const String& fileName, const String& objectType)
+bool LuaScriptInstance::CreateObject(const String& fileName, const String& scriptObjectType)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     
@@ -113,7 +113,7 @@ bool LuaScriptInstance::CreateObject(const String& fileName, const String& objec
     if (!luaFile->LoadAndExecute(luaState_))
         return false;
 
-    return CreateObject(objectType);
+    return CreateObject(scriptObjectType);
 }
 
 void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)

+ 11 - 5
Source/Extras/LuaScript/LuaScriptInstance.h

@@ -45,10 +45,10 @@ public:
     static void RegisterObject(Context* context);
 
     /// Create script object.
-    bool CreateObject(const String& objectType);
+    bool CreateObject(const String& scriptObjectType);
 
     /// Create script object.
-    bool CreateObject(const String& fileName, const String& objectType);
+    bool CreateObject(const String& fileName, const String& scriptObjectType);
 
     /// Script subscribe to an event that can by send by any sender.
     void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
@@ -67,7 +67,13 @@ public:
 
     /// Script unsubscribe from a specific sender's all events.
     void ScriptUnsubscribeFromEvents(void* object);
-    
+
+    /// Return script object type.
+    const String& GetScriptObjectType() const { return scriptObjectType_; }
+
+    /// Return script object ref.
+    int GetScriptObjectRef() const { return scriptObjectRef_; }
+
 private:
     /// Handle event.
     void HandleEvent(StringHash eventType, VariantMap& eventData);
@@ -87,8 +93,8 @@ private:
     /// Lua state.
     lua_State* luaState_;
 
-    /// Object type.
-    String objectType_;
+    /// Script object type.
+    String scriptObjectType_;
 
     /// Script object ref.
     int scriptObjectRef_;

+ 4 - 2
Source/Extras/LuaScript/pkgs/LuaScript/LuaScriptInstance.pkg

@@ -2,14 +2,16 @@ $#include "LuaScriptInstance.h"
 
 class LuaScriptInstance : public Component
 {
-    bool CreateObject(const String objectType);
-    bool CreateObject(const String fileName, const String objectType);
+    bool CreateObject(const String scriptObjectType);
+    bool CreateObject(const String fileName, const String scriptObjectType);
     void ScriptSubscribeToEvent @ SubscribeToEvent(const String eventName, const String functionName);
     void ScriptUnsubscribeFromEvent @ UnsubscribeFromEvent(const String eventName);
     void ScriptUnsubscribeFromAllEvents @ UnsubscribeFromAllEvents();    
     void ScriptSubscribeToEvent @ SubscribeToEvent(void* object, const String eventName, const String functionName);
     void ScriptUnsubscribeFromEvent @ UnsubscribeFromEvent(void* object, const String eventName);
     void ScriptUnsubscribeFromEvents @ UnsubscribeFromEvents(void* object);
+    const String GetScriptObjectType() const;
+    int GetScriptObjectRef() const;
 };
 
 $[

+ 159 - 0
Source/Extras/LuaScript/pkgs/Scene/Node.pkg

@@ -1,5 +1,6 @@
 $#include "File.h"
 $#include "Node.h"
+$#include "LuaScriptInstance.h"
 
 enum CreateMode
 {
@@ -98,6 +99,12 @@ class Node : public Serializable
     // template <class T> T* CreateComponent(CreateMode mode = REPLICATED, unsigned id = 0);
     Component* CreateComponent(const String type, CreateMode mode = REPLICATED, unsigned id = 0);
     
+    int CreateScriptObject(const String scriptObjectType);
+    int CreateScriptObject(const String fileName, const String scriptObjectType);
+    
+    int GetScriptObject() const;
+    int GetScriptObject(const String scriptObjectType) const;
+    
     unsigned GetID() const;
     const String& GetName() const;
     StringHash GetNameHash() const;
@@ -413,4 +420,156 @@ static int tolua_SceneLuaAPI_Node_GetComponent00(lua_State* tolua_S)
  return 0;
 #endif
 }
+#define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject00
+
+static int tolua_SceneLuaAPI_Node_CreateScriptObject00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
+ !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+  Node* self = (Node*)  tolua_tousertype(tolua_S,1,0);
+  const String scriptObjectType = ((const String)  tolua_tourho3dstring(tolua_S,2,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
+#endif
+ {
+  LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
+  if (!instance)
+    lua_pushnil(tolua_S);
+  else
+  {
+    instance->CreateObject(scriptObjectType);
+    // Push script object to Lua stack.
+    lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
+  }
+ }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'CreateScriptObject'.",&tolua_err);
+ return 0;
+#endif
+}
+
+#define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_CreateScriptObject01
+
+static int tolua_SceneLuaAPI_Node_CreateScriptObject01(lua_State* tolua_S)
+{
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"Node",0,&tolua_err) ||
+ !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
+ !tolua_isurho3dstring(tolua_S,3,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,4,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+ {
+  Node* self = (Node*)  tolua_tousertype(tolua_S,1,0);
+  const String fileName = ((const String)  tolua_tourho3dstring(tolua_S,2,0));
+  const String scriptObjectType = ((const String)  tolua_tourho3dstring(tolua_S,3,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'NodeCreateScriptObject'", NULL);
+#endif
+ {
+  LuaScriptInstance* instance = self->CreateComponent<LuaScriptInstance>();
+  if (!instance)
+    lua_pushnil(tolua_S);
+  else
+  {
+    instance->CreateObject(fileName, scriptObjectType);
+    // Push script object to Lua stack.
+    lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
+  }
+ }
+ }
+ return 1;
+tolua_lerror:
+ return tolua_SceneLuaAPI_Node_CreateScriptObject00(tolua_S);
+}
+
+#define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject00
+static int tolua_SceneLuaAPI_Node_GetScriptObject00(lua_State* tolua_S)
+{
+#ifndef TOLUA_RELEASE
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,2,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+#endif
+ {
+  const Node* self = (const Node*)  tolua_tousertype(tolua_S,1,0);
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
+#endif
+ {
+  LuaScriptInstance* instance = self->GetComponent<LuaScriptInstance>();
+  if (!instance)
+    lua_pushnil(tolua_S);
+  else
+    lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
+ }
+ }
+ return 1;
+#ifndef TOLUA_RELEASE
+ tolua_lerror:
+ tolua_error(tolua_S,"#ferror in function 'GetScriptObject'.",&tolua_err);
+ return 0;
+#endif
+}
+
+#define TOLUA_DISABLE_tolua_SceneLuaAPI_Node_GetScriptObject01
+static int tolua_SceneLuaAPI_Node_GetScriptObject01(lua_State* tolua_S)
+{
+ tolua_Error tolua_err;
+ if (
+ !tolua_isusertype(tolua_S,1,"const Node",0,&tolua_err) ||
+ !tolua_isurho3dstring(tolua_S,2,0,&tolua_err) ||
+ !tolua_isnoobj(tolua_S,3,&tolua_err)
+ )
+ goto tolua_lerror;
+ else
+ {
+  const Node* self = (const Node*)  tolua_tousertype(tolua_S,1,0);
+  const String scriptObjectType = ((const String)  tolua_tourho3dstring(tolua_S,2,0));
+#ifndef TOLUA_RELEASE
+ if (!self) tolua_error(tolua_S,"invalid 'self' in function 'GetScriptObject'", NULL);
+#endif
+ {
+ int scriptObjectRef = LUA_REFNIL;
+ 
+ PODVector<LuaScriptInstance*> instances;
+ self->GetComponents<LuaScriptInstance>(instances, false);
+ 
+ for (unsigned i = 0; i < instances.Size(); ++i)
+ {
+  if (instances[i]->GetScriptObjectType() == scriptObjectType)
+  {
+    scriptObjectRef = instances[i]->GetScriptObjectRef();
+    break;
+  } 
+ }
+ 
+ if (scriptObjectRef = LUA_REFNIL)
+  lua_pushnil(tolua_S);
+ else
+  lua_rawgeti(tolua_S, LUA_REGISTRYINDEX, scriptObjectRef);
+ }
+ }
+ return 1;
+tolua_lerror:
+ return tolua_SceneLuaAPI_Node_GetScriptObject00(tolua_S);
+}
 $}