3

So I have heard that Lua is a good scripting language that ties into C++. Does anyone know some good resources for picking it up, for someone with lots of C++ experience?

rlbond
  • 65,341
  • 56
  • 178
  • 228
  • This web site provides information about integrating lua and C++ and seems to be compatible with lua 5.2 which has some changes from lua 5.0 http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm – Richard Chambers Dec 15 '12 at 21:14
  • Here is a code project example though it uses the older lua interface calls however it can be helpful. http://www.codeproject.com/Articles/11508/Integrating-Lua-into-C – Richard Chambers Dec 15 '12 at 21:18

3 Answers3

9

You may want to look at toLua++ or Luabind for C++ integration.

As far as learning lua itself goes, the Programming in Lua book or even the Lua Reference Manual shouldn't be out of your league at all; see the documentation section of the lua website.

The usual rule applies: read lots of other code when you're getting started. If it's your cup of tea, you could e.g. go dig though World of Warcraft addons for some (admittedly specialized) real-world examples.

And listen to the community: subscribe to some mailing lists, take a look at the lua-users resources (especially the wiki), et cetera.

I work at a game development company, and we use primarily C++ and lua together. We don't actually use Luabind or toLua++ yet (mostly just a lack of time to test and integrate them), but a few things we've learned:

  • you'll want to make a choice between creating and destroying lua environments (lua_State instances) on demand and keeping one or more around; getting rid of them can alleviate memory issues and provide nice unpolluted execution environments
  • take advantage of lua_pcall's ability to register a debug function, see discussion on Gamedev.net
  • if you're on a memory budget, consider using lua_setallocf to change allocator behavior -- constrain it to its own area of memory to prevent fragmentation, and take advantage of a more efficient small object allocator (perhaps boost::pool) to reduce overhead (other ideas in an earlier answer)
  • get a good lua-aware editor; we use SciTE and Crimson Editor a lot where I work
  • pay some attention to your garbage collector, call gc with various arguments and see what works best for your performance and memory requirements; we've had games where full gc each frame was the right choice, and others where 10% per frame was the right choice
  • when you get comfortable, reach out to metatables; altering index and newindex has proved especially useful for us
  • oh, and coroutines are sexy
Community
  • 1
  • 1
leander
  • 8,527
  • 1
  • 30
  • 43
1

These might give you an idea of where to start. The Lua C library is a bit low-level than you might expect with regards to getting your own C++ code mixed into the Lua virtual-machine etc, but have a look at these anyway.

http://csl.sublevel3.org/lua

http://www.codeproject.com/KB/cpp/luaincpp.aspx

http://heavycoder.com/tutorials/lua_embed.php

Nick Bedford
  • 4,365
  • 30
  • 36
0

First off all see http://www.lua.org/manual/5.1/manual.html#3. It's very easy to integrate Lua and C++. But for large programs I'd suggest to use swig.

AlexeyBor
  • 21
  • 1