Page 1 of 1

[Solved] player index got nil in event on_marked_for_deconstruction

Posted: Mon Oct 01, 2018 5:10 pm
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 2338 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

Re: player index got nil in event on_marked_for_deconstruction

Posted: Mon Oct 01, 2018 5:48 pm
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?

Re: player index got nil in event on_marked_for_deconstruction

Posted: Mon Oct 01, 2018 6:02 pm
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

Re: player index got nil in event on_marked_for_deconstruction

Posted: Tue Oct 02, 2018 6:14 am
by darkfrei
Why argument #2?

Re: player index got nil in event on_marked_for_deconstruction

Posted: Tue Oct 02, 2018 9:16 am
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.

Re: player index got nil in event on_marked_for_deconstruction

Posted: Fri Oct 05, 2018 8:06 pm
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 2271 times

Re: player index got nil in event on_marked_for_deconstruction

Posted: Sat Oct 06, 2018 7:18 am
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.)

Re: player index got nil in event on_marked_for_deconstruction

Posted: Sat Oct 06, 2018 7:44 am
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.

Re: player index got nil in event on_marked_for_deconstruction

Posted: Sat Oct 06, 2018 8:37 am
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.