3

This is not about how to call a lua function from C++. I can already do that successfully. This is about the lua function "loadfile" no longer working when used in a function called by C++.

I'm trying to load a lua file into another lua file so that I can call its functions. If I move test() from functions.lua to game.lua, mystring receives "test" just fine.

However, when I add the init() function to the game.lua file and move test() to the functions.lua file:

  1. loadfile no longer seems to work, producing "error in error handling"
  2. calling test() doesn't work (probably due to the failed loadfile) which also results in a "error in error handling" message.

C++:

// Having loaded game.lua...
state["init"]();
std::string mystring = state["test"]();

game.lua:

function init()
    loadfile("functions.lua")()
end

functions.lua:

function test()
  return "test"
end

Error:

"Error in error handling"

When I use ZeroBrane to test this Lua script by simply doing:

init()
print(test())

It all works fine... so there's something wrong with how I'm either using C++ or using the lua file.

What am I doing wrong here? Why doesn't loadfile seem to work when used in this manner? Does lua not get initialized to the point of seeing its regular functions with started by C++? Really strange how loadfile works via ZeroBrane but not from C++.

I'm using Selene, a C++11 header-only bindings to Lua.

1 Answer 1

0

Don't forget to establish the lua context!

Establishing Lua Context

using namespace sel;
State state; // creates a Lua context
State state{true}; // creates a Lua context and loads standard Lua libraries
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.