[0.17.42] pcall protects other mods event handlers

Bugs that are actually features.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

[0.17.42] pcall protects other mods event handlers

Post 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 685 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.
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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 16099
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.42] pcall protects other mods event handlers

Post 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.
If you want to get ahold of me I'm almost always on Discord.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [0.17.42] pcall protects other mods event handlers

Post 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.
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 “Not a bug”