Browse Source

LuaScript library header formatting. Allow parameters also for procedural Lua functions. Renamed LuaScriptInstance::Execute() to ExecuteFunction().

Lasse Öörni 12 years ago
parent
commit
f93a5e4a23

+ 1 - 1
Source/Engine/Graphics/StaticModelGroup.h

@@ -27,7 +27,7 @@
 namespace Urho3D
 namespace Urho3D
 {
 {
 
 
-/// Renders several instances of static models while culling and receiving light as one unit. Using this class is not necessary for instanced rendering, but can be used as a CPU-side optimization.
+/// Renders several object instances while culling and receiving light as one unit. Can be used as a CPU-side optimization, but note that also regular StaticModels will use instanced rendering if possible.
 class URHO3D_API StaticModelGroup : public StaticModel
 class URHO3D_API StaticModelGroup : public StaticModel
 {
 {
     OBJECT(StaticModelGroup);
     OBJECT(StaticModelGroup);

+ 0 - 3
Source/Extras/LuaScript/LuaFile.h

@@ -56,13 +56,10 @@ public:
 private:
 private:
     /// File size.
     /// File size.
     unsigned size_;
     unsigned size_;
-
     /// File data.
     /// File data.
     SharedArrayPtr<char> data_;
     SharedArrayPtr<char> data_;
-
     /// Has loaded.
     /// Has loaded.
     bool hasLoaded_;
     bool hasLoaded_;
-
     /// Has executed.
     /// Has executed.
     bool hasExecuted_;
     bool hasExecuted_;
 };
 };

+ 62 - 2
Source/Extras/LuaScript/LuaScript.cpp

@@ -146,7 +146,7 @@ bool LuaScript::ExecuteString(const String& string)
     return true;
     return true;
 }
 }
 
 
-bool LuaScript::ExecuteFunction(const String& functionName)
+bool LuaScript::ExecuteFunction(const String& functionName, const VariantVector& parameters)
 {
 {
     PROFILE(ExecuteFunction);
     PROFILE(ExecuteFunction);
 
 
@@ -158,7 +158,9 @@ bool LuaScript::ExecuteFunction(const String& functionName)
         return false;
         return false;
     }
     }
 
 
-    if (lua_pcall(luaState_, 0, 0, 0))
+    unsigned numParams = PushParameters(luaState_, parameters);
+    
+    if (lua_pcall(luaState_, numParams, 0, 0))
     {
     {
         const char* message = lua_tostring(luaState_, -1);
         const char* message = lua_tostring(luaState_, -1);
         LOGERROR("Execute Lua function failed: " + String(message));
         LOGERROR("Execute Lua function failed: " + String(message));
@@ -318,6 +320,64 @@ int LuaScript::Print(lua_State *L)
     return 0;
     return 0;
 }
 }
 
 
