Skip to main content
GameDev.net gamedev.net
🔒 Locked

Python and Lua

Started by blanky Oct 17, 2005 at 5:33 PM 30 replies 12.8k views
Original Post
blanky
blanky
Okay guys, I just found out about lua and have managed to integrate it with C++. I love it for being simple yet flexible, and plan on using it. However, I discovered that lua doesn't natively support binding classes to it, of course there are ways and I've discovered a few, but this got me thinking. Is python easier to use with C++ than lua? I've seen example python/C++ code and it seems a lot more complicated (mostly initation), but once that's done, is it all good for python? I mean, cuase I know python has classes and what not, so I thought it'd be easier, thanks for the help. Also, forgot, it seems like lua is slimmer than python, lightweight, which is one of the reasons why I chose it, is python all that bigger? Thanks again.
SiCrane
SiCrane
Python isn't bad to embed if you use boost::python. If you use the native C API, it's obnoxious. Actually it's worse than obnoxious, but I lack the skills to state it more strongly. Embedding Python does have quite a big footprint, though you can reduce it somewhat by building a custom version of Python that omits modules that you don't plan to use.
Mite51
Mite51
Theres been a lot of posts comparing AngelScript and Lua. I've used lua on 2 projects and at first I liked it, but after digging in the guts to hook up extra features I found I really hated it. I belive the main gripe with python is the overhead, it has quite the memory footprint. Personally I plan to give AngelScript a chance on my next project, or failing that revive my own script engine I started.

Maybe it was just me, but I found luas internal code to be way to over engineered, to the point of being well intentioned spagetti code. Also stacks are used everywhere, which in some cases just made my brain hurt. Lua can be good IF you don't plan on extending it and you have non-programmers doing the script coding.
blanky
blanky
Do you recommend python over lua? This is just your opinion, of course.
SiCrane
SiCrane
I recommend trying both and seeing which you like better. I personally use Python and not Lua, but you may find Lua to be a better fit for you.
John Schultz
John Schultz
Quote:
Original post by Mite51
Theres been a lot of posts comparing AngelScript and Lua. I've used lua on 2 projects and at first I liked it, but after digging in the guts to hook up extra features I found I really hated it. I belive the main gripe with python is the overhead, it has quite the memory footprint. Personally I plan to give AngelScript a chance on my next project, or failing that revive my own script engine I started.

Maybe it was just me, but I found luas internal code to be way to over engineered, to the point of being well intentioned spagetti code. Also stacks are used everywhere, which in some cases just made my brain hurt. Lua can be good IF you don't plan on extending it and you have non-programmers doing the script coding.


