Build Lua Source Into Project

Started by
4 comments, last by Hodgman 11 years ago

Things are coming along nicely, and my game engine's starting to support some really nice features (when they're not broke!). I'd like to support Lua in my own. I've started checking out tutorials that show how to build it as a library and link it, but I'd rather import the source into my engine, and build the Lua source code itself into my engine to totally integrate it. This way, when I release it, developers won't have an issue having to build the library themselves, setup all the linking etc...

The problem is, Lua's source code contains its own main() functions in lua.c and luac.c. I've read online that I can just omit lua.h/.c and luac.h/.c, but it looks like I'd be missing out on some error checking that looks important.

I'm also new to Lua, so I'll be learning how it works as I try to get it to compile. I'm also using Xcode 4 to build this for Mac and iOS of that's important.

Advertisement
lua.c is the source to build the stand-alone Lua interpreter. luac.c is the source to build the standalone Lua compiler, to pre-compile scripts to bytecode. They don't include any error checking that would be important for any kind of game engine, although it might be useful to study so that you can see what kind of error checking they do perform and see if it might be useful to include such in your own project. But a lot of what they perform mostly has to do with parsing command-line arguments on startup, so if you are not planning on interacting much with your game via command-line args then you don't need that.

Lua is a simple enough library to work with. It doesn't include any outside dependencies, it's small and lightweight enough that you don't necessarily have to build it as a library if you don't want, etc... You can just include any .c file that doesn't include it's own main() directly into your project.

However, I highly recommend building Lua as a .DLL. Why? Because LuaJIT, that's why. If Lua is built as a DLL, the LuaJIT DLL can be swapped in without any troubles whatsoever. Just copy and paste the DLL to your executable folder. And LuaJIT is a pretty neat thing to have access to, if performance is important. (Which, in a game, it is.) Including the Lua source into the engine itself means that developers who use your engine can't just simply drop LuaJIT in if they so desire.
Yeah, you can treat Lua.c as basically being an example application that makes use of the Lua library in order to create a command line interpreter.

Regarding LuaJIT, I've just included the LuaJIT source directly into my engine project (so I've the opposite situation - end users can't choose to swap over to the official Lua version).

Regarding LuaJIT, I've just included the LuaJIT source directly into my engine project (so I've the opposite situation - end users can't choose to swap over to the official Lua version).

I see nothing wrong with this...

I may have jumped the gun on this. After removing the lua.c and luac.c, it compiled fine and was reading basic scripts I was trying out from tutorials. I'm still glad I posted here because you bring up LuaJIT, which is something that looks important. I'm just starting out on Lua right now, so I'll learn more about how it works and interacts with C, then I'll go from there as far as implementing LuaJIT, which I have the source to now.

LuaJIT is great, as it is just another implementation of Lua that obeys their specifications exactly, but it has much better performance. Even if you disable the "JIT" part (where it compiles your Lua scripts into native CPU instructions), it's still faster than Lua due to large parts of it having been hand-optimized.

However, the down-side is that because it's so optimized, it's source code is barely readable, unless you're also an optimization expert. This isn't a problem in normal usage, but if you use any API illegally (e.g. pass it bad pointers, etc), then you can make that API crash internally. If you make LuaJIT crash internally, it will be hard to debug, whereas with regular Lua, you might get a bit more useful debugging info, like call-stacks etc...

Once you've got Lua working fine, you can pretty much just replace your Lua source files with the LuaJIT source files and everything will still work the same.

This topic is closed to new replies.

Advertisement