|
|
@@ -25,6 +25,7 @@
|
|
|
#include "Context.h"
|
|
|
#include "Log.h"
|
|
|
#include "LuaFile.h"
|
|
|
+#include "LuaFunction.h"
|
|
|
#include "LuaScript.h"
|
|
|
#include "LuaScriptInstance.h"
|
|
|
#include "MemoryBuffer.h"
|
|
|
@@ -42,33 +43,34 @@ extern "C"
|
|
|
}
|
|
|
#include "tolua++.h"
|
|
|
|
|
|
+#include "DebugNew.h"
|
|
|
+
|
|
|
namespace Urho3D
|
|
|
{
|
|
|
|
|
|
static const char* scriptObjectMethodNames[] = {
|
|
|
- ".Start",
|
|
|
- ".Stop",
|
|
|
- // ".DelayedStart",
|
|
|
- ".Update",
|
|
|
- ".PostUpdate",
|
|
|
- ".FixedUpdate",
|
|
|
- ".FixedPostUpdate",
|
|
|
- ".Load",
|
|
|
- ".Save",
|
|
|
- ".ReadNetworkUpdate",
|
|
|
- ".WriteNetworkUpdate",
|
|
|
- ".ApplyAttributes"
|
|
|
+ "Start",
|
|
|
+ "Stop",
|
|
|
+ "Update",
|
|
|
+ "PostUpdate",
|
|
|
+ "FixedUpdate",
|
|
|
+ "FixedPostUpdate",
|
|
|
+ "Load",
|
|
|
+ "Save",
|
|
|
+ "ReadNetworkUpdate",
|
|
|
+ "WriteNetworkUpdate",
|
|
|
+ "ApplyAttributes"
|
|
|
};
|
|
|
|
|
|
-LuaScriptInstance::LuaScriptInstance(Context* context) :
|
|
|
+LuaScriptInstance::LuaScriptInstance(Context* context) :
|
|
|
Component(context),
|
|
|
scriptObjectRef_(LUA_REFNIL)
|
|
|
{
|
|
|
luaScript_ = GetSubsystem<LuaScript>();
|
|
|
- luaState_ = luaScript_->GetLuaState();
|
|
|
+ luaState_ = luaScript_->GetState();
|
|
|
|
|
|
for (unsigned i = 0; i < MAX_LUA_SCRIPT_OBJECT_METHODS; ++i)
|
|
|
- scriptObjectMethodRefs_[i] = LUA_REFNIL;
|
|
|
+ scriptObjectMethods_[i] = 0;
|
|
|
}
|
|
|
|
|
|
LuaScriptInstance::~LuaScriptInstance()
|
|
|
@@ -89,13 +91,17 @@ void LuaScriptInstance::RegisterObject(Context* context)
|
|
|
|
|
|
void LuaScriptInstance::ApplyAttributes()
|
|
|
{
|
|
|
- CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_APPLYATTRIBUTES]);
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_APPLYATTRIBUTES];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::OnSetEnabled()
|
|
|
{
|
|
|
if (enabled_)
|
|
|
- SubscribeToScriptMethodEvents();
|
|
|
+ SubscribeToScriptMethodEvents();
|
|
|
else
|
|
|
UnsubscribeFromScriptMethodEvents();
|
|
|
}
|
|
|
@@ -140,36 +146,15 @@ void LuaScriptInstance::SetScriptObjectType(const String& scriptObjectType)
|
|
|
|
|
|
ReleaseObject();
|
|
|
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- lua_getglobal(luaState_, "CreateScriptObjectInstance");
|
|
|
- if (!lua_isfunction(luaState_, -1))
|
|
|
- {
|
|
|
- LOGERROR("Could not find lua function CreateScriptObjectInstance");
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Get table as first paramter.
|
|
|
- lua_getglobal(luaState_, scriptObjectType.CString());
|
|
|
- if (!lua_istable(luaState_, -1))
|
|
|
- {
|
|
|
- LOGERROR("Could not find lua table " + scriptObjectType);
|
|
|
- lua_settop(luaState_, top);
|
|
|
+ LuaFunction* function = luaScript_->GetFunction("CreateScriptObjectInstance");
|
|
|
+ if (!function || !function->BeginCall())
|
|
|
return;
|
|
|
- }
|
|
|
-
|
|
|
- // Push this as second parameter.
|
|
|
- tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
|
|
|
|
- // Call ObjectType:new function.
|
|
|
- if (lua_pcall(luaState_, 2, 1, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
+ function->PushLuaTable(scriptObjectType);
|
|
|
+ function->PushUserType((void*)this, "LuaScriptInstance");
|
|
|
+
|
|
|
+ if (!function->EndCall(1))
|
|
|
return;
|
|
|
- }
|
|
|
|
|
|
scriptObjectType_ = scriptObjectType;
|
|
|
scriptObjectRef_ = luaL_ref(luaState_, LUA_REGISTRYINDEX);
|
|
|
@@ -180,34 +165,48 @@ void LuaScriptInstance::SetScriptObjectType(const String& scriptObjectType)
|
|
|
|
|
|
void LuaScriptInstance::SetScriptDataAttr(PODVector<unsigned char> data)
|
|
|
{
|
|
|
- int functionRef = scriptObjectMethodRefs_[LSOM_LOAD];
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
+ return;
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_LOAD];
|
|
|
+ if (!function)
|
|
|
return;
|
|
|
|
|
|
MemoryBuffer buf(data);
|
|
|
- CallScriptObjectFunction(functionRef, (Deserializer&)buf);
|
|
|
+ if (function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType((Deserializer&)buf, "Deserializer");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::SetScriptNetworkDataAttr(PODVector<unsigned char> data)
|
|
|
{
|
|
|
- int functionRef = scriptObjectMethodRefs_[LSOM_READNETWORKUPDATE];
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
return;
|
|
|
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_READNETWORKUPDATE];
|
|
|
+ if (!function)
|
|
|
+ return;
|
|
|
+
|
|
|
MemoryBuffer buf(data);
|
|
|
- CallScriptObjectFunction(functionRef, (Deserializer&)buf);
|
|
|
+ if (function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType((Deserializer&)buf, "Deserializer");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::ScriptSubscribeToEvent(const String& eventName, const String& functionName)
|
|
|
{
|
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
|
|
|
|
- int functionRef = luaScript_->GetScriptFunctionRef(realFunctionName);
|
|
|
- if (functionRef != LUA_REFNIL)
|
|
|
+ LuaFunction* function = luaScript_->GetFunction(realFunctionName);
|
|
|
+ if (function)
|
|
|
{
|
|
|
StringHash eventType(eventName);
|
|
|
SubscribeToEvent(eventType, HANDLER(LuaScriptInstance, HandleEvent));
|
|
|
- eventTypeToFunctionRefMap_[eventType] = functionRef;
|
|
|
+ eventTypeToFunctionMap_[eventType] = function;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -215,34 +214,34 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvent(const String& eventName)
|
|
|
{
|
|
|
StringHash eventType(eventName);
|
|
|
|
|
|
- HashMap<StringHash, int>::Iterator i = eventTypeToFunctionRefMap_.Find(eventType);
|
|
|
- if (i != eventTypeToFunctionRefMap_.End())
|
|
|
+ HashMap<StringHash, LuaFunction*>::Iterator i = eventTypeToFunctionMap_.Find(eventType);
|
|
|
+ if (i != eventTypeToFunctionMap_.End())
|
|
|
{
|
|
|
UnsubscribeFromEvent(eventType);
|
|
|
- eventTypeToFunctionRefMap_.Erase(i);
|
|
|
+ eventTypeToFunctionMap_.Erase(i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::ScriptUnsubscribeFromAllEvents()
|
|
|
{
|
|
|
- if (eventTypeToFunctionRefMap_.Empty())
|
|
|
+ if (eventTypeToFunctionMap_.Empty())
|
|
|
return;
|
|
|
|
|
|
UnsubscribeFromAllEvents();
|
|
|
- eventTypeToFunctionRefMap_.Clear();
|
|
|
+ eventTypeToFunctionMap_.Clear();
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::ScriptSubscribeToEvent(void* sender, const String& eventName, const String& functionName)
|
|
|
{
|
|
|
String realFunctionName = functionName.Replaced(":", ".");
|
|
|
|
|
|
- int functionRef = luaScript_->GetScriptFunctionRef(realFunctionName);
|
|
|
- if (functionRef != LUA_REFNIL)
|
|
|
+ LuaFunction* function = luaScript_->GetFunction(realFunctionName);
|
|
|
+ if (function)
|
|
|
{
|
|
|
Object* object = (Object*)sender;
|
|
|
StringHash eventType(eventName);
|
|
|
SubscribeToEvent(object, eventType, HANDLER(LuaScriptInstance, HandleObjectEvent));
|
|
|
- objectToEventTypeToFunctionRefMap_[object][eventType] = functionRef;
|
|
|
+ objectToEventTypeToFunctionMap_[object][eventType] = function;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -251,11 +250,11 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvent(void* sender, const String& e
|
|
|
StringHash eventType(eventName);
|
|
|
Object* object = (Object*)sender ;
|
|
|
|
|
|
- HashMap<StringHash, int>::Iterator i = objectToEventTypeToFunctionRefMap_[object].Find(eventType);
|
|
|
- if (i != objectToEventTypeToFunctionRefMap_[object].End())
|
|
|
+ HashMap<StringHash, LuaFunction*>::Iterator i = objectToEventTypeToFunctionMap_[object].Find(eventType);
|
|
|
+ if (i != objectToEventTypeToFunctionMap_[object].End())
|
|
|
{
|
|
|
UnsubscribeFromEvent(object, eventType);
|
|
|
- objectToEventTypeToFunctionRefMap_[object].Erase(i);
|
|
|
+ objectToEventTypeToFunctionMap_[object].Erase(i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -263,55 +262,56 @@ void LuaScriptInstance::ScriptUnsubscribeFromEvents(void* sender)
|
|
|
{
|
|
|
Object* object = (Object*)sender;
|
|
|
|
|
|
- HashMap<Object*, HashMap<StringHash, int> >::Iterator it = objectToEventTypeToFunctionRefMap_.Find(object);
|
|
|
- if (it == objectToEventTypeToFunctionRefMap_.End())
|
|
|
+ HashMap<Object*, HashMap<StringHash, LuaFunction*> >::Iterator it = objectToEventTypeToFunctionMap_.Find(object);
|
|
|
+ if (it == objectToEventTypeToFunctionMap_.End())
|
|
|
return;
|
|
|
|
|
|
UnsubscribeFromEvents(object);
|
|
|
- objectToEventTypeToFunctionRefMap_.Erase(it);
|
|
|
+ objectToEventTypeToFunctionMap_.Erase(it);
|
|
|
}
|
|
|
|
|
|
-bool LuaScriptInstance::ExecuteFunction(const String& functionName, const VariantVector& parameters)
|
|
|
+PODVector<unsigned char> LuaScriptInstance::GetScriptDataAttr() const
|
|
|
{
|
|
|
if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
- return false;
|
|
|
-
|
|
|
- int functionRef = luaScript_->GetScriptFunctionRef(scriptObjectType_ + "." + functionName);
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return false;
|
|
|
-
|
|
|
- return CallScriptObjectFunction(functionRef, parameters);
|
|
|
-}
|
|
|
+ return PODVector<unsigned char>();
|
|
|
|
|
|
-PODVector<unsigned char> LuaScriptInstance::GetScriptDataAttr() const
|
|
|
-{
|
|
|
- int functionRef = scriptObjectMethodRefs_[LSOM_SAVE];
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_SAVE];
|
|
|
+ if (!function)
|
|
|
return PODVector<unsigned char>();
|
|
|
|
|
|
VectorBuffer buf;
|
|
|
- CallScriptObjectFunction(functionRef, (Serializer&)buf);
|
|
|
+ if (function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType((Serializer&)buf, "Serializer");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
|
|
|
return buf.GetBuffer();
|
|
|
}
|
|
|
|
|
|
PODVector<unsigned char> LuaScriptInstance::GetScriptNetworkDataAttr() const
|
|
|
{
|
|
|
- int functionRef = scriptObjectMethodRefs_[LSOM_WRITENETWORKUPDATE];
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL || functionRef == LUA_REFNIL)
|
|
|
+ if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
+ return PODVector<unsigned char>();
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_WRITENETWORKUPDATE];
|
|
|
+ if (!function)
|
|
|
return PODVector<unsigned char>();
|
|
|
|
|
|
VectorBuffer buf;
|
|
|
- CallScriptObjectFunction(functionRef, (Serializer&)buf);
|
|
|
+ if (function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType((Serializer&)buf, "Serializer");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
|
|
|
return buf.GetBuffer();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
void LuaScriptInstance::FindScriptObjectMethodRefs()
|
|
|
{
|
|
|
for (unsigned i = 0; i < MAX_LUA_SCRIPT_OBJECT_METHODS; ++i)
|
|
|
- scriptObjectMethodRefs_[i] = luaScript_->GetScriptFunctionRef(scriptObjectType_ + scriptObjectMethodNames[i], true);
|
|
|
+ scriptObjectMethods_[i] = GetScriptObjectFunction(scriptObjectMethodNames[i]);
|
|
|
|
|
|
if (enabled_)
|
|
|
SubscribeToScriptMethodEvents();
|
|
|
@@ -319,31 +319,31 @@ void LuaScriptInstance::FindScriptObjectMethodRefs()
|
|
|
|
|
|
void LuaScriptInstance::SubscribeToScriptMethodEvents()
|
|
|
{
|
|
|
- if (scriptObjectMethodRefs_[LSOM_UPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_UPDATE])
|
|
|
SubscribeToEvent(E_UPDATE, HANDLER(LuaScriptInstance, HandleUpdate));
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_POSTUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_POSTUPDATE])
|
|
|
SubscribeToEvent(E_POSTUPDATE, HANDLER(LuaScriptInstance, HandlePostUpdate));
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_FIXEDUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_FIXEDUPDATE])
|
|
|
SubscribeToEvent(E_PHYSICSPRESTEP, HANDLER(LuaScriptInstance, HandleFixedUpdate));
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_FIXEDPOSTUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_FIXEDPOSTUPDATE])
|
|
|
SubscribeToEvent(E_PHYSICSPOSTSTEP, HANDLER(LuaScriptInstance, HandlePostFixedUpdate));
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::UnsubscribeFromScriptMethodEvents()
|
|
|
{
|
|
|
- if (scriptObjectMethodRefs_[LSOM_UPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_UPDATE])
|
|
|
UnsubscribeFromEvent(E_UPDATE);
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_POSTUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_POSTUPDATE])
|
|
|
UnsubscribeFromEvent(E_POSTUPDATE);
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_FIXEDUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_FIXEDUPDATE])
|
|
|
UnsubscribeFromEvent(E_PHYSICSPRESTEP);
|
|
|
|
|
|
- if (scriptObjectMethodRefs_[LSOM_FIXEDPOSTUPDATE] != LUA_REFNIL)
|
|
|
+ if (scriptObjectMethods_[LSOM_FIXEDPOSTUPDATE])
|
|
|
UnsubscribeFromEvent(E_PHYSICSPOSTSTEP);
|
|
|
}
|
|
|
|
|
|
@@ -351,53 +351,75 @@ void LuaScriptInstance::HandleUpdate(StringHash eventType, VariantMap& eventData
|
|
|
{
|
|
|
using namespace Update;
|
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
- CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_UPDATE], timeStep);
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_UPDATE];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushFloat(timeStep);
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
using namespace PostUpdate;
|
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
- CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_POSTUPDATE], timeStep);
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_POSTUPDATE];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushFloat(timeStep);
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::HandleFixedUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
using namespace PhysicsPreStep;
|
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
- CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_FIXEDUPDATE], timeStep);
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_FIXEDUPDATE];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushFloat(timeStep);
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
using namespace PhysicsPostStep;
|
|
|
float timeStep = eventData[P_TIMESTEP].GetFloat();
|
|
|
- CallScriptObjectFunction(scriptObjectMethodRefs_[LSOM_FIXEDPOSTUPDATE], timeStep);
|
|
|
+
|
|
|
+ LuaFunction* function = scriptObjectMethods_[LSOM_FIXEDPOSTUPDATE];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushFloat(timeStep);
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::HandleEvent(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int functionRef = eventTypeToFunctionRefMap_[eventType];
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- CallScriptObjectFunction(functionRef, eventType, eventData);
|
|
|
+ LuaFunction* function = eventTypeToFunctionMap_[eventType];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType(eventType, "StringHash");
|
|
|
+ function->PushUserType(eventData, "VariantMap");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::HandleObjectEvent(StringHash eventType, VariantMap& eventData)
|
|
|
{
|
|
|
- if (scriptObjectRef_ == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
Object* object = GetEventSender();
|
|
|
- int functionRef = objectToEventTypeToFunctionRefMap_[object][eventType];
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- CallScriptObjectFunction(functionRef, eventType, eventData);
|
|
|
+ LuaFunction* function = objectToEventTypeToFunctionMap_[object][eventType];
|
|
|
+ if (function && function->BeginCall(this))
|
|
|
+ {
|
|
|
+ function->PushUserType(eventType, "StringHash");
|
|
|
+ function->PushUserType(eventData, "VariantMap");
|
|
|
+ function->EndCall();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void LuaScriptInstance::ReleaseObject()
|
|
|
@@ -412,182 +434,17 @@ void LuaScriptInstance::ReleaseObject()
|
|
|
luaL_unref(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
scriptObjectRef_ = LUA_REFNIL;
|
|
|
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
- lua_getglobal(luaState_, "DestroyScriptObjectInstance");
|
|
|
- if (!lua_isfunction(luaState_, -1))
|
|
|
+ LuaFunction* function = luaScript_->GetFunction("DestroyScriptObjectInstance");
|
|
|
+ if (function && function->BeginCall())
|
|
|
{
|
|
|
- LOGERROR("Could not find lua function DestroyScriptObjectInstance");
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Push this as second parameter.
|
|
|
- tolua_pushusertype(luaState_, (void*)this, "LuaScriptInstance");
|
|
|
- if (lua_pcall(luaState_, 1, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef)
|
|
|
-{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 1, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef, float timeStep)
|
|
|
-{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Push time step.
|
|
|
- tolua_pushnumber(luaState_, timeStep);
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Deserializer& deserializer)
|
|
|
-{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Push Deserializer.
|
|
|
- tolua_pushusertype(luaState_, (void*)&deserializer, "Deserializer");
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef, Serializer& serializer) const
|
|
|
-{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Push Deserializer.
|
|
|
- tolua_pushusertype(luaState_, (void*)&serializer, "Serializer");
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 2, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void LuaScriptInstance::CallScriptObjectFunction(int functionRef, StringHash eventType, VariantMap& eventData)
|
|
|
-{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Push event type.
|
|
|
- tolua_pushusertype(luaState_, (void*)&eventType, "StringHash");
|
|
|
-
|
|
|
- // Push event data.
|
|
|
- tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 3, 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
+ function->PushUserType((void*)this, "LuaScriptInstance");
|
|
|
+ function->EndCall();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool LuaScriptInstance::CallScriptObjectFunction(int functionRef, const VariantVector& parameters)
|
|
|
+LuaFunction* LuaScriptInstance::GetScriptObjectFunction(const String& functionName)
|
|
|
{
|
|
|
- if (functionRef == LUA_REFNIL)
|
|
|
- return false;
|
|
|
-
|
|
|
- int top = lua_gettop(luaState_);
|
|
|
-
|
|
|
- // Push function.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef);
|
|
|
-
|
|
|
- // Push script object.
|
|
|
- lua_rawgeti(luaState_, LUA_REGISTRYINDEX, scriptObjectRef_);
|
|
|
-
|
|
|
- // Push parameters.
|
|
|
- if (!tolua_pushurho3dvariantvector(luaState_, parameters))
|
|
|
- {
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // Call script object function.
|
|
|
- if (lua_pcall(luaState_, 1 + parameters.Size(), 0, 0) != 0)
|
|
|
- {
|
|
|
- const char* message = lua_tostring(luaState_, -1);
|
|
|
- LOGERROR("Execute Lua function failed: " + String(message));
|
|
|
- lua_settop(luaState_, top);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
+ return luaScript_->GetFunction(scriptObjectType_ + "." + functionName, true);
|
|
|
}
|
|
|
|
|
|
}
|