How Do Lua States Work in Firetuner?

Snowden42

Chieftain
Joined
Nov 4, 2016
Messages
5
I'm a Software Engineer professionally, and I have a background in Python so I'm familiar with scripting languages like Lua, but I'm completely at a loss as to how Lua States work. From my limited understanding of Lua, everything is a table--Ok, that's fine, I guess you could consider a class a table of bindings and functions, I buy that.

So then it follows that a Lua state is essentially just a table? Can I think of these as instances, in an Object-Oriented sense? Also, I'm having trouble performing any introspection on Lua states. For example, the InGameTopOptionsMenu references Controls (a table I think?). But printing the key value pairs of the table gives me nothing meaningful (I think nothing at all, to be honest). It seems like Lua states are basically contexts, but I can't for the life of my tell how these variables are injected (like Controls, UI, Events).

Any info would be helpful in this regard--It seems like there's a bit of a lack of info on the Lua side of things at the moment.
 
What do you mean by lua state exactly?
Luastate is the name of the C binding which lets you access the lua virtual machine. Is it what you're talking about? If not, I suggest finding another word because you're just going to confuse anyone who works with actual luastates.

You can think of lua as a set of tables. One table is _G, which is the "global" namespace. Civ probably prevents you from accessing it as a table, however (civ 5 did).
These tables can have a metatable, which holds a set of functions, including some functions telling the table how to answer when you're requesting or setting a key.
Using metatables, you can have zero keys/values in your table but still think it has.
For instance here's a "delegate":

local table1 = {somekey="xxx"}
local table2 = {}
local meta = { __index = function(self,k) return table1.k end}
setmetatable(table2,meta)

Now table2 will behave as if it were table1, but if you request ipairs or pairs on it, it should look empty. (Note I didn't run it, but this should give you an idea).
So table2.somekey should be "xxx" but introspecting table2 won't find it.

The binding between C and lua can be done in many ways. It's possible to take a function pointer in C, put it in a table in lua and make it behave as a lua function through the metatable. In that case, the function would appear in thepairs() of the table.
But you could also say C functions are tables and call them with _call (that'd be weird but whatever), or the __index metamethod could look into the C code to return a lua function. It could, in theory, even create it on the flight.
So, I don't know how they implemented the binding between lua and C but there is no guarantee that you can introspect methods at all.
 
Back
Top Bottom