简体   繁体   中英

“Error in error handling” when calling lua function from C++

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM