1

When I call lua_pcall from C++ to call a Lua-function, I usually do something like this:

 const int pcall {lua_pcall(lua_state, 1, 1, 0)};
 if (pcall == LUA_OK){      
    ...
    return true;
    // OK!
 }
 std::cerr << "Error: pcall failed. Code: ";
 std::cerr << pcall;
 std::cerr << ", '" << lua_tostring(lua_state, -1) << "'\n";
 ...
 // ERROR!

This is nice, but not enough to determine the exact error-line in my lua-code. This is my terminal output:

Error: pcall failed. Code: 2, 'attempt to call a nil value'
Error: pcall failed. Code: 2, 'attempt to call a boolean value'
Error: pcall failed. Code: 2, 'attempt to call a nil value'
Error: pcall failed. Code: 2, 'attempt to call a nil value'
Error: pcall failed. Code: 2, 'attempt to call a userdata value'

How can you retrieve more information (for example the code-line) about each error?

user1511417
  • 1,880
  • 3
  • 20
  • 41
  • 2
    See [luaL_traceback](https://www.lua.org/manual/5.3/manual.html#luaL_traceback). – lhf Jul 16 '18 at 18:05
  • Thanks, but it seems that this function is not documented anywhere. You are more than welcome to post a little working example as an answer. – user1511417 Jul 16 '18 at 18:40
  • I gave a link to the manual entry. Which version of Lua are you using? – lhf Jul 16 '18 at 19:47
  • 5.3.4. Yes, I've read this entry and I just don't get it. I tried calling `luaL_traceback(lua_state, lua_state, nullptr, 1);` after the first `if(...){...}` in the post's code. This did not do anything. – user1511417 Jul 16 '18 at 19:59

0 Answers0