Reflection/Introspection

Place to get help with not working mods / modding interface.
Post Reply
kfsone
Inserter
Inserter
Posts: 21
Joined: Sun Apr 08, 2018 9:59 pm
Contact:

Reflection/Introspection

Post 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?
"Worst thing about being a game dev is the dev-mode switch when you spot a stray pixel/server lag right as your guild starts the boss fight, your team engages the enemy, or whenever it's least inconvenient. And you're dead."

Bilka
Factorio Staff
Factorio Staff
Posts: 3159
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Reflection/Introspection

Post 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
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2904
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Reflection/Introspection

Post 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

Bilka
Factorio Staff
Factorio Staff
Posts: 3159
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Reflection/Introspection

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

kfsone
Inserter
Inserter
Posts: 21
Joined: Sun Apr 08, 2018 9:59 pm
Contact:

Re: Reflection/Introspection

Post 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.
"Worst thing about being a game dev is the dev-mode switch when you spot a stray pixel/server lag right as your guild starts the boss fight, your team engages the enemy, or whenever it's least inconvenient. And you're dead."

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Reflection/Introspection

Post 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.

Post Reply

Return to “Modding help”