Page 1 of 1

Debugging utilities built in to Factorio

Posted: Sun Apr 17, 2016 8:10 pm
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?

Re: Debugging utilities built in to Factorio

Posted: Sun Apr 17, 2016 8:22 pm
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 )) ).

Re: Debugging utilities built in to Factorio

Posted: Sun Apr 17, 2016 8:57 pm
by prg
Another useful function is debug.traceback() to figure out how the hell you ended up somewhere.

Re: Debugging utilities built in to Factorio

Posted: Sun Apr 17, 2016 9:37 pm
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?

Re: Debugging utilities built in to Factorio

Posted: Sun Apr 17, 2016 10:26 pm
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.

Re: Debugging utilities built in to Factorio

Posted: Sun May 01, 2016 7:11 pm
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.

Re: Debugging utilities built in to Factorio

Posted: Sun May 01, 2016 7:14 pm
by prg
Is that using Steam? Try the DRM free version, that should stay attached to the terminal.

Re: Debugging utilities built in to Factorio

Posted: Sun May 01, 2016 7:31 pm
by Jorn86
I should have figured Steam was the problem :)

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

Re: Debugging utilities built in to Factorio

Posted: Sat Oct 01, 2016 11:47 am
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%

Re: Debugging utilities built in to Factorio

Posted: Sat Oct 01, 2016 11:55 am
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

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:25 pm
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...

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:32 pm
by prg
Try serpent.block() instead of serpent.dump().

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:37 pm
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

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:45 pm
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]]

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:49 pm
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

Re: Debugging utilities built in to Factorio

Posted: Fri Oct 07, 2016 7:55 pm
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: