Search code examples
c++scriptinglualua-api

How can I get list of all table names in .lua script using Lua C API?


I have a lua file which looks like this:

tableA = {…}
tableB = {…}
tableC = {…}

I want to get std::vector which will contain the names of all tables defined in file: {"tableA", "tableB", "tableC"}

How can I do this with Lua C API?


Solution

  • Tables don't have names. Variables do. In this case, the variables are globals so the variable name is just a key in the globals table. That said, if this is an API you're writing, then it's probably a bad design. You should give the script a means of "registering" meaningful values rather than forcing the script author to put them in a specific place.