Jump to content
  • Advertisement
Sign in to follow this  
golgoth

Attach Lua script AI To C++ class!

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! 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

Share this post


Link to post
Share on other sites
Advertisement
Alright!

so the whole process seams ok... but as a test ive tried:

lua_pcall(m_lua, 0, 0, 0);
lua_pcall(m_lua, 0, 0, 0);

the script is execute only once... why is that?

should i do:

_lua = lua_open())

every frame?

Share this post


Link to post
Share on other sites
Before you execute any function with lua_pcall you must first push the function name onto the stack. You cannot just call lua_pcall(state, 0, 0, 0) and expect it to behave the same as it does when you first load the file.

You could just call load_file over and over but that is probably less efficient (way less).

Instead in your script make some func like

function ai_func()
-- do useful stuff here
end

then do something like this during your update:
lua_pushstring(L, "ai_func");
lua_pcall(L,0,0,0;

Share this post


Link to post
Share on other sites
actually I made a mistake,

first push the func name, then get it, then do pcall

so it would be

lua_pushstring(L, "funcname");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pcall(L,0,0,0;

Share this post


Link to post
Share on other sites
O so interessting!

what about using:

lua_getglobal(m_lua, "in_function");
lua_pcall(m_lua, 0, 0, 0);

???

both way it works wonderfully!

thx everyone!

Share this post


Link to post
Share on other sites
yes using lua_getglobal works equally well. In fact, it is the exact same. If you will look at lua.h you'll see lua_getglobal is just a macro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!