12
\$\begingroup\$

I'm writing a Factorio mod, and I'm trying to figure out why it doesn't work. Can I do some kind of error / debug logging somewhere? Both manual (printing variables) and simple error reporting (accessing a property that doesn't exist) would be great. I know about the player.print method, but I can only get it to print string literals, not objects:

script.on_event(defines.events.on_player_created, function(event)
    pcall(function()
        local player = game.get_player(event.player_index)
        player.print("Hi player") -- works
        player.print(player) -- does not work. Any error message would be nice
    end)
end)

I've also tried the log file in the application folder, and the various debug modes (F5/F6 in game), but so far haven't found anything useful.

\$\endgroup\$
1
  • \$\begingroup\$ Perhaps you're assuming it will automatically generate a string from the object? You might need to create and explicitly use some kind of ToString() method. \$\endgroup\$
    – House
    May 1, 2016 at 20:47

3 Answers 3

3
\$\begingroup\$

According to this forum post

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Sounds good, but it doesn't quite work. Running factorio.exe from the command prompt starts the game, but returns immediately and I only see the output of the first log line. Using start factorio.exe /wait doesn't work either, because it only opens a console window and doesn't start the game at all. \$\endgroup\$
    – Jorn
    May 1, 2016 at 19:03
1
\$\begingroup\$

You can use the error() function which sends to factorio-current.log

For example:

error("Hi player")

or

error(serpent_block(table_name))

\$\endgroup\$
2
  • \$\begingroup\$ serpent_block --> serpent.block \$\endgroup\$ Feb 12, 2019 at 19:45
  • \$\begingroup\$ It actually shows an "error" popup with the message at the game loading and prevents the mod to be loaded. \$\endgroup\$ Feb 12, 2019 at 19:47
0
\$\begingroup\$

There is a write_file method in LuaGameScript.

game.write_file("mylog.log", serpent_block(table_name)) -- normal write
game.write_file("mylog.log", serpent_block(table_name), true) -- appending

(http://lua-api.factorio.com/0.15.23/LuaGameScript.html#LuaGameScript.write_file)

The file will end up in a folder called script-output under %APPDATA%/Factorio, next to your scenarios folder.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Does not seem to work from data.lua \$\endgroup\$
    – LOST
    Aug 28, 2017 at 0:25
  • 1
    \$\begingroup\$ data.lua is not a normal runtime script. Its purpose is to describe a data structure and nothing else. Most(all?) runtime objects are not accessible during it's interpretation. In particular the global LuaGameScript object called game does not even exist then. \$\endgroup\$ Sep 3, 2017 at 15:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .