factoriolib / flib

A set of high-quality, commonly-used utilities for creating Factorio mods.
https://mods.factorio.com/mod/flib
MIT License
64 stars 15 forks source link

How to use flib.gui while using stdlib's event system? #19

Closed kevinmama closed 4 years ago

kevinmama commented 4 years ago

My mod is heavily depends on stdlib. Since stdlib don't have gui system like flib. so I add flib and try.

In flib, gui.register_handlers() will call script.on_event, stdlib's protect code detect directly call to script.on_event after stdlib event system initialized. And report an Error.

Is there any way to integrate stdlib and flib? The awesome gui system in flib will help me a lot.

JanSharp commented 4 years ago

as you can see in the docs, gui.register_handlers just registers event handerls for all gui events which just call gui.dispatch_handlers.

That means you can write handlers for all gui events yourself using stdlib's event system which just call gui.dispatch_handlers. Or you can basically copy the gui.register_handlers sourse and modify it to call register the handler through stdlib instead, for example:

function custom_register_handlers()
  for name, id in pairs(defines.events) do
    if string_sub(name, 1, 6) == "on_gui" then
      Event.register(id, function(e) flib_gui.dispatch_handlers(e) end)
    end
  end
end
raiguard commented 4 years ago

Yes, as JanSharp said, all that gui.register_handlers() does is register handlers that call gui.dispatch_handlers(e). So if you need to use stdlib's event system, you can use stdlib's event.register and have the handler call gui.dispatch_handlers(e). The snippet that JS provided in his comment will work perfectly.

Good luck!

kevinmama commented 3 years ago

Same issue for gui-beta. gui-beta call flib_gui.hook_events when it is load. I can't use my own event handler