Roland Posted October 27, 2014 Share Posted October 27, 2014 Calling a Lua Script function from C++ without arguments works just fine. No problem. But when it comes to bringing along an argument I need some hint. Here is an example. LUA: Script.myval = 10 function Script::increment() self.myval = self.myval + 1 end function Script::add( val ) self.myval = self.myval + val end C++: (assuming we have the entity to which the script attached) entity->CallFunction( "increment" ); // works fine. no problem Any idea how to add argument for the call of "add" ? I understand that this has to be done using the Interpreter class. But how ? Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
Josh Posted October 27, 2014 Share Posted October 27, 2014 Here's an example: bool Entity::CallFunction(const std::string& name, Entity* entity, const Vec3& position, const Vec3& normal, const float speed) { if (component==NULL) return false; bool success = false; //Get the component table int stacksize = Interpreter::GetStackSize(); //Get the global error handler function int errorfunctionindex = 0; #ifdef DEBUG Interpreter::GetGlobal("LuaErrorHandler"); errorfunctionindex = Interpreter::GetStackSize(); #endif Push(); Interpreter::GetField("script"); if (Interpreter::IsTable()) { Interpreter::GetField(name); if (Interpreter::IsFunction()) { Interpreter::PushValue(-2);//Push script table onto stack Interpreter::PushObject(entity); Interpreter::PushVec3(position); Interpreter::PushVec3(normal); Interpreter::PushFloat(speed); #ifdef DEBUG errorfunctionindex = -(Interpreter::GetStackSize()-errorfunctionindex+1); #endif success = Interpreter::Invoke(5,0,errorfunctionindex); } } Interpreter::SetStackSize(stacksize); return success; } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Roland Posted October 27, 2014 Author Share Posted October 27, 2014 Thank's a lot Josh. Works like a charm Quote Roland Strålberg Website: https://rstralberg.com Link to comment Share on other sites More sharing options...
beo6 Posted October 27, 2014 Share Posted October 27, 2014 So what is the extra Object pointer good for in this method? bool CallFunction(const std::string& name, Object* extra=NULL); Quote Link to comment Share on other sites More sharing options...
Josh Posted October 29, 2014 Share Posted October 29, 2014 It's an argument that will be passed to the Lua function, if present. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.