Skip to main content
gamedev.net
🔒 Locked

Returning function from Lua

Started by Rip7 Aug 29, 2006 at 6:18 AM 8 replies 1.6k views
Original Post
Rip7
Rip7
First of all I use Luabind as an interface between Lua (5.1) and C++ I want to return a table with lua functions that I can call in C++. When I read the docs I had the idea to use a luabind object te catch the result and then to execute this object. Now in practice this doesn't really work. Here is my test code: Lua: ----

Commands = {}

Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end
Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end
Commands.Construct = function (FromProxy, TargetProxy) print ("Construct") end
Commands.Destroy =  function (FromProxy, TartgetProxy) print ("Destroy") end

function SetPropertyCommand (Type)

return Commands

end

C++: (Commands_ is the luabind::object) ----

Commandgroup::Commandgroup (std::string Type, LuaStateObject& Luahandling):
Luahandling_ (Luahandling)
{
  Commands_ = luabind::call_function <luabind::object> (Luahandling_.Getstate(),
  "SetPropertyCommand", Type);
}

void Commandgroup::ExecCommand (std::string CommandName, ProxyObj& Target, ProxyObj& Source)
{
  //TEST
  luabind::call_function <void> (Commands_[1], boost::ref(Target),
  boost::ref(Source)); //error
  //TEST
}
Error: ------ Assertion failed: crep && "you are trying to use an unregistered type", file ../ ../../../Luabind/luabind/luabind/luabind/detail/policy.hpp, line 808 This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Press any key to continue. Now I wonder if Lua returns this as a 'pass by value' or as a reference I have also tried to pass a luabind object as a reference argument (with boost::ref) but this didn't work either. Sorry if this is a noob question but I'm totally new with Lua. Thanks for your help
streamer
streamer
Why don't you try to return multiple values?
Like:
return Commands.Attack, Commands.Defend, Commands.Destroy
Rip7
Rip7
Quote:
Original post by streamer
Why don't you try to return multiple values?
Like:
return Commands.Attack, Commands.Defend, Commands.Destroy


I have tried that already but the problem remains. Also changing Commands_[1] to Commands_ and passing only one function (like Commands.Attack) doesn't help. Maybe it is impossible to do this? Or the way the function is passed is wrong.
streamer
streamer
But what are you try to do? Pass values to LUA or pass values from LUA to c++?

also if this:

Commands.Attack = function (FromProxy, TargetProxy) print ("Attack") end
Commands.Defend = function (FromProxy, TargetProxy) print ("Defend") end
Commands.Construct = function (FromProxy, TargetProxy) print ("Construct") end
Commands.Destroy = function (FromProxy, TartgetProxy) print ("Destroy") end

are function definitions you need

Commands.Attack = function Attack(FromProxy, TargetProxy) print ("Attack") end
Commands.Defend = function Defend(FromProxy, TargetProxy) print ("Defend") end
Commands.Construct = function Construct(FromProxy, TargetProxy) print ("Construct") end
Commands.Destroy = function Destroy(FromProxy, TartgetProxy) print ("Destroy") end

and also they are missing returning values

function Attack(FromProxy, TargetProxy)
print ("Attack")
AttackNMY(1)
return 1
end

Commands.Attack = Attack(1,1)

in c++:
...
int AttackNMY(lua_State *pLuaState )
{
const int s = (int)lua_tonumber(pLuaState,1);

// s is argument of AttackNMY in LUA

return 1;
}

...
lua_register( pLuaState, "AttackNMY", AttackNMY);
Rip7
Rip7
The title of the topic is 'Returning function from Lua'. So I'm trying to pass a function from LUA to C++.
streamer
streamer
Then check my edited post [smile]
streamer
streamer
a little explanation:

pLuaState = lua_open();
luaopen_base(pLuaState); /* opens the basic library */
luaopen_table(pLuaState); /* opens the table library */
luaopen_io(pLuaState); /* opens the I/O library */
luaopen_string(pLuaState); /* opens the string lib. */
luaopen_math(pLuaState); /* opens the math lib. */
with:
lua_register( pLuaState, "AttackNMY", AttackNMY);

you register a Lua function called AttackNMY and bind it with AttackNMY c++ function ("AttackNMY" LUA, AttackNMY c++).

So when you execute LUA script:

lua_dofile( pLuaState,"myscript.lua");

when LUA finds AttackNMY function in script, LUA calls binded C++ function, in this case function with same name AttacNMY()

int AttackNMY(lua_State *pLuaState )
{
const int s = (int)lua_tonumber(pLuaState,1);

// s is argument of AttackNMY in LUA

return 1;
}

you can get any number of parameters with
const int s = (int)lua_tonumber(pLuaState,1); // first parameter number
const char *g = lua_tostring(pLuaState, 2);// second param string

returng values to LUA are
lua_pushstring(pLuaState, "yeah");
lua_pushboolean(pLuaState, (int)false);
lua_pushnumber( pLuaState, 1 );

you can push back multiple results

int AttackNMY(lua_State *pLuaState )
{
const int s = (int)lua_tonumber(pLuaState,1);

// s is argument of AttackNMY in LUA
lua_pushboolean(pLuaState, (int)true); // attack someone
lua_pushnumber( pLuaState, 1 ); // attack enemy 1
return 1;
}

I hope this clears some thing to you.
Rip7
Rip7
That was a very interesting explanation about how to use LUA in C(++). But what I really need is a Luabind implementation.
And the problem isn't solved either because in fact I want that Lua returns a set of Lua functions and save that in a luabind object and later use the functions in that object with my C++ program (see my initial post and the luabind docs).

Nevertheless thanks for your help.
streamer
streamer
Sorry, but I'm not familiar with luabind. I use plain LUA with c++.
[sad]
kUfa
kUfa
If you want to call a lua function from C, use lua_call:
http://www.lua.org/manual/5.1/manual.html#lua_call
Call your SetPropertyCommand with lua_call, and then you ll get your functions table.
I never used luabind to bind lua classes to C++ (even though the main page says that it is possible), but anyway i dont use luabind anymore, cant help you with that. But lua_call is probably what you are looking for.

/kUfa

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.