Page 1 of 1

[0.17.42] pcall protects other mods event handlers

Posted: Thu Jun 06, 2019 1:45 pm
by eradicator
What?

pcall(some_function) will protect event handlers of other mods than the pcall'ing mod if some_function triggers an event.

Reproduction

Just start a new game and see the error being printed.
pcall.png
pcall.png (267.72 KiB) Viewed 686 times
Test-mod-AB.7z
(683 Bytes) Downloaded 32 times
Test Mod A:

Code: Select all

script.on_event(defines.events.on_player_created,function(e)
  local A,B = pcall(function() game.players[e.player_index].force = 'neutral' end)
  game.print('A:'..tostring(A))
  game.print('B:'..tostring(B))
  end)
Test Mod B:

Code: Select all

script.on_event(defines.events.on_player_changed_force,function(e)
  error('This should not be protected?.')
  end)
Expected Behavior

I'm not even sure this is really a bug. It just *feels* wrong. I kinda expected pcall to only protect code inside my own mod.

Re: [0.17.42] pcall protects other mods event handlers

Posted: Thu Jun 06, 2019 2:58 pm
by Rseding91
Thanks for the report however that's what pcall does: it traps all errors that result from code executed inside of the pcall.

Unless you're parsing input from the player you never want to use pcall as it will just hide bugs in your code. If that 1 scenario didn't exist I would disallow the use of pcall in mods.

Re: [0.17.42] pcall protects other mods event handlers

Posted: Thu Jun 06, 2019 3:25 pm
by eradicator
Rseding91 wrote: Thu Jun 06, 2019 2:58 pm Unless you're parsing input from the player
That is exactly what i was doing. It's my "/sudo" mod. I was just suprised that it catches event handlers in *other* mods. Thanks for confirming it's not a bug.