I starting using Lua years ago (around 4.x), then switched to LuaPlus (with Lua 5.x). By the time I needed more complex scripting beyond config/setup, I had read about various updates/fixes developers had applied to Lua (including Joshua's ref-counting applied to Lua 5.x for LuaPlus). I also read about Alberto's issues with Lua and FarCry, and also learned about the scripting language developed after that project: Squirrel (with parts derived from Lua). While I liked the Squirrel language design and syntax better than Lua, I had two reservations:

1. No remote debugger (an excellent debugger exists for Lua/LuaPlus).
2. No easy/simple binding solution (LuaPlus and luabind are both good solutions for Lua).

After studying LuaPlus and luabind, I solved (2) with SqPlus. Alberto recently released a remote debugger for Squirrel, solving (1).

If you really like Lua, check out LuaPlus (relatively simple) and luabind (much more complicated, but also more powerful (especially when dealing with class instantiation)). For Python, boost::python is the way to go. If you are looking for something simpler than Lua (including the core compiler/interpreter), perhaps give Squirrel a try.
Sneftel
Sneftel
Quote:
Original post by Mite51
Theres been a lot of posts comparing AngelScript and Lua. I've used lua on 2 projects and at first I liked it, but after digging in the guts to hook up extra features I found I really hated it. I belive the main gripe with python is the overhead, it has quite the memory footprint. Personally I plan to give AngelScript a chance on my next project, or failing that revive my own script engine I started.

Maybe it was just me, but I found luas internal code to be way to over engineered, to the point of being well intentioned spagetti code. Also stacks are used everywhere, which in some cases just made my brain hurt. Lua can be good IF you don't plan on extending it and you have non-programmers doing the script coding.

I have to say, I'm surprised. Many people use Lua primarily because they find it extremely easy to modify. In fact, it's gotten so that feature suggestions on the lua-l list tend to be accompanied by a quick patch to the source code, merely as a proof-of-concept demonstration. Yeah, the bowels of the VM are spaghetti code.... but that's pretty common to high-performing VMs. Step out a couple of function-call levels, and it's nice and easy to play with.
blanky
blanky
Thanks guys, by the way, John Schultz, thanks for the links. I plan on using lua still, but which do you prefer, luabind or luaplus? Also, do these go side by side with the normal lua? or are these seperate and already include lua, thanks bud! Please continue this conversation, I assure it'll be helpful to people who have this question.
John Schultz
John Schultz
Quote:
Original post by blankdev
Thanks guys, by the way, John Schultz, thanks for the links. I plan on using lua still, but which do you prefer, luabind or luaplus? Also, do these go side by side with the normal lua? or are these seperate and already include lua, thanks bud! Please continue this conversation, I assure it'll be helpful to people who have this question.


It depends on what you need; for simple binding, LuaPlus is easier to work with as it is much simpler than luabind. If you don't mind using the boost headers and a more complex compile/setup, luabind is much more powerful (I like the class registration style).

LuaPlus comes with a modified Lua core (ref counted). However, you can use LuaPlusCD (call dispatcher) with an unmodified Lua core.
It appears luabind works with an unmodified Lua 5.x core (I read the docs/source examples only).
blanky
blanky
Thanks, for simple class registration, what do you think of luna? Here http://lua-users.org/wiki/CppObjectBinding . I just need simple function binding (which lua already handles) and C++ class binding, nothing fancy. Thanks.
John Schultz
John Schultz
Quote:
Original post by blankdev
Thanks, for simple class registration, what do you think of luna? Here http://lua-users.org/wiki/CppObjectBinding . I just need simple function binding (which lua already handles) and C++ class binding, nothing fancy. Thanks.


Given your intended use, I would try both luabind and luna, and pick the one you like the best.

A luna example link.
blanky
blanky
I've been looking for luaplus tutorials without success, can you help me out? Thanks bud, yeah I'll look.
John Schultz
John Schultz
Quote:
Original post by blankdev
I've been looking for luaplus tutorials without success, can you help me out? Thanks bud, yeah I'll look.


I'm not aware of any tutorials beyond the (many) examples included with the LuaPlus distribution (some examples are also posted on the LuaPlus forum).
sandelz
sandelz
If using SWIG or BOOST it'll be easier to bind c++ to python, but it'll leave you with many other problems like object ownership hassle with and huge files eves with tiny libraries (I couldn't reduce the filesize no matter what I did).

People always say it's horrible to bind python "manually", but I prefer it. It leaves you with more control over the things you do and it isn't horrible, just a little bit more work :)

And with python over lua? Well python offers a complete, developer ready language(many know python, but not lua). That's why I use python instead of lua.

edit:added things
--- sandelz
blanky
blanky
Thanks guys, thanks for the help.

As to the lua examples, they're too expansive and I'm afraid I'd do something wrong, of course I'll use them for reference, but for beginning that'd be a bit hard to consume. Can you guys just tell me, list, even, the functions one has to call to make a class and function available to lua? I dont need anything extensive, just something so I know what to lookup, thanks guys.

As to python, I really really really would like to try it, using Boost::Python, however, the build process is soo confusing, I've tried it countless times, so that stops me from actually trying it. I googled already on how to install it, and they all point to the install pages on the boost site, which are confusing. I found a thread on gamedev on how to install it, but no success as well, I saw one guy just edit config.hpp but from that I didn't know what was next, if he built the library or he just used the includes/source. I was looking to see if there were any pre-built boost or boost::Python libraries for msvc, also with no answer. I'd really much like to try python (since I've already tried lua, which I like), but I cant seem to get boost to work. I'd like to use boost over SWIG first since I know more people that use Boost. So if anyone could please help me out with this I'd greatly appreciate it, thanks guys.
SiCrane
SiCrane
I have an unpublished article on building projects with boost::python. If you're having trouble building boost itself, then there's an example of the steps you need to do to build the relevant parts statically in your application instead of as a DLL.
TDragon
TDragon
The Boost build process may seem a bit confusing, mainly because there are many options. But for normal usage for a single setup, it's actually quite simple. The following instructions assume you're using Windows.

