I discussed this on the stdlib's thread but nobody responded.
Code: Select all
function test()
print("test")
end
Gui.Event.register (defines.events.on_player_armor_inventory_changed, "top", test)
Code: Select all
function test()
print("test")
end
Gui.Event.register (defines.events.on_player_armor_inventory_changed, "top", test)
So I can only have one event handler active at any given time?Supercheese wrote:From what I can tell, the StdLib forms a list of all events, and assigns them each only a single function, initially just a null function that does nothing. When you call register() on that event, your code gets added to that event-function, and will run each time the event fires. In this fashion, you could have multiple functions that are independently registered and de-registered from an event -- so maybe sometimes you want this part of on_tick to run, so you register it, then later you want another part, so you register that, but later you de-register the first bit, so it stops running while the second bit still goes. It's a bit tricky at times, but can be helpful for performance under certain circumstances.
EDIT: Also, you call register() with only two arguments, not three. See: http://afforess.github.io/Factorio-Stdl ... Event.html
This looks more like a mix between the gui events and the others.kiba wrote: Gui.Event.register (defines.events.on_player_armor_inventory_changed, "top", test)
[/code]
Code: Select all
Event.register(defines.events.on_player_armor_inventory_changed, test)
Code: Select all
Gui.on_click("button_name", test) --(Gui.on_click is short for Gui.Event.register(defines.events.on_gui_click, test)
Ah, my problem is trying to register "gui.top" rather than creating an element for it, before I could register it toward a GUI element?Choumiko wrote:This looks more like a mix between the gui events and the others.kiba wrote: Gui.Event.register (defines.events.on_player_armor_inventory_changed, "top", test)
[/code]
For non gui events it isEvent.register lets you register mutliple functions for a single event and let's you remove them individually, as Supercheese describedCode: Select all
Event.register(defines.events.on_player_armor_inventory_changed, test)
Haven't used the GUI events from stdlib yet, butshould call the function if a gui element named button_name is clickedCode: Select all
Gui.on_click("button_name", test) --(Gui.on_click is short for Gui.Event.register(defines.events.on_gui_click, test)
Gui.Event.register() can have multiple patterns assigned per event, but only one handler/function per pattern. (I might be wrong about this, didn't write the Gui.Event)
I'm not sure what you want to achieve, but if by gui.top you mean e.g. game.players[1].gui.top then it should look likekiba wrote:Ah, my problem is trying to register "gui.top" rather than creating an element for it, before I could register it toward a GUI element?
Code: Select all
Gui.on_click("top", test)
Code: Select all
function test()
LOG.log("asdf_asdf")
LOG.write()
print("test")
end
Event.register (defines.events.on_player_armor_inventory_changed, test)
Code: Select all
Event=require("stdlib.event.event")
local function do_once_tick(event)
for _, player in pairs(game.players) do
player.print("Once")
end
Event.remove(defines.events.on_tick, do_once_tick)
end
Event.register(defines.events.on_tick, do_once_tick)
Also there's no way this is MP safe since it would end up running every time someone connected/loaded a game.Nexela wrote:I use the events system all over without a problem. not sure why your example isn't working.
Code: Select all
Event=require("stdlib.event.event") local function do_once_tick(event) for _, player in pairs(game.players) do player.print("Once") end Event.remove(defines.events.on_tick, do_once_tick) end Event.register(defines.events.on_tick, do_once_tick)
Sure, but any such "fire once" system isn't going to be save-load stable since you can't save/load closures and it isn't going to fire that event until the next tick.Nexela wrote:That was example code :p
I am not exactly sure myself. Supposedly, the advantage of using Afforess' stdlib is that I don't have to do so much gruntwork.Rseding91 wrote:Just wondering, what's the advantage to aliasing script.on_event(...)?
To me, it seems like it's just going to obscure the code so someone else doesn't understand what's happening compared to just registering the events directly. It will also be ever so slightly slower because it has to do more function calls.
Main advantage is multiple handlers per event, and ability to deregister by handler, as well as built-in error handling.Rseding91 wrote:Just wondering, what's the advantage to aliasing script.on_event(...)?
To me, it seems like it's just going to obscure the code so someone else doesn't understand what's happening compared to just registering the events directly. It will also be ever so slightly slower because it has to do more function calls.
How are the event handlers persisted through save-load? And if not, how are the order of the registered events maintained if mods have to re-register on_load?Afforess wrote:Main advantage is multiple handlers per event, and ability to deregister by handler, as well as built-in error handling.Rseding91 wrote:Just wondering, what's the advantage to aliasing script.on_event(...)?
To me, it seems like it's just going to obscure the code so someone else doesn't understand what's happening compared to just registering the events directly. It will also be ever so slightly slower because it has to do more function calls.
Sorry about the slow response, hopefully you've figured things out by now Kiba.
They aren't, persisting closures is complex and generally not useful. The order is deterministic because event handlers are registered when the lua files are loaded, exactly how the script.on_event handlers are registered now. The Stdlib handlers just wrap those handlers with a table to allow multiple registration.Rseding91 wrote: How are the event handlers persisted through save-load? And if not, how are the order of the registered events maintained if mods have to re-register on_load?
That's assuming people use it like that. Some register event handlers conditionally. For example: runtime the player builds something and that activates the on_tick handler for say 30 seconds and then it turns off again.Afforess wrote:They aren't, persisting closures is complex and generally not useful. The order is deterministic because event handlers are registered when the lua files are loaded, exactly how the script.on_event handlers are registered now. The Stdlib handlers just wrap those handlers with a table to allow multiple registration.Rseding91 wrote: How are the event handlers persisted through save-load? And if not, how are the order of the registered events maintained if mods have to re-register on_load?
If it makes it more clear, stdlib event handlers *are* just wrappers. You can do a strict find-and-replace in a text editor, and replace script.on_event with Event.register, and your code will work exactly like before.
As long as the events are registered in a deterministic fashion (register on_tick after an assembler is created for 30s), and it occurs on all hosts, there is no problem, right? I believe the problem would be when a mod registers handlers based on the state of local variables or something else not deterministic.Rseding91 wrote:That's assuming people use it like that. Some register event handlers conditionally. For example: runtime the player builds something and that activates the on_tick handler for say 30 seconds and then it turns off again.Afforess wrote:They aren't, persisting closures is complex and generally not useful. The order is deterministic because event handlers are registered when the lua files are loaded, exactly how the script.on_event handlers are registered now. The Stdlib handlers just wrap those handlers with a table to allow multiple registration.Rseding91 wrote: How are the event handlers persisted through save-load? And if not, how are the order of the registered events maintained if mods have to re-register on_load?
If it makes it more clear, stdlib event handlers *are* just wrappers. You can do a strict find-and-replace in a text editor, and replace script.on_event with Event.register, and your code will work exactly like before.
Not sure if anyone does that with this library system but it's just something to think about
Afforess wrote:As long as the events are registered in a deterministic fashion (register on_tick after an assembler is created for 30s), and it occurs on all hosts, there is no problem, right? I believe the problem would be when a mod registers handlers based on the state of local variables or something else not deterministic.