custom-input: ability to dynamically consume events
Posted: Fri Aug 19, 2016 7:33 pm
I personally always use the "e" key to close GUIs in the game, but it's not currently possible to have to exact same behavior with mods.
The native game will always catch a close window (e) keypress if a GUI is opened rather than using the other (open inventory) action.
Trying to replicate the same thing for mods, using a global "close key" between the game and all mods, we hit a few problems, depending on the way we set the "consuming" key of the custom-input.
The native game will always catch a close window (e) keypress if a GUI is opened rather than using the other (open inventory) action.
Trying to replicate the same thing for mods, using a global "close key" between the game and all mods, we hit a few problems, depending on the way we set the "consuming" key of the custom-input.
- nil (default): the "e" keypress will always be passed to every mod (unless there's a native GUI open). This sounds good, except that this means it will pass the key to the mod, then execute the open inventory action
- "game-only": this looks better, but it means the open-inventory action will never be executed because it will be catched unconditionally by the mod.
- "scripts-only": the open-inventory action will execute AND only one mod can catch it
- "all" : open-inventory will never execute and only one mod can catch it
My proposal is to add a new "dynamic" key which will allow the input handler to decide whether it consumes the event or not, by returning a boolean:With that, a handler code would look like this:Code: Select all
data:extend({ { type = "custom-input", name = "mod-close-window", key_sequence = "e", consuming = "dynamic" }
This would meet all the uses cases :Code: Select all
script.on_event("mod-close-window", function(event) if (global.gui_opened) then close_gui() return true end end)
- If the game has a native GUI open, it will catch it (this seems to always be the case) which is fine
- If not it will call the event for the first mod in list.
- Assuming that first mod has no gui window open, it will return false or nil and the key will be propagated to the next mod
- If the next mod has a window open, it will close it and return true. This stops the propgation of the event which will not be passed the game and will not cause the open inventory action.
- If all mods have their windows closed, then the key press is passed to the game and will execute the open inventory action.