Is the LUA console capable of reflection?

Place to get help with not working mods / modding interface.
BinaryMan
Inserter
Inserter
Posts: 21
Joined: Mon Mar 24, 2014 12:02 am
Contact:

Is the LUA console capable of reflection?

Post 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?
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Is the LUA console capable of reflection?

Post 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.
BinaryMan
Inserter
Inserter
Posts: 21
Joined: Mon Mar 24, 2014 12:02 am
Contact:

Re: Is the LUA console capable of reflection?

Post 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.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Is the LUA console capable of reflection?

Post 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
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Rseding91
Factorio Staff
Factorio Staff
Posts: 14301
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Is the LUA console capable of reflection?

Post by Rseding91 »

If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”