So, I have been writing Factorio scripts for nearly 3 years now, and I have decided upon late on the following solution for myself, I tried a few methods over the years:
First method: The standard Factorio way.
Code: Select all
script.on_event(defines.events.on_whatever, function(event)
if event.this and event.that then go on...
if not this or that then other_function() end
end)
Basically, I just dump all the script onto the event registration. It works in the simple cases, but not for more complex things.
Second method: Some conditional checking to point towards functions
Code: Select all
script.on_event(defines.event.on_entity_created, function(event)
if event.entity.name == "big-turret" then
big_turret_built(event.entity)
elseif event.entity.name == "small-turret" then
small_turret_build(event.entity)
end
end)
This method is nice in some ways, I can check for entity.valid in the event registration, and skip checking in the function call, and can keep things clean. But it can get messy and bloated past the more simple cases.
Third method: Throw the event to all the functions, and they do their own checking
Code: Select all
script.on_event(defines.events.on_entity_created, function(event)
big_turret_built(event)
small_turret_built(event)
other_something_built(event)
end)
This is cleanly, I can easily throw another function on the bottom of the stack, and not worry about anything else, or fitting it in with the rest of the script registration. The downside is, you have to check event entity validity in each function, over and over.
It can be inefficient, but I think you should always check anyway.
However when working with more scripts/modules, I ran into a problem, that you will have to register the events, but individually for each event.
For instance PvP will have a `on_gui_click` handler, and Silo-script will also have an `on_gui_click` handler, so I would need to 'know' that each of these scripts needs me to register the handler in the control script:
Code: Select all
script.on_event(defines.events.on_gui_click, function(event)
silo_script.on_gui_click(event)
pvp.on_gui_click(event)
end)
This is really laborious to do. Adding a new script/module means I have to open it and see which event it wants to be listened to, if it has a `on_entity_created`, I need to make sure I add that in my control.lua...
Current solution I am using: Each library handling all events
Code: Select all
script.on_event(defines.events, function(event)
for name, library in pairs (libraries) do
library.on_event(event)
end
end)
Then each library checks if it has a function for that event:
Code: Select all
events =
{
[defines.events.on_tick] = on_tick,
[defines.events.on_gui_click] = on_gui_click
}
lib.on_event = function(event)
action = events[event.name]
if action then
action(event)
end
end
This could be further optimized down the line, but for now it is really working out for me. The downside is the same as before, I need to check all event data validity before accessing it.
The benefit though of completely not caring is really great. I have a WIP mod now with 10+ scripts/modules, each with separate global data and script event handlers, and I have not run in to any problem for now.