Crash when writing to file via loop or control.lua

Place to get help with not working mods / modding interface.
Post Reply
ironleaf
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jan 25, 2019 7:05 pm
Contact:

Crash when writing to file via loop or control.lua

Post by ironleaf »

Greetings, I am exporting values to text files and I got stuck on consumption numbers...

If I enter this command into the console I will get a text file in my script-output dir with the correct number of iron plates consumed:

Code: Select all

/c game.write_file("iron-plate.txt" , game.player.force.item_production_statistics.get_output_count("iron-plate"))
When I try to run that command in a loop (from the console or control.lua) like this:

Code: Select all

/c script.on_event(defines.events.on_tick, function(event)
   if event.tick % 60*5 == 0 then --every 5 seconds for testing only. Increase to 10 min when working
game.write_file("iron-plate.txt" , game.player.force.item_production_statistics.get_output_count("Iron-plate"))
     end
 end)
I get the following error at the correct tick:

Error while running event level::on_tick (ID 0)
(command):1: attempt to index field 'player' (a nil value)
stack traceback:
(command):1: in function <(command):1>



This method/command structure is working for count.entities.filtered and count.tiles.filtered with no problem using a slightly different command but I can’t seem to get production stats to export.

I’m not a programmer so if someone could post the correct command I should be using I would really appreciate it - the API references aren't helping me, sadly.

Thanks in advance! :D

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by quyxkh »

`game.player` is only defined in console-invoked commands, see its docs. You want `game.players[event.player_index]` in event handlers.

edit: aaaannnd I didn't notice the "on_tick" there, as others point out later ticks aren't player-specific. Sorry for adding yet more blindspotting to the thread.
Last edited by quyxkh on Wed May 06, 2020 9:37 pm, edited 1 time in total.

ironleaf
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jan 25, 2019 7:05 pm
Contact:

Re: Crash when writing to file via loop or control.lua

Post by ironleaf »

Thanks quyxkh,

I revisited this option last night (I did see it show up in various posts previously) but no matter where I put it before, in or around the function I just end up with syntax errors or "unexpected symbol near')'" errors.

I think the closest I came resulted in a new crash with this error:
Bad argument #3 of 3 to '__index' (string expected, got nil)

I’ll try again this evening. Thanks for the suggestion.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by Pi-C »

ironleaf wrote:
Wed May 06, 2020 4:39 pm
I think the closest I came resulted in a new crash with this error:
Bad argument #3 of 3 to '__index' (string expected, got nil)
The problem is, that event.player_index doesn't exist for on_tick. Just try for yourself:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	log("on_tick: " .. serpent.line(event))
end)
This will output

Code: Select all

[…]
on_tick: {name = 0, tick = 12664}
[…]
You could hard-code a player instead:

Code: Select all

local player = game.get_player("Your_player_name")
local player_a = game.players[1]
You could also iterate over all players:

Code: Select all

for _, player in ipairs(game.players) do
	player.print("Some text to print")
end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by DaveMcW »

You don't need a player at all here, just a force.

For example,

Code: Select all

game.forces.player.item_production_statistics

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by Pi-C »

DaveMcW wrote:
Wed May 06, 2020 5:27 pm

Code: Select all

game.forces.player.item_production_statistics
Was that a typo? I think it should be just

Code: Select all

game.forces.item_production_statistics
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by DaveMcW »

https://lua-api.factorio.com/latest/LuaForce.html wrote:Default forces are player, enemy and neutral. Players and mods can create additional forces (up to 64 total).

Pi-C
Smart Inserter
Smart Inserter
Posts: 1639
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Crash when writing to file via loop or control.lua

Post by Pi-C »

Ooops, my bad. Of course you've got to identify the force! I was too focused on properties, so I didn't read it as "game.forces["player"]. :-D
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

posila
Factorio Staff
Factorio Staff
Posts: 5201
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: Crash when writing to file via loop or control.lua

Post by posila »

Pi-C wrote:
Wed May 06, 2020 5:52 pm
Ooops, my bad. Of course you've got to identify the force! I was too focused on properties, so I didn't read it as "game.forces["player"]. :-D
_G["game"]["forces"]["player"]["item_production_statistics"]

ironleaf
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jan 25, 2019 7:05 pm
Contact:

Re: Crash when writing to file via loop or control.lua

Post by ironleaf »

Thanks everyone, you folks are awesome!

For others interested the final working command is:

Code: Select all

script.on_event(defines.events.on_tick, function(event)   
   if event.tick % 60*5 == 0 then 
      game.write_file("iron-plate.txt", game.forces.player.item_production_statistics.get_output_count("iron-plate"))
   end
end)
I just need to workout exporting energy stats and this may become a full blown mod - I'm a long way away from just tracking total entities on the map via PRTG :)

Post Reply

Return to “Modding help”