   New to Lua/Embedding
  Hello,  I've been learning a bit about the lua and python scripting languages and how to embed them into your C++ application. Now learning about the languages themselves have been somewhat smooth.  But embbedding them seems to be near impossible and is here where I need some help.  I first gave boost python a try but I gave up after not being able to successfully complete the first hello world tutorial for a day.   I am now giving lua a try and have first attempted to use tolua++ for the lua/c++ wrapper.  However this is giving me much not welcomed memories from boost python in that, there is really no comprehensible tutorial, at least from what I been able to pull up. The installation snippit here http://www.codenix.com/~tolua/ is almost a joke, but I would like it to make sence of course.  So my question is that is there a comprehensible tutorial on how to embed lua into your c++ programs using visual studio 2010?  I see that these wrappers create certain files that lau can use to call your c++ code from.  But can the c++ code get the data from lau as well by the same means?I appreciate any advice,Thanks.   
  I would recommend you use an earlier version of visual studio if possible. I am also working on project which involves both lua and python.I embedded lua using luabind: http://www.nuclex.org/articles/cxx/1-quick-introduction-to-luabindThis site has a good explanation and pre-build libraries for VS2008. If you would like to use VS10, then you will have to build luabind and boost yourself. But from my experience luabind is very powerful and fairly easy to use so give it a try.Python is pretty straight forward to use on its own. As long as you make sure you reference count properly, you don't need anything else (especially for embedding)here are a few links I used:http://www.codeproject.com/KB/cpp/embedpython_1.aspxhttp://www.linuxjournal.com/article/8497;  Have you read the documentation for the Lua C API? It's actually pretty easy to use by itself. It just takes some boilerplate code to catch errors and convert types. Here's a good starting point: Overview of Lua's C API. The next page gets right into some sample code. It's pretty easy to follow. It's a good idea to at least dabble in it, to have an idea of what the binding libraries are doing underneath.Then the reference: C API Manual.;  Yes, that is true. LuaC is actually pretty straightforward. But everything is done through the lua stack which you will have to manipulate directly. It's actually quite easy again but like the post above said you will need some boiler-plate code and have to create a few wrappers. Luabind is a quick solution, so go with that if you are short on time.   ;  Thanks guys, that helps me out alot.Ill give Lua's C API a try.  Will it work with c++? EDIT: I do see that you can use it with c++ code now. I am quite short on time actually, and I tried luabind but it still is too complicated for me, it seems that none of the tutorials assume nothing.  Such as herehttp://www.nuclex.org/articles/cxx/1-quick-introduction-to-luabind The writer makes an attempt on making it simple and to some degree he achieves that, when trying to build luabind with his demo.  But he doesent mention however ( at least that I could see ), how to load 'what' lua script  As for the luabind documentation, it fails to make certain things clear to me such as installation ( which I did everything as it told, but it claimed that it couldent find my lua include directory ) and in its hello world example it doesent explain at all how it got hello_world.dll in the lua code, do you generate that? It never expained : ( I learned a bit about python but am just beginning lua, however do you think I should stick with python and use there Python/C API?  For a game engine, is python alot harder to use then lua when trying to embed them?Everything helps, if there any other advice keep it coming : )[Edited by - MichaelWojcik on May 9, 2010 6:58:20 PM];  Your best reference is going to be the Lua 5.1 Reference Manual as theOcelot mentioned. I often use this site, Luai: Lua reference manual helper: all Lua bits  as well because it's very simple and easy to find quick reference material.You can start with this example, Simple Cpp Binding, as a very bare minimal way of exposing C++ to Lua. It doesn't show that much, but it's more just a simple example to get you started and familiar with a few of the mechanics involved.Once you have a simple grasp on those concepts, making sure to reference the Lua API docs as needed, this article, Wrapping C++ classes in Lua goes over some more things you need to consider as you develop your bindings as part of the embedding process. That code posted there is not complete and the author leaves some stuff out, but it's another resource to help you further understand the process.After you have gone through those resources and have learned a bit, this example should make sense to you. It's something I've been working on to get a better idea of manual Lua embedding processes rather than resorting to using a Lua wrapping library (I can afford doing it by hand, educational purposes really). It goes over a lot, but not all of the different aspects of exposing data to Lua and exposing Lua to C++. It's not complete and might have bugs since it's a work in progress, but it help you further your understanding of the embedding process.Lua 5.1.4 Exampleextern "C" {#include "lua.h"#include "lualib.h"#include "lauxlib.h"}#include <stdlib.h>#include <string>#include <set>// See the function: MyClass_GCclass MyClass;std::set<MyClass *> MyClass_stack;// Example classclass MyClass{private:int tmp;public:MyClass() : tmp(0){printf("---] %s (default ctor) [---\n\n", __FUNCTION__);}~MyClass(){printf("---] %s [---\n\n", __FUNCTION__);}MyClass(const MyClass & rhs){printf("---] %s (copy ctor) [---\n\n", __FUNCTION__);tmp = rhs.tmp;}MyClass & operator =(const MyClass & rhs){printf("---] %s [---\n\n", __FUNCTION__);if(this == &rhs)return *this;tmp = rhs.tmp;return *this;}// Setter function that will be exposed to Lua.void SetTmp(int val){printf("---] %s [---\n\n", __FUNCTION__);tmp = val;}// Getter function that will be exposed to Lua.int GetTmp() const{printf("---] %s [---\n\n", __FUNCTION__);return tmp;}// This is a class function that is not directly exposed to Lua, but // will be called through other functions exposed to Lua.void PushTheRedButton(){printf("---] %s [---\n\n", __FUNCTION__);}// Alternatively, rather than expose each function directly, you can// setup an Invoke system that calls the functions from hard coded// logic. This could be a global function as well registered to the // namespace of the class.int Invoke(lua_State * L, std::string func){if(func == "PushTheRedButton"){PushTheRedButton();}else if(func == "GetTmp"){lua_pushinteger(L, GetTmp());return 1;}else if(func == "SetTmp"){SetTmp(luaL_checkinteger(L, 1));}else{char err[256];_snprintf_s(err, 255, "Function \"%s\" does not exist in the MyClass class.\n", func.c_str());lua_pushstring(L, err);lua_error(L);}return 0;}};// To keep consistency easier, in case we want to renameconst char * LUA_NAME_MYCLASS = "namespace.MyClass";// We want to write our own little function that emulates how a call like// luaL_checkinteger or luaL_checkstring would work for example.MyClass * luaL_checkMyClass(lua_State* L, int index);// Just a function that pulls data from / calls a function the Lua statevoid Global_TestMyClassFunc(lua_State* L, MyClass * ptrMyClass){printf("---] %s [---\n\n", __FUNCTION__);// Get a function and pass data to itlua_getglobal(L, "g");if(lua_isfunction(L, lua_gettop(L))){lua_pushstring(L, "Testing 1, 2, 3");if(lua_pcall(L, 1, 0, 0)){printf("%s\n", lua_tostring(L, -1));lua_pop(L, 1);}}ptrMyClass->PushTheRedButton();// Get a function and retrieve data from itlua_getglobal(L, "GetTraceBack");if(lua_isfunction(L, lua_gettop(L))){if(lua_pcall(L, 0, 1, 0)){printf("%s\n", lua_tostring(L, -1));lua_pop(L, 1);}if(lua_isstring(L, -1)){printf("\n\n%s\n\n", lua_tostring(L, -1));}else{lua_pushstring(L, "GetTraceBack did not return a string.");lua_error(L);}}}// Lua "glue" function for exposing a global function to Luaint Lua_TestMyClassFunc(lua_State* L){printf("---] %s [---\n\n", __FUNCTION__);Global_TestMyClassFunc(L, luaL_checkMyClass(L, 1));return 0;}// We want to write our own little function that emulates how a call like// luaL_checkinteger or luaL_checkstring would work.MyClass * luaL_checkMyClass(lua_State* L, int index){int top = 0;void * ud = 0;luaL_checktype(L, index, LUA_TTABLE);lua_getfield(L, index, "__self");top = lua_gettop(L);ud = luaL_checkudata(L, top, LUA_NAME_MYCLASS);lua_pop(L, 1);return *((MyClass**)ud);}// Exposes an existing MyClass object to Luavoid MyClass_expose(lua_State * L, MyClass * ptrMyClass, const char * globalname){printf("---] %s [---\n\n", __FUNCTION__);lua_newtable(L); // variablelua_newtable(L); // instancelua_pushvalue(L, 1);lua_setmetatable(L, -2);lua_pushvalue(L, 1);lua_setfield(L, 1, "__index");MyClass ** s = (MyClass **)lua_newuserdata(L, sizeof(MyClass *));*s = ptrMyClass;luaL_getmetatable(L, LUA_NAME_MYCLASS);lua_setmetatable(L, -2);lua_setfield(L, -2, "__self");lua_setglobal(L, globalname);}// Allocates a new MyClass object and sets up a Lua object for it.int MyClass_new(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);luaL_checktype(L, 1, LUA_TTABLE);lua_newtable(L);lua_pushvalue(L, 1);lua_setmetatable(L, -2);lua_pushvalue(L, 1);lua_setfield(L, 1, "__index");MyClass ** s = (MyClass **)lua_newuserdata(L, sizeof(MyClass *));*s = new MyClass();luaL_getmetatable(L, LUA_NAME_MYCLASS);lua_setmetatable(L, -2);lua_setfield(L, -2, "__self");return 1; }// Allocates a new MyClass object from an existing MyClass object// and sets up a Lua object for it.int MyClass_clone(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);MyClass * ptrMyClass = luaL_checkMyClass(L, 2);luaL_checktype(L, 1, LUA_TTABLE);lua_newtable(L);lua_pushvalue(L, 1);lua_setmetatable(L, -2);lua_pushvalue(L, 1);lua_setfield(L, 1, "__index");MyClass ** s = (MyClass **)lua_newuserdata(L, sizeof(MyClass *));*s = new MyClass(*ptrMyClass);luaL_getmetatable(L, LUA_NAME_MYCLASS);lua_setmetatable(L, -2);lua_setfield(L, -2, "__self");return 1; }// Lua "glue" function for a class member function.int MyClass_SetTmp(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);int argc = lua_gettop(L);if(argc != 2){// Don't say 2 because Lua passes object as 1st arg, function needs 1 onlylua_pushstring(L, "Expected 1 argument to MyClass::SetTmp");lua_error(L);}MyClass * ptrMyClass = luaL_checkMyClass(L, 1);ptrMyClass->SetTmp(luaL_checkinteger(L, 2));return 0;}// Lua "glue" function for a class member function.int MyClass_GetTmp(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);int argc = lua_gettop(L);if(argc != 1){// Don't say 2 because Lua passes object as 1st arg, function needs 1 onlylua_pushstring(L, "Expected no arguments to MyClass::GetTmp");lua_error(L);}MyClass * ptrMyClass = luaL_checkMyClass(L, 1);lua_pushinteger(L, ptrMyClass->GetTmp());return 1;}// Lua "glue" function so we can emulate an "invoke" style API// rather than having to expose each function individually.int MyClass_invoke(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);MyClass * ptrMyClass = luaL_checkMyClass(L, 1);std::string name = luaL_checkstring(L, 2);// Offset arguments by 2, since the table is 1st // and the func name is 2ndlua_remove(L, 1);lua_remove(L, 1);return ptrMyClass->Invoke(L, name);}// Functions that are going to be registeredconst luaL_Reg MyClass_Registry[] ={{"new", MyClass_new},{"clone", MyClass_clone},{"invoke", MyClass_invoke},{"SetTmp", MyClass_SetTmp},{"GetTmp", MyClass_GetTmp},{NULL, NULL},};// Garbage Collection function for objects of MyClass type.// This approach might not be optimal for a lot of allocations,// but shows how you can accommodates for stack/heap objects// if your design needs it so you don't have to modify the design// of class objects. Not thread safe, so keep that in mind.int MyClass_GC(lua_State * L){printf("---] %s [---\n\n", __FUNCTION__);MyClass * c = *(MyClass**)lua_touserdata(L, 1);std::set<MyClass *>::iterator itr = MyClass_stack.find(c);if(itr != MyClass_stack.end()){MyClass_stack.erase(itr);printf("*** Not deleting (%x) because it was allocated on the stack!\n", c);}else{delete c;printf("*** Deleting (%x) because it was allocated on the heap!\n", c);}return 0;}// Register the MyClass object with Luavoid registerMyClass(lua_State *L){  printf("---] %s [---\n\n", __FUNCTION__);luaL_newmetatable(L, LUA_NAME_MYCLASS);lua_pushstring(L, "__gc");lua_pushcfunction(L, MyClass_GC);lua_settable(L, -3);luaL_register(L, 0, MyClass_Registry);lua_pushvalue(L, -1);lua_setfield(L, -2, "__index");luaL_register(L, LUA_NAME_MYCLASS, MyClass_Registry);}// Global functions to expose to Luastatic const luaL_reg Global[] ={{"TestMyClassFunc", Lua_TestMyClassFunc},{NULL, NULL},};int main(int argc, char * argv[]){printf("---] %s [---\n\n", __FUNCTION__);lua_State * L = 0;L = lua_open();luaL_openlibs(L);registerMyClass(L);luaL_openlib(L, "Global", Global, 0);// Easily register some constants#define r(name) { lua_pushinteger(L, name); lua_setglobal(L, #name ); }r(EXIT_SUCCESS)r(EXIT_FAILURE)#undef r// This set of code shows registering a short lived object with Lua// and then removing it. Our expose function clears the __gc field// so we don't have a double deallocation and instead let memory// be managed by the C++ side.{MyClass myc;// Manually track objects on the stackMyClass_stack.insert(&myc);luaL_dostring(L, "print(\"tmpMyClass\", tmpMyClass)");MyClass_expose(L, &myc, "tmpMyClass");luaL_dostring(L, "print(\"tmpMyClass\", tmpMyClass)");luaL_dostring(L, "tmpMyClass:SetTmp(404)");luaL_dostring(L, "print(tmpMyClass:GetTmp())");lua_pushnil(L);lua_setglobal(L, "tmpMyClass");luaL_dostring(L, "collectgarbage(\"collect\")");}{MyClass * myc2 = new MyClass;luaL_dostring(L, "print(\"tmpMyClass22\", tmpMyClass2)");MyClass_expose(L, myc2, "tmpMyClass2");luaL_dostring(L, "print(\"tmpMyClass2\", tmpMyClass2)");luaL_dostring(L, "tmpMyClass2:SetTmp(808)");luaL_dostring(L, "print(tmpMyClass2:GetTmp())");lua_pushnil(L);lua_setglobal(L, "tmpMyClass2");luaL_dostring(L, "collectgarbage(\"collect\")");// Contents of 'myc2' are now invalid, you can create dangling// pointers if you are not careful here!}luaL_dostring(L, "print(\"tmpMyClass\", tmpMyClass)");// A Lua function we want to add to the state firstif(luaL_dostring(L, "function GetTraceBack() return tostring(debug.traceback()); end")){printf("%s\n", lua_tostring(L, -1));lua_pop(L, 1);}// Test out the constantsif(luaL_dostring(L, "function g(text)""\n""print(\"function g(\\\"\"..text..\"\\\")\")""\n""print(\"EXIT_SUCCESS = \" .. EXIT_SUCCESS)""\n""print(\"EXIT_FAILURE = \" .. EXIT_FAILURE)""\n""end""\n")){printf("%s\n", lua_tostring(L, -1));lua_pop(L, 1);}// Test out using the MyClass objectif(luaL_dostring(L, "function f()""\n""local var = namespace.MyClass:new()""\n""var:SetTmp(42)""\n""print(\"var:GetTmp() -> \" .. var:GetTmp())""\n""Global.TestMyClassFunc(var)""\n""local var2 = namespace.MyClass:clone(var)""\n""var2:SetTmp(90)""\n""print(\"var:GetTmp() -> \" .. var:GetTmp())""\n""print(\"var2:GetTmp() -> \" .. var2:GetTmp())""\n""var2:invoke(\"SetTmp\", 777)""\n""print(\"var2:Invoke(\\\"GetTmp\\\") -> \" .. var2:invoke(\"GetTmp\"))""\n""var2:invoke(\"PushTheRedButton\")""\n""var2[\"userdata\"]=123""\n""print(\"var2[\\\"userdata\\\"] = \" .. tostring(var2[\"userdata\"]))""\n""print(\"var[\\\"userdata\\\"] = \" .. tostring(var[\"userdata\"]))""\n"//"var2:invoke(\"ThisDoesNotExit\")""\n"// This is as close to manual deletion as you can get.//"var = nil""\n"//"collectgarbage(\"collect\")""\n""end""\n""\n""f()""\n""\n""print(\"Hello World\")""\n")){printf("%s\n", lua_tostring(L, -1));lua_pop(L, 1);}lua_close(L);return 0;}And an example output on my PC---] main [------] registerMyClass [------] MyClass::MyClass (default ctor) [---tmpMyClass nil---] MyClass_expose [---tmpMyClass table: 004A2248---] MyClass_SetTmp [------] MyClass::SetTmp [------] MyClass_GetTmp [------] MyClass::GetTmp [---404---] MyClass_GC [---*** Not deleting (12ff48) because it was allocated on the stack!---] MyClass::~MyClass [------] MyClass::MyClass (default ctor) [---tmpMyClass22    nil---] MyClass_expose [---tmpMyClass2table: 004A2230---] MyClass_SetTmp [------] MyClass::SetTmp [------] MyClass_GetTmp [------] MyClass::GetTmp [---808---] MyClass_GC [------] MyClass::~MyClass [---*** Deleting (34b648) because it was allocated on the heap!tmpMyClass nil---] MyClass_new [------] MyClass::MyClass (default ctor) [------] MyClass_SetTmp [------] MyClass::SetTmp [------] MyClass_GetTmp [------] MyClass::GetTmp [---var:GetTmp() -> 42---] Lua_TestMyClassFunc [------] Global_TestMyClassFunc [---function g("Testing 1, 2, 3")EXIT_SUCCESS = 0EXIT_FAILURE = 1---] MyClass::PushTheRedButton [---stack traceback:   [string "function GetTraceBack() return tostring(deb..."]:1: in function <[string "function GetTraceBack() return tostring(deb..."]:1>   [C]: in function 'TestMyClassFunc'   [string "function f()..."]:5: in function 'f'   [string "function f()..."]:18: in main chunk---] MyClass_clone [------] MyClass::MyClass (copy ctor) [------] MyClass_SetTmp [------] MyClass::SetTmp [------] MyClass_GetTmp [------] MyClass::GetTmp [---var:GetTmp() -> 42---] MyClass_GetTmp [------] MyClass::GetTmp [---var2:GetTmp() -> 90---] MyClass_invoke [------] MyClass::SetTmp [------] MyClass_invoke [------] MyClass::GetTmp [---var2:Invoke("GetTmp") -> 777---] MyClass_invoke [------] MyClass::PushTheRedButton [---var2["userdata"] = 123var["userdata"] = nilHello World---] MyClass_GC [------] MyClass::~MyClass [---*** Deleting (4a50f0) because it was allocated on the heap!---] MyClass_GC [------] MyClass::~MyClass [---*** Deleting (4a22a8) because it was allocated on the heap!Press any key to continue . . .The important thing to note is that there may be other ways of doing things shown in that code, so don't take anything as a definitive approach. Consult the Lua docs for the final say on different aspects of using the Lua API.One of the concepts it does not show is how to work with Callback functions from Lua or C++. That's a more advanced topic that your implementation strategy will depend on your code design. If you need something like that right away, have a look at this: how to callback a lua function from a c function.Once you are more comfortable with the embedding process manually, then you can relook at the automated libraries. Those save a lot of time and work, since you really don't want to have to code every Lua interface for every object by hand. You'll see a lot of the code can be reused, and only types change, so that's how some of the binding libraries work. Others involve secondary forms of processing to automate code generation. You could even write your own if you wanted to as well.Anyways, hope that helps. Good luck!   ;  Thanks for the help These are unfortuantly still very confusing for me.  Speaking about the information I've experienced so far, I see lots of differant "snapshots" of information but I wouldn't know how to put it all together and I dont know why there isn't really a consistant learning source source for that.  And I think that is becuase there seems to be no sources on how to go about embedding in a practical way, most of it seems like thoery and then only bits of code. And I dont mean to be rude, but of the intructions for the wrapper tools I've come across are poorly written and criptic.  I am glad that lua works well for others here, but I spent a month learning python and dont know much about lua so I think I may reconsider and use python instead, but ofcourse that will rely apon if its any more comprehensible although my familairty with python would be a plus. Im a bit worried though becuase I dont know how long it will take to successully get this embedding working and I am time pressed.  I believe this would be my best go in that case?http://docs.python.org/c-api/intro.html#embedding-python   ;  Python from what I've read isn't meant to be embedded but to extend or create applications from its standalone. That's the underlying philosophy of Python. Lua is really one of the easiest scripting language to embed. You just compile in the Lua src or use it as a dll, and a binding library (optional) and thats pretty much it. If your using Boost, that's a non-trivial library to integrate and you'll have to read up on it. I haven't used boost in years.Have u read this page :http://docs.python.org/extending/embedding.htmlfor Python? That seems pretty straight forward on how to embed Python. If you want to try Lua its best to take it in steps, first build a project with Lua integrated either as a DLL or static lib, then you can move from there. I don't suggest trying to write binding by hand until later, use the binding libraries first. I suggest using MLuabind or Luabind, both have a dependency on template libraries (Loki for MLuabind and Boost for Luabind). Choose whichever fits your need, Loki is a very light weight template library, while Boost is a gigantic template library.Good Luck!-ddn   ;  Thanks for the further advice.  Well after checking my options on Python, I may not give up just yet on lua. After reading this book, how well off do you guys think I'll be?http://www.amazon.com/exec/obidos/ASIN/8590379825/lua-home-20[Edited by - MichaelWojcik on May 15, 2010 12:18:50 AM];Quote:Original post by MichaelWojcikThanks for the further advice.  Well after checking my options on Python, I may not give up just yet on lua. After reading this book, how well off do you guys think I'll be?http://www.amazon.com/exec/obidos/ASIN/8590379825/lua-home-20You'll be pretty well off! [wink]The first version of that book is available for free online in an electronic  format: Programming in Lua (first edition). The second version adds information about 5.1 and buying it helps support Lua, so it's worth it if you plan on investing in Lua. I have both the Programming in Lua and Lua Programming Gems books and they are pretty useful.You will still want to consult the Lua 5.1 Reference Manual and any other online resources as well as you program though. The sections related to embedding aren't any different than what you can find online, so buying the books (Programming in Lua and the Reference Manual) is more for supporting Lua rather than getting access to information you can't find anywhere else. The source code of Lua is also another useful reference in really understanding how things work, but that's not really covered in depth anywhere that I know of.   