Alberto Posted February 8, 2010 Share Posted February 8, 2010 Hello Is it possible to expose C++ classes to Lua? I ask this question because I have a lot of C++ A.I. classes , from AI books Quote Link to comment Share on other sites More sharing options...
Rick Posted February 8, 2010 Share Posted February 8, 2010 Yes it is, however it's not that straight forward. There are tools that can help though. If you google you'll get a ton of hits. Also note though, that it seems there is a problem with exposing anything from C++ to LUA if you are using LE and linking in pure lua. Josh has written his own lua functions that share the same name as the pure lua functions so if you link pure lua into your program they'll collide and won't compile. So you either have to complain to Josh and hope he changes it or you'd have to wrap lua in a namespace (since it's open source you can) but lua is a pretty big library and that can take time. Or maybe you can think of another way. I'm all ears on that. Quote Link to comment Share on other sites More sharing options...
Josh Posted February 8, 2010 Share Posted February 8, 2010 The entire Lua API is not exposed in the DLL yet. Eventually it will be. I have just wrapped one function at a time as they are needed. You can retrieve the engine's global lua state variable. I am not sure if a separate instance of Lua you initialize yourself will work with that Lua state, but it's worth trying. If not, the entire Lua API will eventually be exposed.. 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...
Rick Posted February 8, 2010 Share Posted February 8, 2010 Are you wrapping lua functions because of the BMax users? If so, could you by chance make the wrapper methods a slightly different name? Something that let's us know they are LE wrapped methods? For us C++ programmers there really is no need to wrap the lua methods and if they didn't have the same names we could just import lua and use the lua functions ourselves. Please don't put this restriction on the C++ programmers. It's just an easy thing to rename the lua methods you are wrapping to something else. Since they have the same name the compiler gets screwed up. I am not sure if a separate instance of Lua you initialize yourself will work with that Lua state, but it's worth trying. There is no need for us to initialize lua in our C++ program if we use the framewerks because you already do that. At that point the lua_State* is all that matters, and since you expose that we could (if the function names were different) use normal lua commands. Quote Link to comment Share on other sites More sharing options...
Canardia Posted February 8, 2010 Share Posted February 8, 2010 Josh, could we rename the lua_xxx commands in the C API to Lua_SetGlobal, etc... so that all names start with capital letters? Then people can use the original Lua headers with C++ and other languages with LE. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Josh Posted February 8, 2010 Share Posted February 8, 2010 There is no need for us to initialize lua in our C++ program if we use the framewerks because you already do that. At that point the lua_State* is all that matters, and since you expose that we could (if the function names were different) use normal lua commands. In theory. The C++ instance of the Lua library might have some memory allocation it uses that is separate from the engine's instance of Lua, so I am not sure it would work. If the end user can just use the engine's Lua state with their own initialization of Lua, that would certainly be the most straightforward approach. 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...
Rick Posted February 8, 2010 Share Posted February 8, 2010 In theory. The C++ instance of the Lua library might have some memory allocation it uses that is separate from the engine's instance of Lua, so I am not sure it would work. I'd be more than happy to test it, but I need the LE lua function names to be different than the lua library function names to test it. I'd really prefer to not wrap lua in a namespace because it's a pretty big library and that would be a huge pain. If the end user can just use the engine's Lua state with their own initialization of Lua, that would certainly be the most straightforward approach. I agree 100%. Please rename those lua functions you have wrapped and I'll test it out. I currently see no reason this wouldn't work. Well, I guess the only reason might be you expose it as BP instead of the lua state type, but a conversion to the lua state type should work. Quote Link to comment Share on other sites More sharing options...
Josh Posted February 8, 2010 Share Posted February 8, 2010 Just comment them out of the header and try it. 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...
Rick Posted February 8, 2010 Share Posted February 8, 2010 OK, will do when I get home. I already have a test example setup for exposing a C++ function to lua which works with just C++ and lua, but doesn't with LE and lua. Will post here with the results. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 8, 2010 Share Posted February 8, 2010 Yes, this works. If I comment out the lua methods LE has in engine.cpp and engine.h and then use the lua library in my project I was able to expose my C++ method to lua and have it call. The C++ code static int KattSendMessage(lua_State* L) { char* _msg = (char*)lua_tostring(L, 1); char* _extra = (char*)lua_tostring(L, 2); string msg = _msg; string extra = _extra; LuaMessageManager::Instance().ProcessMessage(msg, extra); return 1; } BP L = GetLuaState(); lua_register((lua_State*)L, "KattSendMessage", KattSendMessage); In object of my lua models I placed inside the object:Update() method: KattSendMessage("rick", "test") I placed a breakpoint in the C++ function and it did indeed get called with "rick" & "test". I'd say this will work like a charm. Josh if you could rename the LE lua function it would make life easier as we wouldn't have to change them ourselves and always maintain a separate copy of the headers when new versions come out. [EDIT] The thing to be careful about with exposing your own classes/functions to lua is the editor won't recognize them and give a nil value, which kind of sucks. Probably have to wrap them in a nil value check. Quote Link to comment Share on other sites More sharing options...
Canardia Posted February 9, 2010 Share Posted February 9, 2010 I think Josh was waiting for this proof that it works before he can agree that the lua_xxx functions in LE should be renamed capitals per word, like all LE functions are anyways. I had the idea of removing the underscore too, and making them LE conform: SetLuaGlobal, PushLuaObject, etc... Great job Rick to get it to work! Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Rick Posted February 9, 2010 Share Posted February 9, 2010 If I wanted to test thing out myself how would I do it? If I rename these methods in engine.cpp and engine.h to have the l in lua be L, and I try to compile it complains: 1>c:\program files\leadwerks engine sdk\cpp\engine.cpp(2714) : error C3861: 'lelua_gettop': identifier not found 1>c:\program files\leadwerks engine sdk\cpp\engine.cpp(2719) : error C3861: 'lelua_pushobject': identifier not found 1>c:\program files\leadwerks engine sdk\cpp\engine.cpp(2724) : error C3861: 'lelua_pop': identifier not found 1>c:\program files\leadwerks engine sdk\cpp\engine.cpp(2729) : error C3861: 'lelua_setglobal': identifier not found From what I can see these lelua_ methods should be coming from the dll? When I look at an example like leCreateGraphics(), the definition or declaration isn't found anywhere that I can see. [EDIT] Nevermind, I see. Quote Link to comment Share on other sites More sharing options...
Josh Posted February 9, 2010 Share Posted February 9, 2010 If this does indeed work, the Lua commands don't need to be declared at all in the header. 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...
Rick Posted February 9, 2010 Share Posted February 9, 2010 I also got it working with a rename of the le lua files. Quote Link to comment Share on other sites More sharing options...
Canardia Posted February 9, 2010 Share Posted February 9, 2010 If this does indeed work, the Lua commands don't need to be declared at all in the header. I would keep the basic Lua commands which allow setting of the Framework variable in the headers, because not everyone needs Lua in their project. And of course rename them so they don't conflict with the C++ Lua headers. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Rick Posted February 9, 2010 Share Posted February 9, 2010 I agree with Lumooja. To remove the dependency of some people who just want this basic functionality it would be better that they didn't have to bring lua into their project. Just rename the lua functions you created and problem solved. Quote 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.