2

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?

Enlico
  • 23,259
  • 6
  • 48
  • 102

2 Answers2

1

To get the names of random global variables created during the execution of a script file you will need to use environment games to run the script in your own "global" context.

If you can modify the script it would be much easier to get it to return the tables (or just their names) to the calling script.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
1

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.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • I know about globals table, but it has lots of other variables in it so it doesn't give me what I want. I'm using lua scripts as configuration files for characters, props etc. in my game. Right now I need to have have a list which contains all names of entities defined in script file. Right now I'm using simple txt file which lists all entities which can be loaded from script. It would be better if I could know names of all variables I can load from file having the list of their names. –  Jun 29 '14 at 19:12
  • Again, you shouldn't be reaching into the script looking for named items, the script should be telling you what entities it has. That said, you asked for a list of "all tables defined in a file", showing an example where those tables are global. Iterate through the globals for values where `type(x) == 'table'` and you're done. Still much better to let the script tell you about entities by *value* rather than by name. – Mud Jun 29 '14 at 20:17
  • Thanks, that will do, I guess. Btw, what to you mean saying "let the script tell you about entities by value rather than by name"? Suppose I have some entity named "car", for example. I need to load it from script. But I need to know from which file I can load it. So it would be quite handy to iterate through all script files and create std::pair> where first string is name of the file and vector is list of all entities defined in file so I can easily find needed script later. –  Jun 29 '14 at 20:54
  • 1
    You have your script call `RegisterEntity("car", ...)` or `Entity { foo=bar}` or `Car { foo=bar }` or `config("car", ...)` or `conf("car") { foo=bar }` or any of a million other ways you might want to arrange things so that the script tells *you* where the config for a particular entity is, rather than rather than your C++ code digging through objects created by the script looking for it. – Mud Jun 30 '14 at 00:55
  • So I can have one script file which will register all entities and tell C++ where to find them, right? –  Jun 30 '14 at 09:00
  • The script doesn't merely tell C++ were to find the entity, *it passes the entity to C++*. – Mud Jun 30 '14 at 16:26
  • But I also need C++ to know where to find the entity if this particular entity needs to be reloaded, for example –  Jun 30 '14 at 17:00
  • 1
    @winch A script is compiled as a function body. It can return a list of values of any length(s). You just need a convention of what to return: It could be the entity; It could be a function that creates an entity; It could be a table with certain fields. (Those are more of the "million ways.") – Tom Blodget Jul 01 '14 at 05:27