Page 1 of 1

Reflection/Introspection

Posted: Sun Apr 08, 2018 10:25 pm
by kfsone
Q: How do I inspect Lua objects in-game?

Code: Select all

print(data.raw.item["roboport"])
The last time I worked in Lua on a game, I made myself a repl that let me live inspect things. So far the only Lua access I've found in Factorio is the ~ console via the /c command, and that doesn't provide access to the "data" object. If there's no in-game access to it, is there a recommended strategy for doing something like this during a module startup.

Specifically: I'm trying to replace the texture filename of the roboport entity and

Code: Select all

data:extend({ 
  ...
  {
    type="roboport",
    name="roboport",
    ...
    base = {
      layers = {
        {
          filename = "...",
        }
    }
  }
})
doesn't doesn't produce a

Code: Select all

data.raw.item["roboport"].base
(let alone base.layers[0].filename).

What's the best way to:

- do something equivalent to Python's "dir(data.raw)" or "data.raw.keys()"?
- output that? log? print?

Re: Reflection/Introspection

Posted: Mon Apr 09, 2018 12:48 am
by Bilka
You have to log() it in the data phase. See this mod for an example of that: https://mods.factorio.com/mod/DataRawSerpent

Re: Reflection/Introspection

Posted: Mon Apr 09, 2018 7:20 am
by darkfrei
Bilka wrote:You have to log() it in the data phase. See this mod for an example of that: https://mods.factorio.com/mod/DataRawSerpent
Same mod for 0.16 viewtopic.php?f=135&t=45107#p324171

Re: Reflection/Introspection

Posted: Mon Apr 09, 2018 8:22 am
by Bilka
darkfrei wrote:
Bilka wrote:You have to log() it in the data phase. See this mod for an example of that: https://mods.factorio.com/mod/DataRawSerpent
Same mod for 0.16 viewtopic.php?f=135&t=45107#p324171
Execpt yours is more complicated which isnt really suited for a beginner :) The mod I linked also works in 0.16 if OP just grabs the data.lua for their own mod.

Re: Reflection/Introspection

Posted: Mon Apr 09, 2018 9:39 pm
by kfsone
Thank you, took me a few cycles to work out I had access to serpent. Between not having done Lua in earnest for a few years and no repl I'm stumbling on every quirk of lua... 1 based arrays, indeed.

Re: Reflection/Introspection

Posted: Tue Apr 10, 2018 11:06 am
by eradicator
kfsone wrote:Thank you, took me a few cycles to work out I had access to serpent. Between not having done Lua in earnest for a few years and no repl I'm stumbling on every quirk of lua... 1 based arrays, indeed.
Protip for later: Serpent is much less useful in the control.lua stage than in the data.lua stage, because the userdata objects returned by most API functions can not be introspected with serpent.