[Solved] player index got nil in event on_marked_for_deconstruction

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

[Solved] player index got nil in event on_marked_for_deconstruction

Post by WIZ4 »

Hi, I wrote a script to remove the deconstruction markers if the player has played less than 4 hours on my server.

Code: Select all

function not_deconstruct(event)
  local player = game.players[event.player_index]
  if player.online_time > 60*60*60*4 then return end
  player.clean_cursor()
  event.entity.cancel_deconstruction(player.force)
  player.print{"msg.not_deconstruct"}
end

Code: Select all

script.on_event(defines.events.on_marked_for_deconstruction, function(event)
  not_deconstruct(event)
end)
And the players randomly receive this error when they use the deconstruction plan:
Screenshot_6.jpg
Screenshot_6.jpg (288.07 KiB) Viewed 1989 times
control.lua:110 = local player = game.players[event.player_index]
I can not reproduce this error, and not understand why player_index = nil. Do you have any guesswork?
No mods
Last edited by WIZ4 on Fri Oct 05, 2018 8:06 pm, edited 1 time in total.
My native language is russian. Sorry if my messages are difficult to read.

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

Re: player index got nil in event on_marked_for_deconstruction

Post by eradicator »

Code: Select all

on_marked_for_deconstruction

Called when an entity is marked for deconstruction with the Deconstruction planner or via script.

Contains
entity :: LuaEntity
player_index :: uint (optional)
The doc clearly states that player_index is optional. So you need to check if it actually exists.
I have no idea in what situation your players manage to cause this though if you're planning "without mods". Is your scenario marking things for deconstruction? Which line is 110?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: player index got nil in event on_marked_for_deconstruction

Post by WIZ4 »

eradicator wrote:
Mon Oct 01, 2018 5:48 pm

Code: Select all

on_marked_for_deconstruction

Called when an entity is marked for deconstruction with the Deconstruction planner or via script.

Contains
entity :: LuaEntity
player_index :: uint (optional)
The doc clearly states that player_index is optional. So you need to check if it actually exists.
I have no idea in what situation your players manage to cause this though if you're planning "without mods". Is your scenario marking things for deconstruction? Which line is 110?
control.lua:110 local player = game.players[event.player_index]
I no longer have scripts for marked buildings
My native language is russian. Sorry if my messages are difficult to read.


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

Re: player index got nil in event on_marked_for_deconstruction

Post by eradicator »

darkfrei wrote:
Tue Oct 02, 2018 6:14 am
Why argument #2?
Because the first argument to all metafunctions is the object itself.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: player index got nil in event on_marked_for_deconstruction

Post by WIZ4 »

The problem was that coal falling out of the rock was automatically marked by deconstruction. And the owner of the marker was not a player.
xr2vsoNrSok.jpg
xr2vsoNrSok.jpg (12.26 KiB) Viewed 1922 times
My native language is russian. Sorry if my messages are difficult to read.

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

Re: player index got nil in event on_marked_for_deconstruction

Post by eradicator »

WIZ4 wrote:
Fri Oct 05, 2018 8:06 pm
The problem was that coal falling out of the rock was automatically marked by deconstruction. And the owner of the marker was not a player.xr2vsoNrSok.jpg

Not a vanilla feature. So it must be your own scenario. If it's an on_died handler or something you can still fix it by adding the player_index.
(Edit: Wrong.)
Last edited by eradicator on Sat Oct 06, 2018 8:37 am, edited 2 times in total.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: player index got nil in event on_marked_for_deconstruction

Post by Bilka »

eradicator wrote:
Sat Oct 06, 2018 7:18 am
WIZ4 wrote:
Fri Oct 05, 2018 8:06 pm
The problem was that coal falling out of the rock was automatically marked by deconstruction. And the owner of the marker was not a player.xr2vsoNrSok.jpg
Not a vanilla feature. So it must be your own scenario. If it's an on_died handler or something you can still fix it by adding the player_index.
If you mark a rock with coal in it for deconstruction, a robot will come, remove the rock and pick up the stone. The coal is dropped and marked for deconstruction. This is vanilla behavior.
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
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: player index got nil in event on_marked_for_deconstruction

Post by eradicator »

Bilka wrote:
Sat Oct 06, 2018 7:44 am
If you mark a rock with coal in it for deconstruction, a robot will come, remove the rock and pick up the stone. The coal is dropped and marked for deconstruction. This is vanilla behavior.
Apparently it's been too long since i last robo-mined stone-rock. At least now i know why player_index is optional there. If that's the only vanilla situation where it's optional it should be safe for op to ignore events without player_index.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”