[1.1.76] Error when passing nil as event handler filter for custom inputs

This subforum contains all the issues which we already resolved.
User avatar
BiterUnion
Inserter
Inserter
Posts: 21
Joined: Fri Aug 19, 2022 7:58 am
Contact:

[1.1.76] Error when passing nil as event handler filter for custom inputs

Post by BiterUnion »

When passing `nil` as an event handler filter to `script.on_event` for custom inputs, an error "Filters can only be used when registering single events." is raised, e.g.:

Code: Select all

script.on_event('test-custom-input', function(event) end, nil)  -- raises error
Not passing `nil` as a filter, does not raise the error:

Code: Select all

script.on_event('test-custom-input', function(event) end)  -- doesn't raise error
I know that custom inputs don't support filters and you may have a reason for handling it this way, but I think passing `nil` should be handled the same as passing no filter. This would be beneficial for general event handling logic, e.g. when iterating over a dict mapping event names to filters and calling `script.on_event` for these pairs.
Attachments
factorio-current.log
(9.98 KiB) Downloaded 79 times
curiosity
Filter Inserter
Filter Inserter
Posts: 478
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by curiosity »

This is all over the API. The number of arguments matters.

edit: for the described use it is easily circumvented as such:

Code: Select all

function register_event_handler(event, handler, event_filters)
    if event_filters then
        script.on_event(event, handler, event_filters)
    else
        script.on_event(event, handler)
    end
end
User avatar
BiterUnion
Inserter
Inserter
Posts: 21
Joined: Fri Aug 19, 2022 7:58 am
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by BiterUnion »

curiosity wrote: Mon Feb 20, 2023 11:04 am edit: for the described use it is easily circumvented as such:
...
This would not work in case I want to set the filter to nil for events that do support filters.

There absolutely are work-arounds and this bug is nothing serious, I just think it would be cleaner to handle nil and no argument the same. At the very least the error message could be clearer.
curiosity
Filter Inserter
Filter Inserter
Posts: 478
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by curiosity »

Actually, in this specific case it will work, because the nil is not meaningful. Passing nil for filters is the same as passing nothing.

But generally, IIRC, there are some meaningful nils out there, so your proposal can only be applied selectively. At which point I'd say that consistency is more valuable: better all API functions behave in the same, if unusual, way than only some of them, with no obvious pattern.
User avatar
BiterUnion
Inserter
Inserter
Posts: 21
Joined: Fri Aug 19, 2022 7:58 am
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by BiterUnion »

curiosity wrote: Mon Feb 20, 2023 11:36 am Actually, in this specific case it will work, because the nil is not meaningful. Passing nil for filters is the same as passing nothing.
Sorry, you are right. I somehow thought omitting the nil would keep the previously set filter.

In the interest of consistency I agree that it should be kept this way. But the error message may be more clear?
curiosity
Filter Inserter
Filter Inserter
Posts: 478
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by curiosity »

Yeah, if you are already passing it a single (if custom) event, the error message is just wrong.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14363
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [1.1.76] Error when passing nil as event handler filter for custom inputs

Post by Rseding91 »

Thanks for the report. I've fixed the error message for the next release.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Resolved Problems and Bugs”