Wednesday, June 10, 2009

Century Milestone

Whooohooo! I just had to do the programmer's cheer, but you know, not too loud. One must stay humble or the Programming Gods will smote you down.

Anyway, its been 100 days of actual work on my game. The journey has been long, but rewarding. I celebrated it yesterday night/this morning (what time is it?) by re-writing my OGL ES graphics engine and I kid you not, after a few compile and link errors, I told myself, "just kick that b*tch off and when it crashes, start debugging." To my amazement, I mean, that usually doesn't happen, it ran flawlessly (staying humble, I will check it out thoroughly later, make sure nothing is lurking to bite).

Also, this evening I finally got around to incorporating Lua into my project. I ran the obligatory "Hello, World!" script to make sure it was working and it is. Whooohooo!!

Now, for those who may still be trying or wanting to incorporate Lua into their projects and haven't been able to get it running or wondering where to start, well I will tell you here, because it wasn't easy for me, and I will try my *best* to make it plain and simple. Ohhh...I hate postings on the web where people leave out the little details.

Anyway, you will (oh yeah...this is strictly getting Lua to run in an xCode/iPhone project) need to grab, "lua-5.1.2(Xcode).zip" (now that I can't remember where I downloaded from since I downloaded it 2 months ago), but if you want it and can't find it, post a comment asking for it and I will email to you. So, the steps...

1) Get lua-5.1.2(Xcode).zip

2) Unzip it

3) It will unzip to the folder lua-5.1.2

4) Open it and double click on the lua.xcodeproj

5) Build the project (just to make sure it builds) ...it will generate a library file but don't try linking with it, too may hassles.

6) Copy the /Src folder from the Lua folder into your project. I created a "Lua," folder inside the "Classes," folder in my project and dumped it there.

7) Find in the /Src folder the files lua.c & luac.c and remove them (they both contain a "main," entry and will screw up the build).

8) Build your project to make sure things can compile (they should).

9) Create a file test.lua and type this in... print("Hello, World!") or whatever you want to print.

10) Copy the test.lua file to your project. I have a folder in my project GameResources, so I added a folder named Scripts and added my test.lua script there. Where ever you copy, just
remember to go to the Xcode menu, select Project and click "Add to Project," and navigate to "test.lua," or if you did create a folder to hold your scripts select the folder, and add it to your project. This will add your script to your Bundle.

11) Now find a file in your project, any one for now, since just testing, I placed my little test inside my view class. Add...

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

Since I wanted the code to just fire off, I added the following lines of code in the view class "initWithCoder" method.

lua_State* L;

L = lua_open();
luaL_openlibs(L);

// The "dofile," needs a path to our script. Well, the iPhone stores your data in the Bundle so
// you will need to grab the path to the bundle.

NSString* bPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"lua"];

// check to see if it was found, if not then you didn't add it to your project properly
if(bPath == nil)
{
NSLog([bPath stringByAppendingString:@"was not found"]);

// abort do something ...some real error handling...whatever
}

lua_dofile(L, [bPath cStringUsingEncoding:1]);

lua_close(L);

That's it..Build and Run...you should see your script write out to the debugger console. Good Luck!

Special thanks goes again to Chris Dillman who pointed me in the direction of including the script in my project, and telling me I needed to get the path to the Bundle. Also, Chris mentioned that each time you touch your script, you will need to clean the build in order for the changes to take place...I haven't actually tried that out to see.

Now...time to get some sleep.

No comments: