lua-users home
lua-l archive

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


Hi Tim!

Tim schrieb:
If I had the time, this is all predicated on time, which I have nill.  We
have like a short
time to do a game, we don't have much staff to work this game, we have most
of an engine
I have little to no gameplay, I wanted to use lua to help get gameplay fast,
then offload
the scripting to someone else who can read a sample script I provide, and be
good to
add other things, and basically bug me about exposing more functions, I
don't have the
time to read lua manual 45 times, and sit back and drink a beer, and read it
again, this isn't
rehab, what I want to do is so basic, it blows my mind.

what I want in simple form.

player 1 uses player1.lua
in the script there are 3 functions, init, proc, exit

player 2 uses player2.lua
in the script there are 3 functions, init, proc, exit

player3 uses player3.lua
in the script there are 3 functions, init, proc, exit

etc etc, get it?

init is called once in my game init.. it does whatever it needs to do, to
set up the player.

proc is called once per game frame, until game end, or player die etc.

exit is called once on exit

I think I used a similar setup for my ircbot modules. You probably need a separate globals table for each of your actors. You could do it like this piece of Lua code:

---------------------------------------------------------
-- this is going to be the globals table for player1. you need a distinct
-- one for each actor
local environ = setmetatable( {}, { __index = _G } )
local chunk = assert( loadfile( "player1.lua" ) )
setfenv( chunk, environ )  -- set the environment
assert( pcall( chunk ) )   -- call the lua file
-- if player1.lua defines the global functions init, proc and exit, these
-- functions are reachable through environ: environ.init, environ.proc and
-- environ.exit
---------------------------------------------------------
Once you have the environ.proc function you can use it like you want: pcall it, create a coroutine and resume it, ... You have to store the environ somewhere - I used a Lua array, but you will probably want an array on the C side (using references (see luaL_ref and luaL_unref)). If you use coroutines you will need to keep those somewhere, too (to prevent them from being gc'd). Using section 3 of the reference manual, I'm sure you can translate the above code to the equivalent C functions.


I would guess I would need co-routines, or muliple lua_states,

I don't think that you need coroutines. Just call your actor.proc function, but if using coroutines you could suspend the proc function somewhere in between and resume execution later. This can be convenient since the function looks like a straight forward description of what's going on (especially if the yields are hidden somewhere in functions).
It depends entirely on how you want to code your proc function...

 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,

Well, probably you have your lua_State set up and call some actor scripts using a different environment every time. You end up with some tables each containing an init, proc and exit function. You store those environment tables somewhere (e.g. the Lua registry). On each frame you get every environment table in turn and call the function proc in it. I think this could be your general plan and I hope that you can at least ask more specific questions now...

 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.



HTH,
Philipp