Debugging utilities built in to Factorio

Place to get help with not working mods / modding interface.
Post Reply
obstinate
Inserter
Inserter
Posts: 45
Joined: Sun Aug 16, 2015 7:25 am
Contact:

Debugging utilities built in to Factorio

Post by obstinate »

I'm wondering what debugging modules are built into Factorio. I know about game.player.print. Are there any other facilities? For example, there are pretty printers that will print tables to strings. Is such a printer built into Factorio?

Also, are there any better alternatives than the game console? For example, a window that can persist over gameplay?

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by Adil »

You can launch the game from console, so standard print function would send its output there. You can redirect that output to files.
There is serpent pretty printer, which can print lua tables ( syntax is print( serpent.block( arg )) ).
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Debugging utilities built in to Factorio

Post by prg »

Another useful function is debug.traceback() to figure out how the hell you ended up somewhere.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

obstinate
Inserter
Inserter
Posts: 45
Joined: Sun Aug 16, 2015 7:25 am
Contact:

Re: Debugging utilities built in to Factorio

Post by obstinate »

Adil wrote:You can launch the game from console, so standard print function would send its output there. You can redirect that output to files.
There is serpent pretty printer, which can print lua tables ( syntax is print( serpent.block( arg )) ).
This is great, thanks. So I can put the game in windowed mode and look at the terminal rather than having to use the in-game console for debugging. Fantastic.

Is there any hot-swapping of mod code?

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by Adil »

obstinate wrote: Is there any hot-swapping of mod code?
control.lua is reloaded with loading save or map restart. data.lua is only reloaded on game restart.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Jorn86
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sun May 01, 2016 7:08 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by Jorn86 »

How do I exactly run Factorio from the console using Windows? Just running

Code: Select all

factorio.exe
will start the game, but return immediately so I don't see any output. Running it using

Code: Select all

start factorio.exe /wait
doesn't start the game at all. Redirecting output to a file like

Code: Select all

factorio.exe > log.txt
doesn't help, as the file remains empty.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Debugging utilities built in to Factorio

Post by prg »

Is that using Steam? Try the DRM free version, that should stay attached to the terminal.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

Jorn86
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sun May 01, 2016 7:08 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by Jorn86 »

I should have figured Steam was the problem :)

The direct downloaded version does show me the output, including serpent-ed tables. Thanks!

White-Tiger
Burner Inserter
Burner Inserter
Posts: 6
Joined: Sat Oct 01, 2016 11:01 am
Contact:

Re: Debugging utilities built in to Factorio

Post by White-Tiger »

Just in case someone finds this through Google...
It's possible to keep using the Steam version and still have a console.
Use "SET LAUNCH OPTIONS..." from Factorio's properties in Steam and set them to:

Code: Select all

cmd /c %command%

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by aubergine18 »

Don't forget the `log()` function available to lua throughout the entire script lifecycle - dumps stuff to game log. It takes single argument (as far as I know) - a string.

For example, in data-final-fixes.lua:

Code: Select all

log( serpent.block( data.raw, {comment = false, numformat = '%1.8g' } ) )
Serpent docs: https://github.com/pkulchenko/serpent
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

matjojo
Filter Inserter
Filter Inserter
Posts: 336
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by matjojo »

aubergine18 wrote:Don't forget the `log()` function available to lua throughout the entire script lifecycle - dumps stuff to game log. It takes single argument (as far as I know) - a string.

For example, in data-final-fixes.lua:

Code: Select all

log( serpent.block( data.raw, {comment = false, numformat = '%1.8g' } ) )
Serpent docs: https://github.com/pkulchenko/serpent
so eeuhm, I'm using serpent.dump, to help check my data integrity, but all my ouptuts looks like this:

Code: Select all

do local _={[1]={name="teamname",players={}}};return _;end
looks like it's outputting: do local _= (the actual output);return _;end


is this also the case for you, or am I seeing a bug?

EDIT: the grammar made sense in dutch...
Last edited by matjojo on Fri Oct 07, 2016 7:33 pm, edited 1 time in total.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Debugging utilities built in to Factorio

Post by prg »

Try serpent.block() instead of serpent.dump().
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

matjojo
Filter Inserter
Filter Inserter
Posts: 336
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by matjojo »

prg wrote:Try serpent.block() instead of serpent.dump().
serpent.block() does not have the 'do local' part in front and behind it, but it has the added problem that it is designed to not show tables in tables, which I need it to do

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Debugging utilities built in to Factorio

Post by prg »

matjojo wrote:serpent.block() does not have the 'do local' part in front and behind it, but it has the added problem that it is designed to not show tables in tables, which I need it to do
Huh?

Code: Select all

/c print(serpent.block({1,2,{3,4,{5,6}}}))

Code: Select all

{
  1,
  2,
  {
    3,
    4,
    {
      5,
      6
    } --[[table: 0x7f12d8873aa0]]
  } --[[table: 0x7f12d8873a40]]
} --[[table: 0x7f12d88739e0]]
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by aubergine18 »

Here's what I use

Code: Select all

local logFormat = {comment = false, numformat = '%1.8g' }

log( serpent.block( someObject, logFormat ) )
Example: In my data.lua, I logged `data.raw` to get this: https://raw.githubusercontent.com/auber ... r/data.raw
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

matjojo
Filter Inserter
Filter Inserter
Posts: 336
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

Re: Debugging utilities built in to Factorio

Post by matjojo »

prg wrote:
matjojo wrote:serpent.block() does not have the 'do local' part in front and behind it, but it has the added problem that it is designed to not show tables in tables, which I need it to do
Huh?

Code: Select all

/c print(serpent.block({1,2,{3,4,{5,6}}}))

Code: Select all

{
  1,
  2,
  {
    3,
    4,
    {
      5,
      6
    } --[[table: 0x7f12d8873aa0]]
  } --[[table: 0x7f12d8873a40]]
} --[[table: 0x7f12d88739e0]]

yeah, I'm sorry. the table in the table I used was empty, I forgot -_- :oops:

Post Reply

Return to “Modding help”