Sign in to follow this  

Accessing Lua Modules from C++

Recommended Posts

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

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

> require "a"
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

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
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.


lua_pushstring(L, "foo");
lua_gettable(L, -2);
int error=lua_pcall(L, numArg, LUA_MULTRET, 0);



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this