Raised event seems to bypass filter

Place to get help with not working mods / modding interface.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Raised event seems to bypass filter

Post by Pi-C »

This bug that manifests itself in the Rocket-Silo Construction mod seems to be caused by one of my mods.

GCKI listens for on_built_entity and makes use of filtering by entity name:

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
GCKI.dprint("Entered event script on_built_entity for player " .. game.players[event.player_index].name)

    local index = event.player_index
    local player = game.players[index]
    local entity = event.created_entity

GCKI.show(player.name, "player.name")
GCKI.show(entity.name, "entity.name")

[…]

    if entity.destroy() then GCKI.dprint("Destroyed entity") end

    GCKI.dprint("End of event script on_built_entity for player " .. game.players[event.player_index].name)
end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})
The other mod raises an event after each stage of the rocket silo has been completed:

Code: Select all

script.raise_event(defines.events.on_built_entity, {created_entity = new_stage, player_index = player_index, stack = stack})
Due to the filtering, this should not interfere with my mod, but apparently it does:

Code: Select all

 151.072 Script @__GCKI__/common.lua:160: End of event script on_player_joined_game for player Pi-C
 218.621 Script @__GCKI__/common.lua:160: Entered event script on_built_entity for player Pi-C
 218.622 Script @__GCKI__/common.lua:160: Show player.name:	|Pi-C| (string)
 218.622 Script @__GCKI__/common.lua:160: Show entity.name:	|rsc-silo-stage2| (string)
 218.622 Script @__GCKI__/common.lua:160: Entered function is_locked_vehicle(car)
 218.622 Script @__GCKI__/common.lua:160: This Car is unlocked.
 218.622 Script @__GCKI__/common.lua:160: Show locker:	|nil| (nil)
 218.622 Script @__GCKI__/common.lua:160: End of function is_locked_vehicle(car)
 218.622 Script @__GCKI__/common.lua:160: Can't place Car at selected location, try another one!
 218.622 Script @__GCKI__/common.lua:160: Destroyed entity
 218.622 Script @__GCKI__/common.lua:160: End of event script on_built_entity for player Pi-C
Does anybody have an idea what's going on there?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: Raised event seems to bypass filter

Post by PyroFire »

You're also missing some event parameters.


https://lua-api.factorio.com/latest/index.html#Events

Code: Select all


event.name=defines.events[event]
event.tick=game.tick

Secondly, you've set up your filters incorrectly.

Code: Select all

script.on_event(function() end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})
This is just nonsense.

It's asking for this.

Code: Select all

script.on_event(function() end, { {filter = "name", name = "car-key"} })

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

Re: Raised event seems to bypass filter

Post by Bilka »

Pi-C wrote:
Sun Nov 17, 2019 8:13 am
Raised event seems to bypass filter

Does anybody have an idea what's going on there?
Yup, that's just how it is currently. You have to keep your own Lua-based filtering code around in the event so that other mods raising events doesn't break things. That's why viewtopic.php?f=34&t=77550 was proposed. And that event behaviour is also what prompted my reply of "garbage system" here: viewtopic.php?p=464821#p464821; I ran into the exact same thing in my mod. I hate how it works - but the points brought up in the thread against the proposal are valid and there is currently no good solution, so the proposal is "left to be".

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
....
end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})
That "LuaPlayerBuiltEntityEventFilters = " shouldn't be there, it expects an array, not a dictionary. But due to how we parse arrays internally, your code still works.
PyroFire wrote:
Sun Nov 17, 2019 8:37 am
You're also missing some event parameters.


https://lua-api.factorio.com/latest/index.html#Events

Code: Select all


event.name=defines.events[event]
event.tick=game.tick

The other mod would be missing them. And it's not, the game adds those automatically to the event when it is given to lua, so they aren't missing at all.
PyroFire wrote:
Sun Nov 17, 2019 8:37 am
Secondly, you've set up your filters incorrectly.

Code: Select all

script.on_event(function() end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})
This is just nonsense.

It's asking for this.

Code: Select all

script.on_event(function() end, { {filter = "name", name = "car-key"} })
As I said above, using named keys is wrong, but it still works.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: Raised event seems to bypass filter

Post by Pi-C »

PyroFire wrote:
Sun Nov 17, 2019 8:37 am
You're also missing some event parameters.


https://lua-api.factorio.com/latest/index.html#Events

Code: Select all

event.name=defines.events[event]
event.tick=game.tick
Sorry, but I don't see how these should be relevant. I'm not using event.name and event.tick in my script, so why should I set them?
Secondly, you've set up your filters incorrectly.

Code: Select all

script.on_event(function() end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})
This is just nonsense.
Makes sense. I wasn't sure that I've got the syntax right. Tested the event handler by building different entities and it seemed to work, though: Before this bug occurred, the event would only be triggered when the car-keys were "built", while nothing would be done for other entities.
It's asking for this.

Code: Select all

script.on_event(function() end, { {filter = "name", name = "car-key"} })
I'll give it a try, thanks for the code snippet!
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

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

Re: Raised event seems to bypass filter

Post by Pi-C »

Bilka wrote:
Sun Nov 17, 2019 9:15 am
Pi-C wrote:
Sun Nov 17, 2019 8:13 am
Raised event seems to bypass filter

Does anybody have an idea what's going on there?
Yup, that's just how it is currently. You have to keep your own Lua-based filtering code around in the event so that other mods raising events doesn't break things. That's why viewtopic.php?f=34&t=77550 was proposed. And that event behaviour is also what prompted my reply of "garbage system" here: viewtopic.php?p=464821#p464821; I ran into the exact same thing in my mod. I hate how it works - but the points brought up in the thread against the proposal are valid and there is currently no good solution, so the proposal is "left to be".
So event filtering actually is useless (at the moment) because I have to include the code I just removed (for checking which entity triggered the event) again? Well, at least it's an easy fix for the problem! :-)

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
....
end, {LuaPlayerBuiltEntityEventFilters = {filter = "name", name = "car-key"}})

That "LuaPlayerBuiltEntityEventFilters = " shouldn't be there, it expects an array, not a dictionary. But due to how we parse arrays internally, your code still works.
I already suspected that I didn't use the correct syntax. But event filtering was something that had just been introduced, so I couldn't look at how other people used it in their mods. You see, I'm not a professional programmer; I'm willing to learn and read documentations, but sometimes I still do stupid mistakes for lack of better knowledge. Having the "technical" documentation is useful, but it still is kind of abstract. Some real examples of how to use things in actual code would be quite helpful -- and I'm quite sure that I'm not the only one who would benefit from this! Just a suggestion -- and thanks a lot for your help! :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

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

Re: Raised event seems to bypass filter

Post by Bilka »

Since this is a google search result, I want to add the note that raise event no longer bypasses event filters. These changes happened in 0.18.27: viewtopic.php?p=495226#p495226
Happy event filtering :)
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: Raised event seems to bypass filter

Post by Pi-C »

Bilka wrote:
Wed Aug 26, 2020 2:43 pm
Since this is a google search result, I want to add the note that raise event no longer bypasses event filters. These changes happened in 0.18.27: viewtopic.php?p=495226#p495226
Happy event filtering :)
Thanks for updating this! I've been aware of the new behavior before, so my mods contained event filtering for Factorio versions that supported it, and code in the called functions to do the filtering for earlier versions. (Kinda redundant because modern versions would run both filters -- but they'd still benefit by filtering out many event calls before they could even make it to those functions.) Still need to remove cruft code for the 1.0 versions, but I'll get there finally, and I'm really looking forward to see performance skyrocketing! :-D
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”