+unsigned LuaScript::PushParameters(lua_State* state, const VariantVector& parameters)
+{
+    unsigned numParams = 0;
+    
+    for (unsigned i = 0; i < parameters.Size(); ++i)
+    {
+        switch (parameters[i].GetType())
+        {
+        case VAR_BOOL:
+            tolua_pushboolean(luaState_, parameters[i].GetBool() ? 1 : 0);
+            ++numParams;
+            break;
+            
+        case VAR_INT:
+            tolua_pushnumber(luaState_, (double)parameters[i].GetInt());
+            ++numParams;
+            break;
+            
+        case VAR_FLOAT:
+            tolua_pushnumber(luaState_, parameters[i].GetFloat());
+            ++numParams;
+            break;
+            
+        case VAR_STRING:
+            tolua_pushstring(luaState_, parameters[i].GetString().CString());
+            ++numParams;
+            break;
+            
+        case VAR_VECTOR2:
+            tolua_pushusertype(luaState_, &const_cast<Vector2&>(parameters[i].GetVector2()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+
+        case VAR_VECTOR3:
+            tolua_pushusertype(luaState_, &const_cast<Vector3&>(parameters[i].GetVector3()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+            
+        case VAR_VECTOR4:
+            tolua_pushusertype(luaState_, &const_cast<Vector4&>(parameters[i].GetVector4()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+
+        case VAR_QUATERNION:
+            tolua_pushusertype(luaState_, &const_cast<Quaternion&>(parameters[i].GetQuaternion()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+            
+        case VAR_COLOR:
+            tolua_pushusertype(luaState_, &const_cast<Color&>(parameters[i].GetColor()), parameters[i].GetTypeName().CString());
+            ++numParams;
+            break;
+        }
+    }
+    
+    return numParams;
+}
+
 int LuaScript::GetScriptFunctionRef(const String& functionName, bool silentIfNotFound)
 int LuaScript::GetScriptFunctionRef(const String& functionName, bool silentIfNotFound)
 {
 {
     HashMap<String, int>::Iterator i = functionNameToFunctionRefMap_.Find(functionName);
     HashMap<String, int>::Iterator i = functionNameToFunctionRefMap_.Find(functionName);

+ 13 - 31
Source/Extras/LuaScript/LuaScript.h

@@ -45,79 +45,61 @@ public:
     /// Destruct.
     /// Destruct.
     ~LuaScript();
     ~LuaScript();
 
 
-    /// Execute script file.
+    /// Execute script file. Return true if successful.
     bool ExecuteFile(const String& fileName);
     bool ExecuteFile(const String& fileName);
-
-    /// Execute script string.
+    /// Execute script string. Return true if successful.
     bool ExecuteString(const String& string);
     bool ExecuteString(const String& string);
-
-    /// Execute script function.
-    bool ExecuteFunction(const String& functionName);
-
+    /// Execute script function. Parameters can be supplied in a VariantVector. Return true if successful.
+    bool ExecuteFunction(const String& functionName, const VariantVector& parameters = Variant::emptyVariantVector);
     /// Script send event.
     /// Script send event.
     void ScriptSendEvent(const String& eventName, VariantMap& eventData);
     void ScriptSendEvent(const String& eventName, VariantMap& eventData);
-
     /// Script subscribe to an event that can by send by any sender.
     /// Script subscribe to an event that can by send by any sender.
     void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
     void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
-
     /// Script unsubscribe from an event.
     /// Script unsubscribe from an event.
     void ScriptUnsubscribeFromEvent(const String& eventName);
     void ScriptUnsubscribeFromEvent(const String& eventName);
-
     /// Script unsubscribe from all events.
     /// Script unsubscribe from all events.
     void ScriptUnsubscribeFromAllEvents();
     void ScriptUnsubscribeFromAllEvents();
-
     /// Script subscribe to a specific sender's event.
     /// Script subscribe to a specific sender's event.
     void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
     void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
-
     /// Script unsubscribe from a specific sender's event.
     /// Script unsubscribe from a specific sender's event.
     void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
     void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
-
     /// Script unsubscribe from a specific sender's all events.
     /// Script unsubscribe from a specific sender's all events.
     void ScriptUnsubscribeFromEvents(void* sender);
     void ScriptUnsubscribeFromEvents(void* sender);
-
+    /// Push parameters from a VariantVector to a Lua state. Return number of parameters successfully pushed.
+    unsigned PushParameters(lua_State* state, const VariantVector& parameters);
+    
     /// Return Lua state.
     /// Return Lua state.
     lua_State* GetLuaState() const { return luaState_; }
     lua_State* GetLuaState() const { return luaState_; }
-
     /// Return script function ref.
     /// Return script function ref.
     int GetScriptFunctionRef(const String& functionName, bool silentIfNotfound = false);
     int GetScriptFunctionRef(const String& functionName, bool silentIfNotfound = false);
-
+    
 private:
 private:
     /// Register loader.
     /// Register loader.
     void RegisterLoader();
     void RegisterLoader();
-
-    /// Loader.
-    static int Loader(lua_State* L);
-
     /// Replace print.
     /// Replace print.
     void ReplacePrint();
     void ReplacePrint();
-
-    /// Print function.
-    static int Print(lua_State* L);
-
     /// Handle event.
     /// Handle event.
     void HandleEvent(StringHash eventType, VariantMap& eventData);
     void HandleEvent(StringHash eventType, VariantMap& eventData);
-
     /// Handle object event.
     /// Handle object event.
     void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
     void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
-
     /// Handle a console command event.
     /// Handle a console command event.
     void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
     void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
-
     /// Push script function.
     /// Push script function.
     bool PushScriptFunction(const String& functionName, bool silentIfNotfound = false);
     bool PushScriptFunction(const String& functionName, bool silentIfNotfound = false);
-
     /// Call script function.
     /// Call script function.
     void CallScriptFunction(int functionRef, StringHash eventType, VariantMap& eventData);
     void CallScriptFunction(int functionRef, StringHash eventType, VariantMap& eventData);
 
 
+    /// Loader.
+    static int Loader(lua_State* L);
+    /// Print function.
+    static int Print(lua_State* L);
+
     /// Lua state.
     /// Lua state.
     lua_State* luaState_;
     lua_State* luaState_;
-
     /// Function name to function ref map.
     /// Function name to function ref map.
     HashMap<String, int> functionNameToFunctionRefMap_;
     HashMap<String, int> functionNameToFunctionRefMap_;
-
     /// Event type to function ref map.
     /// Event type to function ref map.
     HashMap<StringHash, int> eventTypeToFunctionRefMap_;
     HashMap<StringHash, int> eventTypeToFunctionRefMap_;
-
     /// Object to event type to function ref map.
     /// Object to event type to function ref map.
     HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
     HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
 };
 };

+ 2 - 54
Source/Extras/LuaScript/LuaScriptInstance.cpp

@@ -262,7 +262,7 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvents(void* sender)
     objectToEventTypeToFunctionRefMap_.Erase(it);
     objectToEventTypeToFunctionRefMap_.Erase(it);
 }
 }
 
 
-bool LuaScriptInstance::Execute(const String& functionName, const VariantVector& parameters)
+bool LuaScriptInstance::ExecuteFunction(const String& functionName, const VariantVector& parameters)
 {
 {
     if (scriptObjectRef_ == LUA_REFNIL)
     if (scriptObjectRef_ == LUA_REFNIL)
         return false;
         return false;
@@ -538,59 +538,7 @@ bool LuaScriptInstance::CallScriptObjectFunction(int functionRef, const VariantV
     // Push script object.
     // Push script object.
     lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
     lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
 
 
-    unsigned numParams = 1;
-    
-    // Push specified parameters
-    for (unsigned i = 0; i < parameters.Size(); ++i)
-    {
-        switch (parameters[i].GetType())
-        {
-        case VAR_BOOL:
-            tolua_pushboolean(luaState_, parameters[i].GetBool() ? 1 : 0);
-            ++numParams;
-            break;
-            
-        case VAR_INT:
-            tolua_pushnumber(luaState_, (double)parameters[i].GetInt());
-            ++numParams;
-            break;
-            
-        case VAR_FLOAT:
-            tolua_pushnumber(luaState_, parameters[i].GetFloat());
-            ++numParams;
-            break;
-            
-        case VAR_STRING:
-            tolua_pushstring(luaState_, parameters[i].GetString().CString());
-            ++numParams;
-            break;
-            
-        case VAR_VECTOR2:
-            tolua_pushusertype(luaState_, &const_cast<Vector2&>(parameters[i].GetVector2()), parameters[i].GetTypeName().CString());
-            ++numParams;
-            break;
-
-        case VAR_VECTOR3:
-            tolua_pushusertype(luaState_, &const_cast<Vector3&>(parameters[i].GetVector3()), parameters[i].GetTypeName().CString());
-            ++numParams;
-            break;
-            
-        case VAR_VECTOR4:
-            tolua_pushusertype(luaState_, &const_cast<Vector4&>(parameters[i].GetVector4()), parameters[i].GetTypeName().CString());
-            ++numParams;
-            break;
-
-        case VAR_QUATERNION:
-            tolua_pushusertype(luaState_, &const_cast<Quaternion&>(parameters[i].GetQuaternion()), parameters[i].GetTypeName().CString());
-            ++numParams;
-            break;
-            
-        case VAR_COLOR:
-            tolua_pushusertype(luaState_, &const_cast<Color&>(parameters[i].GetColor()), parameters[i].GetTypeName().CString());
-            ++numParams;
-            break;
-        }
-    }
+    unsigned numParams = 1 + luaScript_->PushParameters(luaState_, parameters);
     
     
     // Call script object function.
     // Call script object function.
     if (lua_pcall(luaState_, numParams, 0, 0) != 0)
     if (lua_pcall(luaState_, numParams, 0, 0) != 0)

+ 7 - 43
Source/Extras/LuaScript/LuaScriptInstance.h

@@ -62,127 +62,91 @@ public:
     /// Register object factory.
     /// Register object factory.
     static void RegisterObject(Context* context);
     static void RegisterObject(Context* context);
 
 
-    ///// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
+    /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
     virtual void ApplyAttributes();
     virtual void ApplyAttributes();
 
 
-    /// Create script object.
+    /// Create script object. Return true if successful.
     bool CreateObject(const String& scriptObjectType);
     bool CreateObject(const String& scriptObjectType);
-
-    /// Create script object.
+    /// Create script object. Return true if successful.
     bool CreateObject(const String& scriptFileName, const String& scriptObjectType);
     bool CreateObject(const String& scriptFileName, const String& scriptObjectType);
-
     /// Set script file name.
     /// Set script file name.
     void SetScriptFileName(const String& scriptFileName);
     void SetScriptFileName(const String& scriptFileName);
-
     /// Set script object type.
     /// Set script object type.
     void SetScriptObjectType(const String& scriptObjectType);
     void SetScriptObjectType(const String& scriptObjectType);
-
     /// Set script file serialization attribute by calling a script function.
     /// Set script file serialization attribute by calling a script function.
     void SetScriptDataAttr(PODVector<unsigned char> data);
     void SetScriptDataAttr(PODVector<unsigned char> data);
-    
     /// Set script network serialization attribute by calling a script function.
     /// Set script network serialization attribute by calling a script function.
     void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
     void SetScriptNetworkDataAttr(PODVector<unsigned char> data);
-
     /// Script subscribe to an event that can by send by any sender.
     /// Script subscribe to an event that can by send by any sender.
     void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
     void ScriptSubscribeToEvent(const String& eventName, const String& functionName);
-
     /// Script unsubscribe from an event.
     /// Script unsubscribe from an event.
     void ScriptUnsubscribeFromEvent(const String& eventName);
     void ScriptUnsubscribeFromEvent(const String& eventName);
-
     /// Script unsubscribe from all events.
     /// Script unsubscribe from all events.
     void ScriptUnsubscribeFromAllEvents();
     void ScriptUnsubscribeFromAllEvents();
-
     /// Script subscribe to a specific sender's event.
     /// Script subscribe to a specific sender's event.
     void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
     void ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName);
-
     /// Script unsubscribe from a specific sender's event.
     /// Script unsubscribe from a specific sender's event.
     void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
     void ScriptUnsubscribeFromEvent(void* sender, const String& eventName);
-
     /// Script unsubscribe from a specific sender's all events.
     /// Script unsubscribe from a specific sender's all events.
     void ScriptUnsubscribeFromEvents(void* sender);
     void ScriptUnsubscribeFromEvents(void* sender);
-
-    /// Execute a script object function with parameters. Return true if successful.
-    bool Execute(const String& functionName, const VariantVector& parameters);
+    /// Execute a script object function. Parameters can be supplied in a VariantVector. Return true if successful.
+    bool ExecuteFunction(const String& functionName, const VariantVector& parameters = Variant::emptyVariantVector);
     
     
     /// Return script file name.
     /// Return script file name.
     const String& GetScriptFileName() const { return scriptFileName_; }
     const String& GetScriptFileName() const { return scriptFileName_; }
-
     /// Return script object type.
     /// Return script object type.
     const String& GetScriptObjectType() const { return scriptObjectType_; }
     const String& GetScriptObjectType() const { return scriptObjectType_; }
-
     /// Return script object ref.
     /// Return script object ref.
     int GetScriptObjectRef() const { return scriptObjectRef_; }
     int GetScriptObjectRef() const { return scriptObjectRef_; }
-
     /// Get script file serialization attribute by calling a script function.
     /// Get script file serialization attribute by calling a script function.
     PODVector<unsigned char> GetScriptDataAttr() const;
     PODVector<unsigned char> GetScriptDataAttr() const;
-
     /// Get script network serialization attribute by calling a script function.
     /// Get script network serialization attribute by calling a script function.
     PODVector<unsigned char> GetScriptNetworkDataAttr() const;
     PODVector<unsigned char> GetScriptNetworkDataAttr() const;
 
 
 private:
 private:
     /// Find script object method refs.
     /// Find script object method refs.
     void FindScriptObjectMethodRefs();
     void FindScriptObjectMethodRefs();
-
     /// Handle the logic update event.
     /// Handle the logic update event.
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
-
     /// Handle the logic post update event.
     /// Handle the logic post update event.
     void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
     void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
-
     /// Handle the physics update event.
     /// Handle the physics update event.
     void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
     void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
-
     /// Handle the physics post update event.
     /// Handle the physics post update event.
     void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
     void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
-
     /// Handle event.
     /// Handle event.
     void HandleEvent(StringHash eventType, VariantMap& eventData);
     void HandleEvent(StringHash eventType, VariantMap& eventData);
-
     /// Handle a specific sender's event.
     /// Handle a specific sender's event.
     void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
     void HandleObjectEvent(StringHash eventType, VariantMap& eventData);
-
     /// Release the script object.
     /// Release the script object.
     void ReleaseObject();
     void ReleaseObject();
-    
     /// Call script object function.
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef);
     void CallScriptObjectFunction(int functionRef);
-
     /// Call script object function.
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef, float timeStep);
     void CallScriptObjectFunction(int functionRef, float timeStep);
-
     /// Call script object function.
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef, Deserializer& deserializer);
     void CallScriptObjectFunction(int functionRef, Deserializer& deserializer);
-
     /// Call script object function.
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef, Serializer& serializer) const;
     void CallScriptObjectFunction(int functionRef, Serializer& serializer) const;
-
     /// Call script object function.
     /// Call script object function.
     void CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData);
     void CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData);
-
     /// Call script object function with arbitrary parameters.
     /// Call script object function with arbitrary parameters.
     bool CallScriptObjectFunction(int functionRef, const VariantVector& parameters);
     bool CallScriptObjectFunction(int functionRef, const VariantVector& parameters);
-
-    // Lua Script.
+    
+    // Lua Script subsystem.
     LuaScript* luaScript_;
     LuaScript* luaScript_;
-
     /// Lua state.
     /// Lua state.
     lua_State* luaState_;
     lua_State* luaState_;
-
     /// Script file name.
     /// Script file name.
     String scriptFileName_;
     String scriptFileName_;
-
     /// Script object type.
     /// Script object type.
     String scriptObjectType_;
     String scriptObjectType_;
-
     /// Script object ref.
     /// Script object ref.
     int scriptObjectRef_;
     int scriptObjectRef_;
-
     /// Script object method refs.
     /// Script object method refs.
     int scriptObjectMethodRefs_[MAX_LUA_SCRIPT_OBJECT_METHODS];
     int scriptObjectMethodRefs_[MAX_LUA_SCRIPT_OBJECT_METHODS];
-
     /// Event type to function ref map.
     /// Event type to function ref map.
     HashMap<StringHash, int> eventTypeToFunctionRefMap_;
     HashMap<StringHash, int> eventTypeToFunctionRefMap_;
-    
     /// Object to event type to function ref map.
     /// Object to event type to function ref map.
     HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
     HashMap<Object*, HashMap<StringHash, int> > objectToEventTypeToFunctionRefMap_;
 };
 };

+ 1 - 1
Source/Samples/22_LuaIntegration/LuaIntegration.cpp

@@ -115,7 +115,7 @@ void LuaIntegration::CreateScene()
         parameters.Push(10.0f);
         parameters.Push(10.0f);
         parameters.Push(20.0f);
         parameters.Push(20.0f);
         parameters.Push(30.0f);
         parameters.Push(30.0f);
-        instance->Execute("SetRotationSpeed", parameters);
+        instance->ExecuteFunction("SetRotationSpeed", parameters);
     }
     }
     
     
     // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
     // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can