lua-users home
lua-l archive

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


I have been playing with lua for the last little while trying to get lua working within the game.  I can run a single script via/ lua_dofile, and register some functions.  So I now I have a script such as the following.
 
function playerinit()
local rvalue = 0
    if ( keyhit("DIK_SPACE") > 0 ) then
        drawstring(-1,30,"hit spacebar")
        rvalue = 1
    end
  return rvalue
end
 
ok yeah really simple, I get this just fine, what I want to do is have every player in the game, have it's own script like badguy  uses = badguy.lua, hero uses = hero.lua  what I want to do now is have three states for every player like init, process, exit, so I have a playerinit(), playerproc(), playerexit() in each ai script, so when I process my guy from my c++ code, I can say (from my main process loop c++) registerplayer(this), it registers my luaclass with a pointer to the active player.  Then I  then later call lua .. via lua_getglobal(luaVM, "playerinit") etc, but I want each of my .lua files to have the same function names init proccess exit.  when a state(funcion in lua) is complete it returns true, and then I change from say init to process, and inside process would have commands like..
 
if(enemyisnear()) runforcover();
else patrolpath();
 
That would be my process function, the problem I guess I'm having, is the whole problem with multiple lua_states, and stuff, and how to arrange it, how to set it up, etc.  My app is not multithreaded, but can I use lua_newthread?  I'm basically lost, combing through threads of threads looking for answers so I have come to this.   What it seems as though is happening, is that when I add a new guy into the world, and call lua_dofile(), it basically becomes the script in which all players use, I tried something with calling lua_open and storing a state in the player class, but that didn't seem to work to well..  Ideally I would like to use one lua_state, and then when the lua functions are called from c, it just works itself out, I have no idea, how to set this up.  I'm beyond frustrated, I'm a beginner user, I don't have lots of time to spend researching this stuff, but I want to use it, it's a great language, there must be a good and easy way to do this.  I think I could probably do it if each guy had unique function names, and somehow, store them in the class and call them as needed, but I would rather have the same set of functions for every actor in the world.
 
 
Any Ideas?
 
 
Thanks,
 
Tim Ryness