1. Download Boost and unzip to the directory of your choice.
2. Download Boost.Jam for Windows and unzip to the directory of your choice.
3. Open up a command prompt
4. If your compiler is Visual C++, invoke vsvars32.bat (usually "Program Files\Microsoft Visual Studio\VC7\Bin\vsvars32.bat")
4. If your compiler is MinGW32 (GCC), make sure its binary and include directories are in the path. This is usually already done for you; invoke "gcc -v" to see if it can find gcc, and if it does then you're probably set.
5. CD to the directory you unzipped boost to; then invoke bjam. If your compiler is MinGW, bjam "-sTOOLS=mingw" stage. If VS .NET 2003, bjam "-sTOOLS=vc-7_1" stage. If something else, switch to one of these. ;)
6. Wait. When finished, everything will be C:\Boost (use the --prefix bjam switch to change this).

Hope that helps,
Twilight Dragon
blanky
blanky
Thanks guys that's nice, I built it now, thanks!
John Schultz
John Schultz
Quote:
Original post by blankdev
As to the lua examples, they're too expansive and I'm afraid I'd do something wrong, of course I'll use them for reference, but for beginning that'd be a bit hard to consume. Can you guys just tell me, list, even, the functions one has to call to make a class and function available to lua? I dont need anything extensive, just something so I know what to lookup, thanks guys.


Here's a minimal example with Squirrel (similar binding style with luabind and boost::python):

// Class:
class MyClass {public:  int classVal;  // See examples in testSqPlus2.cpp for passing arguments to the constructor (including variable arguments).  MyClass() : classVal(123) {}  bool process(int iVal,const char * sVal) {    printf("classVal: %d, iVal: %d, sVal %s\n",classVal,iVal,sVal);    classVal += iVal;    return iVal > 1;  } // process};


// Registration and script run test:
// Registration:  SQClassDef<MyClass>("MyClass").    func(MyClass::process,"process").    var(&MyClass::classVal,"classVal");  // Test script:  SquirrelObject helloSqPlus = SquirrelVM::CompileBuffer("    local myClass = MyClass();                           \n    local rVal = myClass.process(1,\"MyClass1\");        \n    print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n    rVal = myClass.process(2,\"MyClass2\");              \n    print(\"Returned: \"+(rVal ? \"true\" : \"false\")); \n    print(\"classVal: \"+myClass.classVal);              \n  ");  SquirrelVM::RunScript(helloSqPlus);


If you have VS.NET 2003/2005, you can download, unzip, compile and run this example in less than 5 minutes (code). For simple, clean and fast, it's hard to beat Squirrel w/SqPlus (is there a reason you have not considered this option?).

Using luabind with Lua, the registration will look similar (registration with boost::python will also look similar). The big difference in registration between these three languages is that SqPlus does not use boost, and thus will not effect compile times or cause large files to be generated (a not-yet-complete boost-based binding library for Squirrel called Squadd also exists). However, if you really like Python or Lua, it's hard to beat the power and flexibility of the boost binding methods.
blanky
blanky
I really want to try out python, but I cant get it to work and I'm getting very mad and depressed, if someone would be nice enough to send me the librarries, I'd greatly appreciate it. I already compiled them but it seems to not be working.

Thanks john, I got it to compile (the library) I'll give it a try after python. I really really really want to use python, and I know this might be too much to ask, but if anyone could please get me started, I'd really really appreciate it. I built the boost::python libraries but I get a folder with release and debug for both the dll and static lib, after that, I link it to the source I want to compile and it still wont find some files :(

EDIT: Nevermind the python things for now, what I did was bjam within the python directory instead of the boost main directory, probably why it's not working.

[Edited by - blankdev on October 18, 2005 10:22:10 PM]

Topic Locked

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

Sign in to reply to this topic.