lua-users home
lua-l archive

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


I'll point you back to Nick Trout's suggestion about co-routines:

http://lua-users.org/lists/lua-l/2003-12/msg00073.html

And also point out that a lua co-routine is the same as a lua thread
(see lua_newthread()).

You should also investigate lua environments.

A note about your architecture:  I think you might get into trouble
using the global variable "processingactor".  What if an entity's script
wants to call a function from another entity's script?

If your functions operate on an entity then, rather than operate on a
global variable, they should take the target entity as a parameter.

One more suggestion:  Try writing a small text-based prototype of your
game entirely in lua.  This will not only help you to learn lua, but
also give you a good idea of where the lua-C interface should be.

-Kevin

> 
> This is what I have now, it's very early yet, I just started, 
> and I have bounced things around trying to get this to work, 
> so it's not what I want in the end, but here it goes.  I have 
> a state machine, that for each state has an init, proc, exit, 
> and in my init for the main game state, I call the following.
> 
> mylua_Init();
> 
> looks like this at this moment ..
> 
> static lua_State * ls;
> 
> void mylua_Init()
> {
>      ls  = lua_open();
> 
>      if (NULL == ls) logger("Error Initializing Lua\n");
>      else
>      {
>             logger("Lua Initialized\n");
> 
>          lua_baselibopen(ls);
>       lua_iolibopen(ls);
>       lua_strlibopen(ls);
>       lua_mathlibopen(ls);
> 
>       lua_register( ls, "addNPC", fl_AddNPC );
>       lua_register( ls, "drawstring", fl_DrawString );
>       lua_register( ls, "drawnumber", fl_DrawNumber );
>       lua_register( ls, "getsceneframe", fl_GetSceneFrame );
>       lua_register( ls, "findsetlocation", fl_FindSetLocation );
>       lua_register( ls, "keyhit", fl_KeyHit);
>       lua_register( ls, "setframe", fl_SetSceneFrame);
> 
> }
> 
> sorry bout the spacing, outlook has hosed my tabs, then I call
> 
> mylua_Load("dam.lua");
> 
> which looks like this ...
> 
> void mylua_Load(const char *scriptname)
> {
>  pushandsetdir("scripts");
>   lua_dofile(ls, scriptname);
>  popdir();
> }
> 
> 
> dam.lua looks like this(looks familor right? :) )
> 
> local Hero = assert( loadfile( "hero.lua" ) )()
> local GuardGuy = assert( loadfile( "guard01.lua" ) )()
> 
> function createHero()
>    return setmetatable( {}, { __index = Hero } )
> end
> 
> function createGuardGuy()
>    return setmetatable( {}, { __index = GuardGuy } )
> end
> 
> Hero = createHero();
> 
> Hero:doSomething()
> 
> 
> 
> this works fine but it's not what I want, want I had before 
> was I would create a hero like this
> 
> MOBHuman *thisguy;
> 
> thisguy = addchar(0,0,0,actorlist);
> 
> then I would load a file like ...
> 
>  mylua_Load("heroscript.lua");
> 
> then in my main loop I would call
> 
> void MOBHuman::Processplayer()
> {
> 
>             which would update the guys position keyboard 
> input , etc etc.
> 
>             I would also call something like mylua_aiprocess(this);
> 
>             which looked something like this..
> 
>             void mylua_aiprocess(MOBHuman *curactor) {
> 
>                     int val;
> 
>                    // I stored the current actor passed in so 
> that all the exposed functions of my game knew which was the 
> current guy
> 
>                 processingactor = curactor;
> 
>                  lua_getglobal(ls, "playerprocess");
> 
>                  lua_call(ls, 0, 1);
> 
>                 // so that when it called something from lua 
> like here is my old scipt I was using
> 
>                 function playerprocess()
>                      local rvalue = 0
> 
>                      if ( keyhit("DIK_SPACE") > 0 ) then
>                           --drawstring(-1,30,"hit spacebar")
>                           setframe(4099,1) -- framenumber, 
> continue playing
>                      end
> 
>                      if ( 
> findsetlocation("herostart.mxo",1,0) ) then  -- 
> lookfor, setposition, setrotation
>                           --drawstring(-1,30,"haha found something")
>                         else
>                           --drawstring(9,30,"ouch didn't find it")
>                      end
> 
>                      if ( getsceneframe() > 4100 ) then
>                           findsetlocation("dockstart.mxo",1,1)
>                           rvalue = 1;
>                      end
> 
>                         return rvalue
>                 end
> 
>                 again this loop was called every frame of my 
> game proc, what this script did was basically take over all 
> input of my game, and force him to an object
> 
>                 that moved in the world until frame 4101 or 
> whatever, then it would bail out, and I would then not call 
> this function any more, this was sorta a hack to get something
> 
>                 working, I planned on revising this, and 
> building from it, when I started having problems with 
> multiple players, having their own scripts.  the idea here 
> was that when a function was
> 
>                 called from lua like this ..
> 
> 
> 
>                     int fl_FindSetLocation( lua_State * luaVM ){
> 
>                                 // I could use 
> processingactor in here, and it would know from(in theory) 
> when it called from lua, that it was dealing with processingactor
> 
>                                 // this is also when I 
> starting having trouble when I added another guy into the 
> world, and created him and loaded his file, which has the 
> same function name in lua as the hero guy playerprocess, since
> 
>                                   // I guess there was only 
> one lua_State it was using the latest playerprocess function 
> for all guys
> 
>                    }
> 
> 
> 
> Hope this isn't too complicated to understand, not even sure 
> the best ways to do things in lua, I almost wrote my own 
> scripting language, when I realized I didn't want to
> 
> re-invent the wheel (so to speak), and lua I stumbled across, 
> from a company who didn't make it  and decided to opensource 
> the whole engine, and source, and binary to
> 
> http://catmother.sourceforge.net/  which I applaud them for, 
> and I have looked that the stuff they have, but it's a wild 
> goose chase trying to figure out what was going on.
> 
> which sometimes is why I hate c++.  Anyways what I what is a 
> highlevel scipting language with calls that pretty much do 
> all internal stuff within
> c++, and I decide
> 
> what to do with the results given back from my game.  In 
> order to do this, I need to be able to have my game call lua 
> basically every frame (maybe every
> few) (for characters and other scritables)
> 
> to update the status of things.  Of couse with anything you 
> need a foundation to build from, and my foundation has 
> termites (at the moment) thats why I look here for help, I 
> appreciate it.
> 
> Tim Ryness
>