Page 1 of 1

Passing full commands to Lua from C++

Posted: Tue Mar 27, 2012 10:45 am
by Moosader
Hey guys--

I'm working on a game in C++ that is using Lua for it's scripting language, and I'm using Luabind.

I was just wondering, if there's a command in Lua or Luabind where I could essentially just pass Lua a string of anything and have it return a value. I don't want to have to specify in C++ what the parameters or how many of them there are, I just want to pass an entire string and have Lua handle it.

For example, I might want to output a table's value
print( character["Bob"]["name"] )
Or call an existing Lua function
room_GetNeighbors()

etc. etc., for debugging purposes.

Thanks for any help.
-Rach

Re: Passing full commands to Lua from C++

Posted: Tue Mar 27, 2012 1:20 pm
by TheBuzzSaw
Here is what I use to execute arbitrary Lua scripts on the fly.

Code: Select all

const char* source = "println(\"Hallo!\")";
int status = luaL_loadstring(luaState, source);
if (status)
    reportErrors();
else
    status = lua_pcall(luaState, 0, LUA_MULTRET, 0);

Re: Passing full commands to Lua from C++

Posted: Sat Mar 31, 2012 3:00 pm
by THe Floating Brain
I know Luabind has table's which you could probably do that with.
As far as sending a string to Lua I think (although I am not entirely sure, I haven't done Luabind for a few months)
STL types come per-wrapped, you could probably set a std::string as a global in Lua by doing the following...

C++:

Code: Select all

//Assume you have set up your Lua state/interpreter.//
std::string greeting = "Hello!";
luabind::globals( luaState ) [ "greeting" ] = &greeting;
Lua:

Code: Select all

print( greeting )
--Output: Hello!
I hope I answered your question.

- TFB