Page 1 of 1
[boskid] How to get the ID of a custom-event?
Posted: Thu Nov 28, 2024 6:25 pm
by Pi-C
We can now define custom events as CustomEventPrototype during the data stage. We can also use the name (string) of that prototype with script.raise_event and script.on_event. However, it seems that the name passed on with the event data is numeric:
12490.617 Script @__Pi-C_lib__/libs/debugging.lua:185: Yes: raising event minime_exchanged_characters!
12490.617 Script @__Pi-C_lib__/libs/debugging.lua:185:
====================================================================================================
Entered event script for unnamed event
Event data: player_index = 1, old_character = character "character" (517), new_character = character "IRobot_character_skin" (518), old_unit_number = 517, new_unit_number = 518, tick = 297822, mod_name = "minime"
(@__minime__/scripts/events.lua: 297)
====================================================================================================
12490.617 Script @__Pi-C_lib__/libs/debugging.lua:185:
----------------------------------------------------------------------------------------------------
Entered function event_handler({mod_name = "minime", name = 218, new_character = "[LuaEntity: IRobot_character_skin at [gps=6.8,-2.9]]", new_unit_number = 518, old_character = "[LuaEntity: character at [gps=7.0,-2.7]]", old_unit_number = 517, player_index = 1, tick = 297822})
(@__minime__/scripts/events.lua: 256)
----------------------------------------------------------------------------------------------------
Is there any way to derive the prototype name (string) from event.name(numeric)?
Re: How to get the ID of a custom-event?
Posted: Thu Nov 28, 2024 7:46 pm
by wizmut
Can you post the relevant code?
I assume you're looking at these pages:
https://lua-api.factorio.com/latest/eve ... InputEvent - particularly "selected_prototype"
https://lua-api.factorio.com/latest/pro ... otype.html - which mentions "include_selected_prototype" should be set to true
https://lua-api.factorio.com/latest/con ... eData.html - "derived_type"
Re: How to get the ID of a custom-event?
Posted: Fri Nov 29, 2024 9:03 am
by Pi-C
wizmut wrote: ↑Thu Nov 28, 2024 7:46 pm
Can you post the relevant code?
Sure. I create the event prototype in data.lua:
Code: Select all
data:extend({
{
type = "custom-event",
name = "minime_exchanged_characters",
}
})
In control, I raise the event like this:
Code: Select all
local pass_on = {
player_index = player.index,
old_character = old_char,
new_character = new_char,
old_unit_number = old_id,
new_unit_number = new_id,
god_mode = data.god_mode,
editor_mode = data.editor_mode
}
script.raise_event("minime_exchanged_characters", pass_on)
And I can listen to the event like that:
Code: Select all
script.on_event("minime_exchanged_characters", function(event) … end)
This is new since Factorio 2.0. Before, you had to use
script.generate_event_name to generate a numeric ID that you'd use when raising the event. However, the numeric ID could change, depending what other new/removed mods would generate their own events. So if you wanted to listen to a custom event, you first had to use
remote calls to ask the other mod for the event ID.
Re: [boskid] How to get the ID of a custom-event?
Posted: Fri Nov 29, 2024 2:15 pm
by Pi-C
@boskid: Hi! As I've just noticed you've taken an interest in this, there is a related issue with
LuaBootstrap::get_event_handler. Currently, passing on the name of a custom event will result in an error:
Code: Select all
11413.654 Error MainLoop.cpp:1432: Exception at tick 286131: Error while running command "minime-show-active-events": Unknown custom-input name: minime_exchanged_characters
stack traceback:
[C]: in function 'get_event_handler'
While we could work around this if we'd get the numeric ID of custom events, it may also be a good idea to allow the string name of a custom event as argument for script.get_event_handler. I hope that I don't come across as too greedy!
Also, could you tell me why
LuaBootstrap.generate_event_name is still available? It's a bit confusing to have two techniques to achieve the same thing (define a custom event). My guess is, it's still there for backwards compatibility but may be removed in the future – can you confirm that, or is there another reason?
Re: [boskid] How to get the ID of a custom-event?
Posted: Fri Nov 29, 2024 2:47 pm
by Xorimuth
Pi-C wrote: ↑Fri Nov 29, 2024 2:15 pm
Also, could you tell me why
LuaBootstrap.generate_event_name is still available? It's a bit confusing to have two techniques to achieve the same thing (define a custom event). My guess is, it's still there for backwards compatibility but may be removed in the future – can you confirm that, or is there another reason?
I can see some extremely niche uses for wanting to be able to dynamically generate events perhaps? Probably not something that would be worth adding if we'd already had the prototype-based custom events but since it is there already I guess it may as well stay.
Re: [boskid] How to get the ID of a custom-event?
Posted: Fri Nov 29, 2024 2:50 pm
by Klonan
Pi-C wrote: ↑Fri Nov 29, 2024 2:15 pm
Also, could you tell me why
LuaBootstrap.generate_event_name is still available? It's a bit confusing to have two techniques to achieve the same thing (define a custom event). My guess is, it's still there for backwards compatibility but may be removed in the future – can you confirm that, or is there another reason?
It is still useful for custom scenarios
Re: [boskid] How to get the ID of a custom-event?
Posted: Fri Nov 29, 2024 3:49 pm
by Pi-C
Xorimuth wrote: ↑Fri Nov 29, 2024 2:47 pm
I can see some extremely niche uses for wanting to be able to dynamically generate events perhaps?
Klonan wrote: ↑Fri Nov 29, 2024 2:50 pm
It is still useful for custom scenarios
Yes, makes sense. But that means we'd still have to keep two methods for registering custom events: the easy way by checking prototypes.custom_events, and by asking the other mods for the event ID per remote call. This could be circumvented if we could pass on a string (the name of the custom-event) and there was a map from event names to numeric IDs (similar to defines.events, but dynamic). Needless to say, I'm not sure whether this even could be implemented …