LUA and XCode

Post: #1
Hello everyone, before I get to the matter at hand I want to take a few sentences to introduce myself to everyone here at the forums. My name is Sebastian Jensen, I am 20 years old and I am from Stockholm, Sweden. I work as a web developer (mostly writing PHP components for Joomla! web sites) but my free time is completely devoted to my hobby project: a X-COM: UFO Enemy Unknown inspired game written in C++ with SDL and OpenGL.

It has always been my ambition to keep this project as data-driven as possible. Therefore we have used tinyXML to parse XML files for practically all of our date. This means soldiers, items, skills and pretty much everything else currently has its own XML file with appropriate attributes.

However, I want to take this one step further and add Lua into the mix, so an XML file for an item will have a "link" to a Lua script that will be executed when it is used or equiped etc. etc.

So now I have 2 questions:

QUESTION ONE: I dont seem to find a XCode framwork package for Lua, so how do I go about including Lua into my project? Can I simply add the header file from the source code available at Don't I have to add a library to the linker too? I have only been working on a Mac platform for 2 months now, so I have no idea how to link stuff like this (with SDL and OpenGL I just added the appropriate XCode framworks ...)

QUESTION TWO: My current idea is to have a singleton class (something like CLuaVirtualMachine) that handles all Lua scripts, but since I haven't really gotten my hands dirty with Lua yet I dont know if this is the best way of going about things. If you have implemented Lua in a object oriented project I would really like to hear how you have set things up ... my experience tells me that whatever I am doing there is a 95% chance someone else out there is already doing it, but better plus I just love reading about smart code solutions, so if you have any, bring it!

Thats it for now. I am glad I finally got around registering here and I look forward to be an active member of these forums...

Thanks in advance,

Sebastian Jensen AKA Madvillainy
Posts: 373
Joined: 2006.08
Post: #2
I can't help you with either of your problems, sorry, but I did want to say "Welcome to the boards" ^_^

Worlds at War (Current Project) -
Posts: 5,143
Joined: 2002.04
Post: #3
Just add lua.h (and any other headers you need) and liblua.a to your project, and Xcode should sort out the rest (though often it doesn't, so post if you have problems).

One of the nice things about Lua's design is that you *don't* have to have a singleton. Singletons are usually bad. I'd just have a LuaInterpreter class that you instantiate where you need it, and pass around from there...
Posts: 834
Joined: 2002.09
Post: #4
Tjena! Wink
You can find an XCode project that builds Lua as a static library and/or a framework here:

As for working with Lua itself, it's been a while since I touched it. However, I would actually recommend you to create multiple VMs, because that way you can keep different scripting tasks separate. For instance, I used to allocate a separate Lua VM for each level I spawned (I scripted win conditions), one for configuration of the application and so on. Lua is fairly lightweight, so an extra VM isn't all that expensive. I could even see me spawning a separate VM for a complex level boss.

Still, keeping them all inside a singleton controller class would probably be sane, so that you have centralized control over them.

The best advice I ever got on using Lua is to use the stack as little as possible. Don't push function arguments on the stack if you're calling between C and Lua. Instead, put all the arguments in a table and push that. That way you get named parameters to the function, and you don't have to track stack depth.

Good luck, and keep asking. Smile
Post: #5
First, Id just like to thank everyone for the quick replies. I am glad to see that these forums seem to be quite lively and well moderated as well.

This stuff is really giving me a headache though.
I mean in a PC enviroment I know Id just put the .a file in the Visual C++ /lib/ directory and the .h files in the /header/ directory ... I guess it is my lack of Unix knowledge that is getting the better of me. I tried adding the framework Fenris suggested but even when adding it into the project I am not able to include lapi.h ("no such file or directory.") Is there anything else I need to do? Surely, I can not be the first who would like to add Lua into XCode.

Other than these issues I am very happy using the Mac as my development platform. Everything Ive written with SDL so far compiles nicely in Windows and I am sure this just has to be the future. With so many Macbooks out there the market has to be really good for well crafted indie games on the Mac?
Posts: 45
Joined: 2006.11
Post: #6
If you are using Lua through a framework, say, "lua.framework" then you have to include the framework name in the include.

#include <lua/lua.h>

This is generally a bad thing to do for ported cross platform libraries (unless they put all their headers in a directory) since you have to surround your #includes with #ifdef __APPLE__ or some variant.

Instead, in XCode, you need to add a header search path to the framework's internal "Headers" directory. Then you can just #include <lua.h> and it will work. Using gcc on the command line, add the -I./lua.framework/Contents/Headers option (assuming the lua.framework is in the same directory).

If all of that is sounding confusing to you, then just copy the lua headers into your project directory, as well as the .a file, and include it in your project. Then you don't have to worry about anything.

Posts: 245
Joined: 2005.11
Post: #7
I have Lua embedded in my current project. I just built Lua with the standard makefile, as far as I remember. That installed it in /usr/local/ from where you can just use '#include "lua.h"' and import "lua.a" into your project. It is a good idea to add a "copy files" build phase to your target as well, and drag "lua.a" so that your resulting app will be portable (as long as you do a release build).
I just have a LuaHandler class which sets up the VM and gets it to run functions as and when required. All classes which need Lua support put their calls through that.
I'll try to remember to check the details when I get home from work and correct any errors here, but I'm fairly sure that's all right. Apart from a couple of gotchas in the initialisation, I've found it remarkably easy to get up and running with Lua. Of course, I went on to develop other bits of the engine so I haven't done any really serious work with it yet.
Post: #8
Yes! Finally working now!

Big thanks to everyone who contributed in this thread; JeroMiya, your post solved my first problem and backslash I think you pretty much answered my second question. I might have more questions about that later though, but that's it for now.

Posts: 5,143
Joined: 2002.04
Post: #9
backslash Wrote:It is a good idea to add a "copy files" build phase to your target as well, and drag "lua.a" so that your resulting app will be portable (as long as you do a release build

.a files are static libraries, you do not need (or want) to include them with your application.
Thread Closed