🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Easing the pain of Lua - C++ integration

Started by
7 comments, last by PaulCunningham 20 years, 7 months ago
Hi all. I''m using Lua as my scripting solution for a (hobby) game I''m developing. I''m interested in automating the export of my C++ classes to Lua as much as possible. Currently I''m using Luna but this requires me to create a proxy class for every real class that I want to export and this is a maintenance nightmare! I''ve looked at toLua (specifically toLua++) and it looks like the toLua compiler would automate the manual creation of the proxy classes. However, I need a little clarification on a few things and advice as to whether my proposed solutions / thoughts are viable: #1 I don''t like the idea of having a seperate ''cleaned'' header file as I''m thinking this could become a maintenace issue and lead to possible mismatches between the two header files. Therefore I''m thinking of using the toLua comment tags (//tolua_begin and //tolua_end) in the real header. #2 I''m a little confused as to how to structure the project. Do I have multiple toLua generated source files that I include in my project: 1 per class and therefore many tolua_''pkgname''_open statements: or a single file that contains everything to be exported. How have others structured their projects? #3 Ideally when I change a header file that has //tolua_* tags, I''d like the corresponding proxy source files to be re-generated, either immediately or on a recompile / build. I''m using Visual Studio 2003 so I''m thinking that there may be some build events I can hook into to do this? It doesn''t have to be toLua either - if anybody has successfully implemented a different approach, I''d happily look at those as well. Cheers, Paul Cunningham
Cheers,Paul CunninghamPumpkin Games
Advertisement
Got the Custom Build Event sorted. Works great.

Just need the other questions answering now

Cheers,
Paul Cunningham
Cheers,Paul CunninghamPumpkin Games
take a look at http://luabind.sourceforge.net/

It allows u to intergrate C++ and lua programs at your programs source level, using templates and metaprogramming techniques.

-ddn
In the last project I woked on we used LUA. We builded up a completely custom system. The wrapper template was based on the same concept of "luna"(you''ll find it in the application notes area on lua.org) and we had a set of objects to manipulate lua tables from C++. Personally I do not like tolua it generates way to much code and even more important is that usually I do not want 1:1 mapping on all C++ structures in script, but a simplified higher lavel API. We had a handwritten set of wrapper functions for each object we wanted to expose It worked more than fine(we had a lot of script).

Recently, because our current engine is designed to be used from high level languages(very high level interfaces: has a restricted set of types and a common base interface) I found a very nice system to automatically generate custom wrappers.
Assuming that you are using VC++;
We wrote an ADD-in that exploit the VC++ IDE parsing capabilities. VC++ has all your classes already parsed because of the class view, so we just use the class view tree for generate a wrapper on demand. We are not using LUA but it fits any language and there is no need for simplified classes etc because the VC++ parser is a complete parser and also undersatnd enums and any wierd C++ construct/syntax.


I hope this helps.
Alberto


-----------------------------
The programming language Squirrel
http://squirrel.sourceforge.net
-----------------------------The programming language Squirrelhttp://www.squirrel-lang.org
quote: Original post by fagiano
We wrote an ADD-in that exploit the VC++ IDE parsing capabilities. VC++ has all your classes already parsed because of the class view, so we just use the class view tree for generate a wrapper on demand. We are not using LUA but it fits any language and there is no need for simplified classes etc because the VC++ parser is a complete parser and also undersatnd enums and any wierd C++ construct/syntax.


Hi fagiano,
This sounds interesting - is this Add-in publicly available? If so, where can we get it?


Dave Dak Lozar Loeser
The WM_NULL message performs no operation. An application sends the WM_NULL message if it wants to post a message that the recipient window will ignore. - MSDN
Dave Dak Lozar Loeser
"Software Engineering is a race between the programmers, trying to make bigger and better fool-proof software, and the universe trying to make bigger fools. So far the Universe in winning."--anonymous
We use the toLua method, and we mark up the headers. By default, everything is parsed; if something is not understood by toLua, we tell it to skip some number of lines:

// toLua: skip 3

We also use something similar to mark header files for processing at all; totally un-marked headers are not parsed/included.

Actually, once we were done with it, it''s not toLua anymore. Among other things, we wrote a generic value input/output class which can type coerce, look up table values, manage object and string lifetimes, etc. I think we ended up re-writing everything in perl in the end -- if not, then at least it feels that way :-)

We process each library (subdirectory) in the project into one lua-bindings file, which contains all of the headers/interfaces for that library.
enum Bool { True, False, FileNotFound };
quote: Original post by Dak Lozar
Hi fagiano,
This sounds interesting - is this Add-in publicly available? If so, where can we get it?


No is not freely available, but my collegue took a day to have it up and running. Is very easy. Just create a C# Add-in with the Add-in wizard and look in MSDN for Visual C++ Code Model; You get an object call CodeBase that contain a tree similar to an XML dom is really a piece of cake.BTW I''m talking about Visual Studio .NET 2003.

ciao
Alberto

-----------------------------
The programming language Squirrel
http://squirrel.sourceforge.net
-----------------------------The programming language Squirrelhttp://www.squirrel-lang.org
Cheers AP - I''ll look at luabind as well.

Just an update really.

I''ve created a custom project item type called "toLua++ component" that I can add to my projects. It uses a simple wizard (.vsz) to add files (.h, .cpp and proxy stub .cpp) from a template description to the project. Just need to hook up with the custom build event I''ve created and the process will be fully automated. There''s a bit of JScript called SetFileProperties that gets called by the IDE that looks like it''d be what I need.

fagiano: toLua++ does generate a lot of code but it is generally code you need. I can use the comment tags to dictate which functions of the class I want to export. Maybe this will become a bit of a chore - The alternative is to code up the exported functions manually or do as you did and develop a custom solution which I don''t have either the time or inclination to do.

Thanks for the ideas though.

Cheers,
Paul Cuningham
Cheers,Paul CunninghamPumpkin Games
Hooked up the Custom Build event tool the the newly generated .h file. Works a treat!

With this structure in place everything is automated and I think it answers my other points as well!

Cheers,
Paul Cunningham
Cheers,Paul CunninghamPumpkin Games

This topic is closed to new replies.

Advertisement