views:

69

answers:

1

I'm trying to get to grips with how Lua works, and how it can be integrated into a project, like an Obj-C based iPhone game. I've got Lua's source integrated fine, and have managed to use a simple manager class to translate function calls written in a .lua file, to Obj-C methods in the project.

I'm now a little stuck; I'm not sure how to go about what I'd like to use Lua for, and having Googled a lot on it I haven't found an article which fills the little gaps in my understanding. What I'd like to do is use Lua to script simple game logic for levels in my game; for example, I might want to check that the player has moved to a certain part of the level, then once he does that I check if he picks up an object, then once he throws that object I check if it hits another object, etc, etc.

In the script, I'd like to have something like this:

if (objectsAreTouching(level.objects["objIndex1"], level.objects["objIndex2"]))
{
   //Move to next win-condition.
}

So I know how to access a function, for example the one named above, objectsAreTouching, but I can't see how I would, from within the .lua file, access a Dictionary or Array from within one of the game's main classes. I assume this must be possible, am I right? If so, how do you do this? If you can, can you then have a method in your game engine which returns a BOOL pass that return value to the script, so that the if statement above would then execute the code within it?

I may well have asked this question badly, and so if I need to clarify anything let me know. Also, I'm aware of the issues surrounding Apple's ban of interpreted code, and it doesn't concern me for this little project.

Thanks for any help!

+4  A: 

You have (at least) three options:

  1. Create the Dictionary (using a Lua table) and Array (also using a Lua table) on the Lua side rather than on the C++ side; you can do this in C++ using Lua's C API, or in a Lua script that you load to construct the game environment.

  2. Provide access functions to Lua to read the C++ based Dictionary and Array. These access functions are just like the objectsAreTouching function you described, but at the level of data accessors. Typically the results returned from the accessors would be lightuserdata.

  3. Use Lua userdata objects to share the state of the Dictionary and Array between Lua and C++. In this case they would be C++ objects requiring accessors, just like (2), but would be managed by Lua and could have methods (a metatable) and an environment associated with them.

Doug Currie
Great answer, except the question was about interfacing with Objective-C, where one is likely to use objects like NSArray and NSDictionary. There is no way to hand an object like that to Lua, because it won't know what to do with it. But the same applies: you need to write your own accessor functions that Lua can use to access the data in your Arrays, Dictionaries or other objects.
Felixyz
Thanks a lot, a great answer! While Felixyz is right that it's not specific to Obj-C, if I can access Arrays and Dictionaries through Lua script, that'll do me; Lua doesn't need to do anything with them, just be able to access them so I can pass them to functions. Hopefully after some further reading I'll get it implemented.Thanks a lot!
debu