Is the LUA console capable of reflection?
Posted: Mon Sep 21, 2015 9:43 pm
I come from a C# background and I'm trying to figure out how to spit out a list of objects, or for a single object a list of name/value pairs for attributes and functions of the object, using the in-game LUA console (which would be faster than editing scripts and restarting each time I want to try something). Example:
there is a "game" object. It has many things including "player", "forces", "surfaces". If I can output a list of objects I might then examine "surfaces" as a list of objects: one of the array list items is "nauvis" (pretend I didn't know that in advance, how would I discover it?). Now maybe I want to list the data and functions so if there is a list of function names ("create_entity" is one that surface supports), but I don't know anything about surface data. An x/y grid, a series of chunks, how would you know?
for key,value in pairs(game) do
p("found member " .. key);
end
Well it says "found member _ _ self" which is ... not helpful.
for key,value in pairs(game.surfaces) => found "nauvis". OK so that's at least giving a list.
for key,value in pairs(game.surfaces.nauvis) => again "_ _ self" which is type userdata (which won't print out and I don't know what it is or how to query it).
I mean, if it can be done in script using something recursive and spit out to file that's OK too since it would reveal the data, but I'd prefer to test and debug in the console if possible. I don't think Factorio console supports all the LUA commands that I'm seeing when I look at LUA guides. How do I expose the objects more so I can see?
there is a "game" object. It has many things including "player", "forces", "surfaces". If I can output a list of objects I might then examine "surfaces" as a list of objects: one of the array list items is "nauvis" (pretend I didn't know that in advance, how would I discover it?). Now maybe I want to list the data and functions so if there is a list of function names ("create_entity" is one that surface supports), but I don't know anything about surface data. An x/y grid, a series of chunks, how would you know?
for key,value in pairs(game) do
p("found member " .. key);
end
Well it says "found member _ _ self" which is ... not helpful.
for key,value in pairs(game.surfaces) => found "nauvis". OK so that's at least giving a list.
for key,value in pairs(game.surfaces.nauvis) => again "_ _ self" which is type userdata (which won't print out and I don't know what it is or how to query it).
I mean, if it can be done in script using something recursive and spit out to file that's OK too since it would reveal the data, but I'd prefer to test and debug in the console if possible. I don't think Factorio console supports all the LUA commands that I'm seeing when I look at LUA guides. How do I expose the objects more so I can see?