Page 1 of 1

Is the LUA console capable of reflection?

Posted: Mon Sep 21, 2015 9:43 pm
by BinaryMan
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?

Re: Is the LUA console capable of reflection?

Posted: Mon Sep 21, 2015 10:23 pm
by Choumiko
BinaryMan wrote:be faster than editing scripts and restarting each time I want to try something).
If you only change control.lua reloading a save is enough.

As for reflection: no clue. Most (if not all) userdata types from Factorio have a help() method (/c game.player.print(game.help()) ) which at least lists values/methods. Note that some values may not be valid for certain types (e.g. calling /c game.player.print(game.player.selected.friction_modifier) when mousing over a mining drill gives an error, though it appears in help() for a mining drill.

Re: Is the LUA console capable of reflection?

Posted: Tue Sep 22, 2015 12:01 am
by BinaryMan
I tried in the data-final-fixes.lua :

Code: Select all

require("config")

for key, value in pairs(data.raw.resources) do
	table.insert(log, value);
end

and in the config.lua:

if not log then log = {} end

if game then
	for key, value in pairs(log) do
		game.makefile(key, value);
	end
end
Just because I have been able to get makefile to work, but I think the log[] array is completely deleted by the time config comes around per https://forums.factorio.com/wiki/inde ... _Lifecycle . And according to other forum posts, there seems to be no way to log values in data.raw for exploratory purposes (as print() does nothing). I'm not sure there is anything else I can do to uncover the data.

Re: Is the LUA console capable of reflection?

Posted: Tue Sep 22, 2015 6:49 am
by prg
print doesn't do nothing, it works just fine for me. The output goes to stdout so you need to run the game in a terminal so you can see it or redirect it to a file.

Also, if you want to get at the resources from control.lua, you can do something like

Code: Select all

for k, v in pairs(game.entity_prototypes) do
    if v.type == "resource" then
        ...
    end
end

Re: Is the LUA console capable of reflection?

Posted: Tue Sep 22, 2015 6:50 am
by Rseding91