ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.16k stars 504 forks source link

Always getting nil out of function call from Lua #1388

Closed stiggy87 closed 2 years ago

stiggy87 commented 2 years ago

Running into a weird issue, where I am trying to build a very simple "Lua console" that I can use to do live-debugging/updates of a much bigger project I'm working on. In the process, I created a simple Lua file filled with functions and all the functions will take an argument as a string, where I will do a getopt type function to parse it out, complete the task and return a string of results, or pass/fail, etc.

Running the Lua manually gets the results I want, but when I call it from the C++, it always returns nil. I'm assuming I'm messing something up on the stack, but I'm not sure. You can see an example of the code here: https://godbolt.org/z/a5qrxeE6h

I did create a single sol2 header since that's most likely how I will end up deploying things, but the example above doesn't do that.

Compilation details: compiler: gcc 9.4 with C++17 w/ Visual Studio Code OS: Ubuntu 20.04 (WSL2) Lua 5.2/5.3

Any help would be great!

stiggy87 commented 2 years ago

Nevermind. I forgot to add in more libraries, especially the sol::lib::string.

On that note, is there a way to blanket open all libraries, or is it strictly explicit?

Rochet2 commented 2 years ago

Gmatch is a part of the string library. You need to open sol::lib::string in order to use it. You can also open all libraries with lua.open_libraries() Also, vars.gmatch should be vars:gmatch as you are calling it like a method.