lua-users home
lua-l archive

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


Look into the example from Game Programming Gems 5 - 1.13 Building Lua into Games.  The author uses a script manager class that can allow registered scripts to yield, etc - basically, assuring all scripts get a chance to process in turn without bogging down the main loop.  His example needs some modification to make it really cool, but it's a good start.
 
Richard
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Ive
Sent: Tuesday, July 19, 2005 8:59 AM
To: 'Lua list'
Subject: RE: Attach Lua script To C++ class!

Hi all!

 

I have pretty much the same dilemma atm

 

The problem with one script per actor is that they can’t share variables and functions!

 

And the problem having only one lua_state* is that you can’t duplicate the same script several time without sharing the same variables and functions for all actors assigned to this script!

 

Is that right?

 

If yes … having the possibility to make global and local script make sense?

 

thx

 

 

 

 

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of herviou
Sent: Tuesday, July 19, 2005 3:24 AM
To: Lua list
Subject: Re: Attach Lua script To C++ class!

 

Hello,
I have choosen the same solution as Jason. Here is a (simple) example you can do to register lua script inside a C++ class (store has a filename)
In C++ pseudo-code:
class LuaAI {
    void acting (void); //the method called to animate your buddy at each frame
    StlString _filename; //the AI script
    bool _first;
    lua_state* L;
}

void
LuaAI::acting(void){
    if (_first){
    lua_dofile(L,_fileName.c_str());
    first=false;
    }  
    lua_pcall(L,0,0,0);
}
Something interresting can be done with this solution, instead of testing the boolean _first you can have a method that checks if the _filename has been modify since its last execution, this way you can modify "on the fly" the behavior of your character!

Personnally I have choosen to have one lua_state for each character, this way each one has its own stack of execution,but again that's a personnal choice...

david.

Ive wrote:

O interesting!
 
Can u be more precise on this and/or point me to some example?
 
thx
 
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Jason Murdick
Sent: Monday, July 18, 2005 3:35 PM
To: Lua list
Subject: Re: Attach Lua script To C++ class!
 
Sure it is!  The two ways I've stored scripts inside C++ classes is to
simply store the script as a string or as a filename/path.
 
Jason
 
 
On 7/18/05, Ive <neosettlers@sympatico.ca> wrote:
  
Am I at the right place to discuss this?
 
...
 
Alright!
 
What I m trying to do is to assign to each actor in a game environment
    
a
  
script ai!
 
I m wondering if I must do:
 
lua_State *m_lua;
 
       if ((m_lua = lua_open()) == NULL)
       {
               Trace("ERROR: LUA initialization failed!\n\n");
       }
       else
       {
               Trace("TELL: LUA initialization successful!\n\n");
 
               luaopen_base(m_lua);
               luaopen_table(m_lua);
               luaopen_io(m_lua);
               luaopen_string(m_lua);
               luaopen_math(m_lua);
               luaopen_debug(m_lua);
 
               RegisterFunction(); /// register all functions
 
       Int l_status = luaL_loadfile(m_lua, in_name.GetName());
       lua_settop(m_lua, 0);
 
       if (l_status != 0)
       {
               lua_error(m_lua);
       }
       }
 
to each of the script!
 
 
Then update them individually each frame with:
 
lua_pcall(m_lua, 0, 0, 0);
 
or
 
my first guest is to have only one lua_State instance and register all
functions once and then keep track with a pointer to each
luaL_loadfile... but I cant find the way to retrieve them... can
    
anyone
  
clear this up please?
 
Is lua_open() should be call once (then register all possible
    
functions
  
once) or to all attached scripts? What is the ultimate way to do this
with out unnecessary overheads?
 
thx