Makes working with events in Factorio a lot more simple.
By default, Factorio allows you to register only one handler to an event.
This module lets you easily register multiple handlers to an event.
Using this module is as simple as replacing script.on_event with Event.register.
Due to the way that Factorio’s event system works, it is not recommended to interminglescript.on_event
andEvent.register
in a mod.
This module hooks into Factorio’s event system, and usingscript.on_event
for the same event will change which events are registered.
This module does not have many of the multiplayer protections thatscript.on_event
does.
Due to this, great care should be taken when registering events conditionally.
local Event = require('__stdlib__/stdlib/event/event')
register(event_id, handler[, filter=nil][, pattern=nil][, options=nil]) | Registers a handler for the given events. |
remove(event_id[, handler][, filter][, pattern]) | Removes a handler from the given events. |
on_load(...) | Shortcut for Event.register(Event.core_events.on_load, function) |
on_configuration_changed(...) | Shortcut for Event.register(Event.core_events.on_configuration_changed, function) |
on_init(...) | Shortcut for Event.register(Event.core_events.on_init, function) |
on_nth_tick(nth_tick, ...) | Shortcut for Event.register(-nthTick, function) |
on_event() | Shortcut for Event.register(defines.events, function) |
dispatch(event) | Calls the handlers that are registered to the given event. |
generate_event_name(event_name) | Retrieve or Generate an event_name and store it in Event.custom_events |
get_event_handler(event_id) | Get event handler. |
set_protected_mode(bool) | Set protected mode. |
set_debug_mode(bool) | Set debug mode default for Event module. |
set_option(option, bool) | Set default options for the event module. |
event_data | The user should create a table in this format, for a table that will be passed into Event.dispatch. |
Registers a handler for the given events.
If a nil
handler is passed, remove the given events and stop listening to them.
Events dispatch in the order they are registered.
An event ID can be obtained via defines.events, script.generate_event_name which is in int, and can be a custom input name which is in string.
The event_id
parameter takes in either a single, multiple, or mixture of defines.events, int, and string.
-- Create an event that prints the current tick every tick.
Event.register(defines.events.on_tick, function(event) game.print(event.tick) end)
-- Register something for Nth tick using negative numbers.
Event.register(-120, function() game.print('Every 120 ticks') end
-- Function call chaining
Event.register(event1, handler1).register(event2, handler2)
Removes a handler from the given events.
When the last handler for an event is removed, stop listening to that event.
An event ID can be obtained via defines.events, script.generate_event_name which is in int, and can be a custom input name which is in string.
The event_id
parameter takes in either a single, multiple, or mixture of defines.events, int, and string.
Shortcut for Event.register(Event.core_events.on_load, function)
Parameters:
Shortcut for Event.register(Event.core_events.on_configuration_changed, function)
Parameters:
Shortcut for Event.register(Event.core_events.on_init, function)
Parameters:
Shortcut for Event.register(-nthTick, function)
Parameters:
Shortcut for Event.register(defines.events, function)
Returns:
Calls the handlers that are registered to the given event.
Abort calling remaining handlers if any one of them has invalid userdata.
Handlers are dispatched in the order they were created.
Parameters:Retrieve or Generate an event_name and store it in Event.custom_events
Parameters:
Event.register(Event.generate_event_name("my_custom_event"), handler)
Get event handler.
Parameters:
Set protected mode.
Parameters:
Set debug mode default for Event module.
Parameters:
Set default options for the event module.
Parameters:
The user should create a table in this format, for a table that will be passed into Event.dispatch.
In general, the user should create an event data table that is in a similar format as the one that Factorio returns.
Fields:The event data table MUST have either
name
orinput_name
.
-- below code is from Trains module.
-- old_id & new_id are additional fields passed into the handler that's registered to Trains.on_train_id_changed event.
local event_data = {
old_id = renaming.old_id,
new_id = renaming.new_id,
name = Trains.on_train_id_changed
}
Event.dispatch(event_data)