Jump to content
  • Advertisement
Sign in to follow this  
Antonym

Accessing Lua Modules from C++

This topic is 3006 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

So I have two lua files. a.lua
module(..., package.seeall);

function foo() print("Hello World!") end



main.lua
> require "a"
> a.foo()
Hello World!



If I do in c++
luaL_dofile(app->l_state, "main");



How then could I call a function in a.lua from c++? I know that one could do
	lua_getglobal(L,"my_function");
lua_call(ps->app->l_state,0,0);

to call a function in main.lua but would the same work if the function I wanted to call was in a.lua? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
So when you call

luaL_dofile(app->l_state, "main");

The main function has a require for a.lua. This will pull a.lua into your lua environment. You need to get the function from the table a onto the stack and then call it directly.

try


lua_getglobal(L,"a");
lua_pushstring(L, "foo");
lua_gettable(L, -2);
assert(lua_isfunction(L,-1));
int error=lua_pcall(L, numArg, LUA_MULTRET, 0);


Cheers,

Bob

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!