lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I would guess I would need co-routines, or muliple lua_states, how to I set
> them up, THE BIG THING how do I interface them with my GAME to make them
> work, you know how much time I have spent reading nothing but .lua files, I
> don't care about how they work inside lua, I want to know how they work
> together with my app, there is plenty of good documents on the scripting
> langauge itself, I can figure that out, but there is little or no
> documentation on interfacing it with your app.

Did you read the 3rd section of the manual? If you've got a basic
understanding of stack manipulation and you read all of the 3rd section
you should be all set to make a scripting interface.

Yes, there are some tricks to it. Here's basically what I came up with
after trying things:

The actual objects themselves should be created with lua_newuserdata so
that the memory management is handled by Lua. You shouldn't delete stuff
with the class's destructors, rather use the __gc metamethod in the
userdata's metatable.

Immediately after getting the new userdata object, I add a few things to
its metatable (handlers for __index and __newindex mostly) to give
access to the variables that are stored in the C data structure (x, y,
size, etc. in my case).

If you need to store global C state, use the table at LUA_REGISTRYINDEX.
(I use this since I'm writing in C and need basically a void* data).

Every frame, I iterate through the list of entities (which I store in a
linked list in C) and then I use lua_pcall on the userdata's "update"
function, giving the userdata and the time passed as parameters.

That's about all there was to it. The hardest part for me was figuring
out that Lua wants to memory manage everything (which makes sense).
Although it's somewhat possible to manipulate C structures that weren't
created by Lua, it is much easier to just use userdatas all over the
place.