lua-users home
lua-l archive

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


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



    


  

begin:vcard
fn:David HERVIOU
n:HERVIOU;David
org;quoted-printable:Ecole Nationale d'Ing=C3=A9nieurs de Brest
adr;quoted-printable;dom:BP 38;;25, rue Claude Chappe;Plouzan=C3=A9;France;29280
email;internet:herviou@enib.fr
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 47
tel;fax:+33 (0)2 98 05 89 79
note;quoted-printable:Doctorant En Informatique au laboratoire LISyC (Laboratoire d'Informatiqu=
	e des Syst=C3=A8mes Complexes) dans le cadre du projet AR=C3=A9Vi.
url:http://www.cerv.fr/~herviou
version:2.1
end:vcard