A Brief LuaCore Tutorial

I’ve been playing with Gus Mueller’s LuaCore, which allows you to run and interact with Lua scripts from inside a Mac Cocoa application. It’s very helpful, but unfortunately the included “TestApp” is pretty simplistic and doesn’t demonstrate much more than simply executing code with LuaCore. Since I had a little trouble getting started, I figured I’d provide a very brief tutorial that should be enough to get you started. Please note that I’m on the edge of my understanding here, so the odds that I can help you past this are pretty slim at this point.

Part 0: Build LuaCore

First, let’s get LuaCore built.

  1. Download the LuaCore.tgz file from Gus’s website.
  2. Open LuaCore.xcodeproj in Xcode and Build it.
  3. In Finder, locate the project directory. You should see a build directory. Inside it will be a Release directory, and inside that a LuaCore.framework directory. Copy or move LuaCore.framework to /Library/Frameworks.

We are now ready to incorporate LuaCore into our own application.

Part 1: Add LuaCore To Your Application

Create or open your Cocoa application in Xcode. Then:

  1. Select the project in the Groups and Files pane.
  2. Select Project | Add to Project… and navigate to /Library/Framworks. Select LuaCore.framework and hit Add.
  3. Go with the defaults.

You should now have LuaCore.framework listed in the Frameworks folder of the project. Go ahead and try to build the application.

Part 2: Call LuaCore from Your Application

Open up main.m or whichever file you want to call Lua from. Add the following import statements:

#import <Foundation/Foundation.h>
#import <LuaCore/LuaCore.h>

Now let’s borrow some code from Gus’s TestApp. Place this in main() or wherever appropriate for your application.

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
LCLua *lua = [LCLua readyLua];
[lua runFileAtPath:@"/Users/you/YourApp/test.lua"];
[lua tearDown];
[pool release];

Note that we’re referencing a .lua file that doesn’t yet exist. Let’s fix that by creating a very simple Lua script in that location:

print("What a buddy!")

Now let’s compile and run your program. You should see this in the Xcode Run Log:

What a buddy!

Part 3: Set a Variable

Let’s send some information from our Cocoa application to the Lua script. One way to do this is so set a variable that can be used by the Lua script when it runs. Change your Lua script as follows:

print("What a "..adjective.." idea!")

This sandwiches the variable adjective into the middle of the string. Note that the variable hasn’t been defined in our Lua script. We can assign it in our Cocoa app, however, so that it will be available for the Lua script. Before [lua runFileAtPath:…, add this line:

[lua pushAsLuaString:@"smashing" withName:@"adjective"];

Now run your app and, if all goes well, get this output:

What a smashing idea!

Part 4: Make Lua Do Something

Setting variables is fine, but what if we want to call a function inside our Lua script? It’s not quite as straight-forward, but once you get the gist of it it’s very doable. We’ll use the LuaCore function callFunction: withArg:. After runFileAtPath:, let’s call the function we’re going to write:

[lua callFunction:@"testFunction" withArg:[NSNumber numberWithInt:5]];

Note that we’re passing an object; withArg:’s type is id. Now we’ll define our function. Create a new Lua file, or clear out the current one. Here’s the code:

function testFunction(arg)
  print("testFunction: " .. arg:self())
end

Why are we calling self() on our argument and not just listing the argument by itself? The reason is that LuaCore turns the id we passed into a Lua table, with all of the object’s functions being callable as if they were Lua functions. Lua doesn’t know how to concatenate a table and a string, so we have to call NSObject’s self method in order to obtain the NSNumber object, which Lua can turn into a string in order to be concatenated. Likewise if we were passing an NSString.

By extension you can see that Lua can cause things to happen in our app in this way.

Where To Go From Here

And now you know all I that know. Except this: LuaCore is based off of LuaObjCBridge, and as far as I can tell it’s a basically a simplifying wrapper for it. You can still access the LuaObjCBridge functions (and Gus does in one part of his TestApp program), so if you get stuck with LuaCore I’d recommend looking at the LuaCore source and cross-referencing it with the LuaObjCBridge documentation, which is a bit more fleshed-out.

My thanks to Gus Mueller for taking the time to make LuaCore available. It’s a great asset, and it definitely makes life much easier than working directly with LuaObjCBridge. Thanks, Gus!

Leave a Reply